Skip to content

Commit d8fd190

Browse files
committed
DB/PreparedSQLPlaceholders: update for PHPCS 4.0
The tokenization of (namespaced) "names" has changed in PHP 8.0, and this new tokenization will come into effect for PHP_CodeSniffer as of version 4.0.0. This commit adds handling for the new tokenization of fully qualified static calls to `wpdb::prepare()`, `sprintf()` and `implode()`.
1 parent c62e70b commit d8fd190

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

WordPress/Sniffs/DB/PreparedSQLPlaceholdersSniff.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ public function register() {
166166
return array(
167167
\T_VARIABLE,
168168
\T_STRING,
169+
\T_NAME_FULLY_QUALIFIED,
169170
);
170171
}
171172

@@ -225,7 +226,9 @@ public function process_token( $stackPtr ) {
225226
}
226227

227228
// Detect a specific pattern for variable replacements in combination with `IN`.
228-
if ( \T_STRING === $this->tokens[ $i ]['code'] ) {
229+
if ( \T_STRING === $this->tokens[ $i ]['code']
230+
|| \T_NAME_FULLY_QUALIFIED === $this->tokens[ $i ]['code']
231+
) {
229232

230233
if ( $this->is_global_function_call( $i, 'sprintf' ) ) {
231234
$sprintf_parameters = PassedParameters::getParameters( $this->phpcsFile, $i );
@@ -765,7 +768,7 @@ protected function analyse_implode( $implode_token ) {
765768
* Check if a token represents a call to a global function with the specified name.
766769
*
767770
* This method verifies that:
768-
* - The token is a T_STRING.
771+
* - The token is a T_STRING or T_NAME_FULLY_QUALIFIED.
769772
* - The token content matches the function name (case-insensitive).
770773
* - It's followed by an opening parenthesis.
771774
* - It's not a method call (no object operator before it).
@@ -783,11 +786,19 @@ protected function analyse_implode( $implode_token ) {
783786
* @return bool True if it's a call to the global function, false otherwise.
784787
*/
785788
protected function is_global_function_call( $functionPtr, $function_name ) {
786-
if ( \T_STRING !== $this->tokens[ $functionPtr ]['code'] ) {
789+
if ( \T_STRING !== $this->tokens[ $functionPtr ]['code']
790+
&& \T_NAME_FULLY_QUALIFIED !== $this->tokens[ $functionPtr ]['code']
791+
) {
787792
return false;
788793
}
789794

790-
if ( strtolower( $this->tokens[ $functionPtr ]['content'] ) !== $function_name ) {
795+
$content_lc = strtolower( $this->tokens[ $functionPtr ]['content'] );
796+
797+
if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $functionPtr ]['code'] ) {
798+
$content_lc = \ltrim( $content_lc, '\\' );
799+
}
800+
801+
if ( $content_lc !== $function_name ) {
791802
return false;
792803
}
793804

0 commit comments

Comments
 (0)