Skip to content

Commit 9e2a835

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 89d9c1d commit 9e2a835

File tree

1 file changed

+31
-6
lines changed

1 file changed

+31
-6
lines changed

WordPress/Sniffs/DB/PreparedSQLPlaceholdersSniff.php

Lines changed: 31 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,7 @@ public function register() {
165165
return array(
166166
\T_VARIABLE,
167167
\T_STRING,
168+
\T_NAME_FULLY_QUALIFIED,
168169
);
169170
}
170171

@@ -224,9 +225,16 @@ public function process_token( $stackPtr ) {
224225
}
225226

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

229-
if ( 'sprintf' === strtolower( $this->tokens[ $i ]['content'] ) ) {
233+
if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $i ]['code'] ) {
234+
$content_lowercase = \ltrim( $content_lowercase, '\\' );
235+
}
236+
237+
if ( 'sprintf' === $content_lowercase ) {
230238
$sprintf_parameters = PassedParameters::getParameters( $this->phpcsFile, $i );
231239

232240
if ( ! empty( $sprintf_parameters ) ) {
@@ -265,7 +273,7 @@ public function process_token( $stackPtr ) {
265273
}
266274
unset( $sprintf_parameters, $valid_sprintf, $last_param );
267275

268-
} elseif ( 'implode' === strtolower( $this->tokens[ $i ]['content'] ) ) {
276+
} elseif ( 'implode' === $content_lowercase ) {
269277
$ignore_tokens = Tokens::$emptyTokens + array(
270278
\T_STRING_CONCAT => \T_STRING_CONCAT,
271279
\T_NS_SEPARATOR => \T_NS_SEPARATOR,
@@ -679,10 +687,17 @@ protected function analyse_sprintf( $sprintf_params ) {
679687
$sprintf_param['end'],
680688
true
681689
);
690+
682691
if ( \T_STRING === $this->tokens[ $implode ]['code']
683-
&& 'implode' === strtolower( $this->tokens[ $implode ]['content'] )
692+
|| \T_NAME_FULLY_QUALIFIED === $this->tokens[ $implode ]['code']
684693
) {
685-
if ( $this->analyse_implode( $implode ) === true ) {
694+
$content_lowercase = \strtolower( $this->tokens[ $implode ]['content'] );
695+
696+
if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $implode ]['code'] ) {
697+
$content_lowercase = \ltrim( $content_lowercase, '\\' );
698+
}
699+
700+
if ( 'implode' === $content_lowercase && $this->analyse_implode( $implode ) === true ) {
686701
++$found;
687702
}
688703
}
@@ -738,11 +753,21 @@ protected function analyse_implode( $implode_token ) {
738753
);
739754

740755
if ( \T_STRING !== $this->tokens[ $array_fill ]['code']
741-
|| 'array_fill' !== strtolower( $this->tokens[ $array_fill ]['content'] )
756+
&& \T_NAME_FULLY_QUALIFIED !== $this->tokens[ $array_fill ]['code']
742757
) {
743758
return false;
744759
}
745760

761+
$content_lowercase = strtolower( $this->tokens[ $array_fill ]['content'] );
762+
763+
if ( \T_NAME_FULLY_QUALIFIED === $this->tokens[ $array_fill ]['code'] ) {
764+
$content_lowercase = \ltrim( $content_lowercase, '\\' );
765+
}
766+
767+
if ( 'array_fill' !== $content_lowercase ) {
768+
return false;
769+
}
770+
746771
$array_fill_value_param = PassedParameters::getParameter( $this->phpcsFile, $array_fill, 3, 'value' );
747772
if ( false === $array_fill_value_param ) {
748773
return false;

0 commit comments

Comments
 (0)