Skip to content

Commit e26eb87

Browse files
authored
Merge pull request #2613 from rodrigoprimo/direct-database-query-case-insensitive
DB/DirectDatabaseQuery: make cache function name matching case-insensitive
2 parents 637711c + 1bcf297 commit e26eb87

File tree

4 files changed

+106
-64
lines changed

4 files changed

+106
-64
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.inc renamed to WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc

Lines changed: 24 additions & 6 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().
@@ -333,6 +333,24 @@ function method_names_are_caseinsensitive() {
333333
$autoload = $wpdb->Get_Var( $wpdb->Prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option_name ) ); // Warning x 2.
334334
}
335335

336-
// Live coding/parse error test.
337-
// This must be the last test in the file.
338-
$wpdb->get_col( '
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[]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?php
2+
3+
/*
4+
* Intentional parse error (unclosed string).
5+
* This should be the only test in this file.
6+
*/
7+
8+
$wpdb->get_col( '

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -35,62 +35,71 @@ public function getErrorList() {
3535
/**
3636
* Returns the lines where warnings should occur.
3737
*
38+
* @param string $testFile The name of the test file being run.
39+
*
3840
* @return array<int, int> Key is the line number, value is the number of expected warnings.
3941
*/
40-
public function getWarningList() {
41-
return array(
42-
5 => 2,
43-
12 => 1,
44-
26 => 2,
45-
27 => 2,
46-
28 => 1,
47-
29 => 1,
48-
38 => 2,
49-
44 => 2,
50-
60 => 1,
51-
61 => 1,
52-
62 => 1,
53-
63 => 1,
54-
65 => 2,
55-
66 => 2,
56-
67 => 2,
57-
80 => 1,
58-
81 => 1,
59-
82 => 1,
60-
83 => 1,
61-
84 => 1,
62-
85 => 1,
63-
86 => 1,
64-
97 => 1,
65-
114 => 1,
66-
123 => 1,
67-
130 => 1,
68-
141 => 1,
69-
150 => 2,
70-
157 => 2,
71-
168 => 2,
72-
175 => 1,
73-
180 => 1,
74-
185 => 1,
75-
190 => 1,
76-
195 => 1,
77-
200 => 1,
78-
205 => 1,
79-
210 => 1,
80-
215 => 1,
81-
220 => 1,
82-
228 => 2,
83-
235 => 2,
84-
251 => 1,
85-
252 => 1,
86-
265 => 1,
87-
269 => 1,
88-
281 => 1,
89-
287 => 2,
90-
288 => 1,
91-
300 => 1,
92-
306 => 2,
93-
333 => 2,
94-
);
42+
public function getWarningList( $testFile = '' ) {
43+
switch ( $testFile ) {
44+
case 'DirectDatabaseQueryUnitTest.1.inc':
45+
return array(
46+
5 => 2,
47+
12 => 1,
48+
26 => 2,
49+
27 => 2,
50+
28 => 1,
51+
29 => 1,
52+
38 => 2,
53+
44 => 2,
54+
60 => 1,
55+
61 => 1,
56+
62 => 1,
57+
63 => 1,
58+
65 => 2,
59+
66 => 2,
60+
67 => 2,
61+
80 => 1,
62+
81 => 1,
63+
82 => 1,
64+
83 => 1,
65+
84 => 1,
66+
85 => 1,
67+
86 => 1,
68+
97 => 1,
69+
114 => 1,
70+
123 => 1,
71+
130 => 1,
72+
141 => 1,
73+
150 => 2,
74+
157 => 2,
75+
168 => 2,
76+
175 => 1,
77+
180 => 1,
78+
185 => 1,
79+
190 => 1,
80+
195 => 1,
81+
200 => 1,
82+
205 => 1,
83+
210 => 1,
84+
215 => 1,
85+
220 => 1,
86+
228 => 2,
87+
235 => 2,
88+
251 => 1,
89+
252 => 1,
90+
265 => 1,
91+
269 => 1,
92+
281 => 1,
93+
287 => 2,
94+
288 => 1,
95+
300 => 1,
96+
306 => 2,
97+
333 => 2,
98+
343 => 1,
99+
350 => 1,
100+
);
101+
default:
102+
return array();
103+
}
95104
}
96105
}

0 commit comments

Comments
 (0)