Skip to content

Commit d20de47

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 calls to `\wpdb::prepare()`, `\sprintf()`, `\implode()` and `\array_fill()`.
1 parent e9af795 commit d20de47

File tree

1 file changed

+14
-3
lines changed

1 file changed

+14
-3
lines changed

WordPress/Sniffs/DB/PreparedSQLPlaceholdersSniff.php

Lines changed: 14 additions & 3 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 );
@@ -775,11 +778,19 @@ protected function analyse_implode( $implode_token ) {
775778
* @return bool True if it's a call to the global function, false otherwise.
776779
*/
777780
protected function is_global_function_call( $function_ptr, $function_name ) {
778-
if ( \T_STRING !== $this->tokens[ $function_ptr ]['code'] ) {
781+
if ( \T_STRING !== $this->tokens[ $function_ptr ]['code']
782+
&& \T_NAME_FULLY_QUALIFIED !== $this->tokens[ $function_ptr ]['code']
783+
) {
779784
return false;
780785
}
781786

782-
if ( strtolower( $this->tokens[ $function_ptr ]['content'] ) !== $function_name ) {
787+
$content_lc = strtolower( $this->tokens[ $function_ptr ]['content'] );
788+
789+
if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $function_ptr ]['code'] ) {
790+
$content_lc = \ltrim( $content_lc, '\\' );
791+
}
792+
793+
if ( $content_lc !== $function_name ) {
783794
return false;
784795
}
785796

0 commit comments

Comments
 (0)