Skip to content

Commit 1bcf297

Browse files
committed
DB/DirectDatabaseQuery: make cache function name matching case-insensitive
The sniff was checking cache function names incorrectly, as it was not considering that PHP function names are case-insensitive, leading to false positives.
1 parent b4cf66e commit 1bcf297

File tree

3 files changed

+37
-6
lines changed

3 files changed

+37
-6
lines changed

WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,18 +229,19 @@ public function process_token( $stackPtr ) {
229229

230230
for ( $i = ( $scopeStart + 1 ); $i < $scopeEnd; $i++ ) {
231231
if ( \T_STRING === $this->tokens[ $i ]['code'] ) {
232+
$content = strtolower( $this->tokens[ $i ]['content'] );
232233

233-
if ( isset( $this->cacheDeleteFunctions[ $this->tokens[ $i ]['content'] ] ) ) {
234+
if ( isset( $this->cacheDeleteFunctions[ $content ] ) ) {
234235

235236
if ( \in_array( $method, array( 'query', 'update', 'replace', 'delete' ), true ) ) {
236237
$cached = true;
237238
break;
238239
}
239-
} elseif ( isset( $this->cacheGetFunctions[ $this->tokens[ $i ]['content'] ] ) ) {
240+
} elseif ( isset( $this->cacheGetFunctions[ $content ] ) ) {
240241

241242
$wp_cache_get = true;
242243

243-
} elseif ( isset( $this->cacheSetFunctions[ $this->tokens[ $i ]['content'] ] ) ) {
244+
} elseif ( isset( $this->cacheSetFunctions[ $content ] ) ) {
244245

245246
if ( $wp_cache_get ) {
246247
$cached = true;
@@ -277,6 +278,8 @@ protected function mergeFunctionLists() {
277278
$this->cacheGetFunctions
278279
);
279280

281+
$this->cacheGetFunctions = array_change_key_case( $this->cacheGetFunctions );
282+
280283
$this->addedCustomFunctions['cacheget'] = $this->customCacheGetFunctions;
281284
}
282285

@@ -286,6 +289,8 @@ protected function mergeFunctionLists() {
286289
$this->cacheSetFunctions
287290
);
288291

292+
$this->cacheSetFunctions = array_change_key_case( $this->cacheSetFunctions );
293+
289294
$this->addedCustomFunctions['cacheset'] = $this->customCacheSetFunctions;
290295
}
291296

@@ -295,6 +300,8 @@ protected function mergeFunctionLists() {
295300
$this->cacheDeleteFunctions
296301
);
297302

303+
$this->cacheDeleteFunctions = array_change_key_case( $this->cacheDeleteFunctions );
304+
298305
$this->addedCustomFunctions['cachedelete'] = $this->customCacheDeleteFunctions;
299306
}
300307
}

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ function bar() {
2121

2222
function baz() {
2323
global $wpdb;
24-
$baz = wp_cache_get( 'baz' );
24+
$baz = WP_CACHE_GET( 'baz' );
2525
if ( false !== $baz ) {
2626
$wpdb->query( 'ALTER TABLE TO ADD SOME FIELDS' ); // Warning x 2.
2727
$wpdb->query( $wpdb->prepare( 'CREATE TABLE ' ) ); // Warning x 2.
2828
$wpdb->query( 'SELECT QUERY' ); // Warning.
2929
$baz = $wpdb->get_results( $wpdb->prepare( 'SELECT X FROM Y ' ) ); // Warning.
30-
wp_cache_set( 'baz', $baz );
30+
WP_cache_SET( 'baz', $baz );
3131
}
3232
}
3333

@@ -66,7 +66,7 @@ function cache_delete_only() {
6666
$wpdb->get_row( 'SELECT X FROM Y' ); // Warning x 2.
6767
$wpdb->get_col( 'SELECT X FROM Y' ); // Warning x 2.
6868

69-
wp_cache_delete( 'key', 'group' );
69+
WP_CACHE_DELETE( 'key', 'group' );
7070
}
7171

7272
// It is OK to use the wp_cache_add() function instead of wp_cache_set().
@@ -332,3 +332,25 @@ function method_names_are_caseinsensitive() {
332332
global $wpdb;
333333
$autoload = $wpdb->Get_Var( $wpdb->Prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option_name ) ); // Warning x 2.
334334
}
335+
336+
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheGetFunctions[] MY_cacheget
337+
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheSetFunctions[] my_CACHESET
338+
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheDeleteFunctions[] MY_cachedel
339+
function cache_custom_mixed_case_A() {
340+
global $wpdb;
341+
$quux = my_cacheget( 'quux' );
342+
if ( false !== $quux ) {
343+
$wpdb->get_results( 'SELECT X FROM Y' ); // Warning direct DB call.
344+
my_cacheset( 'key', 'group' );
345+
}
346+
}
347+
348+
function cache_custom_mixed_case_B() {
349+
global $wpdb;
350+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
351+
my_cachedel( 'key', 'group' );
352+
}
353+
354+
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheGetFunctions[]
355+
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheSetFunctions[]
356+
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheDeleteFunctions[]

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ public function getWarningList( $testFile = '' ) {
9595
300 => 1,
9696
306 => 2,
9797
333 => 2,
98+
343 => 1,
99+
350 => 1,
98100
);
99101
default:
100102
return array();

0 commit comments

Comments
 (0)