Documentation

WPDBTrait

Helper utilities for sniffs which examine WPDB method calls.

Tags
since
3.0.0

The method in this trait was previously contained in the WordPressCS\WordPress\Sniff class and has been moved here.

Table of Contents

Methods

is_wpdb_method_call()  : bool
Checks whether this is a call to one of a specific group of $wpdb method(s).

Methods

is_wpdb_method_call()

Checks whether this is a call to one of a specific group of $wpdb method(s).

protected final is_wpdb_method_call(File $phpcsFile, int $stackPtr, array<string|int, mixed> $target_methods) : bool

Supports both instance method calls (e.g., $wpdb->prepare()) and static method calls (e.g., wpdb::esc_like()).

Note: Static calls on non-static wpdb methods are problematic at runtime, but this helper still matches them so sniffs can flag them in the code under scan.

If the following properties are explicitly declared in the class using this trait, they will be automatically set:

  • $methodPtr: Stack pointer to the method name.
  • $i: Stack pointer to the opening parenthesis of the method call.
  • $end: Stack pointer to the comma after the first parameter, or to the token directly after the first parameter if there is no comma.

The $methodPtr and $i properties may be set even when this method returns false (e.g., for property access like $wpdb->show_errors).

Parameters
$phpcsFile : File

The file being scanned.

$stackPtr : int

The index of the $wpdb variable or wpdb (class) name token.

$target_methods : array<string|int, mixed>

Array of methods. Key(s) should be method name in lowercase.

Tags
since
0.8.0
0.9.0

The return value is now always boolean. The $end and $i member vars are automatically updated.

0.14.0

Moved this method from the PreparedSQL sniff to the base WP sniff.

3.0.0
  • Moved from the Sniff class to this dedicated Trait. - The $phpcsFile parameter was added.

}

Return values
bool —

Whether this is a $wpdb method call.

On this page

Search results