Skip to content

Commit 4569963

Browse files
committed
DB/DirectDatabaseQuery: fix handling of namespaced calls to cache functions
Before this change, the sniff was not properly differentiating between fully qualified calls to global WP cache functions and namespaced calls to non-WP functions with the same name as the WP functions. In other words, the sniff was correctly identifying `\wp_cache_get()` as a WP cache function, but it was incorrectly identifying `MyNamespace\wp_cache_get()` as a WP cache function. This commit fixes this issue.
1 parent 93c8284 commit 4569963

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,15 @@ public function process_token( $stackPtr ) {
235235
continue;
236236
}
237237

238+
// Skip if this is a non-global namespaced function call.
239+
$prevNonEmpty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $i - 1 ), null, true );
240+
if ( \T_NS_SEPARATOR === $this->tokens[ $prevNonEmpty ]['code'] ) {
241+
$prevPrevNonEmpty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $prevNonEmpty - 1 ), null, true );
242+
if ( \T_STRING === $this->tokens[ $prevPrevNonEmpty ]['code'] || \T_NAMESPACE === $this->tokens[ $prevPrevNonEmpty ]['code'] ) {
243+
continue;
244+
}
245+
}
246+
238247
$content = strtolower( $this->tokens[ $i ]['content'] );
239248

240249
if ( isset( $this->cacheDeleteFunctions[ $content ] ) ) {

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -379,3 +379,47 @@ function methodNamesSameAsCacheFunctions() {
379379

380380
return $listofthings;
381381
}
382+
383+
function fullyQualifiedCallsToCacheFunctions1() {
384+
global $wpdb;
385+
386+
if ( ! ( $listofthings = \wp_cache_get( $foo ) ) ) {
387+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
388+
\wp_cache_set( 'foo', $listofthings );
389+
}
390+
391+
return $listofthings;
392+
}
393+
394+
function fullyQualifiedCallsToCacheFunctions2() {
395+
global $wpdb;
396+
397+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
398+
\wp_cache_delete( 'key', 'group' );
399+
}
400+
401+
function callToNamespacedNonGlobalFunctions1() {
402+
global $wpdb;
403+
404+
$listofthings = MyNamespace\wp_cache_get( $foo );
405+
$listofthings = \MyNamespace\wp_cache_get( $foo );
406+
$listofthings = namespace\wp_cache_get( $foo ); // The sniff should start considering this a valid WP cache function once it can resolve relative namespaces.
407+
408+
if ( ! $listofthings ) {
409+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning x 2.
410+
MyNamespace\wp_cache_set( 'foo', $listofthings );
411+
\MyNamespace\wp_cache_set( 'foo', $listofthings );
412+
namespace\wp_cache_set( 'foo', $listofthings ); // The sniff should start considering this a valid WP cache function once it can resolve relative namespaces.
413+
}
414+
415+
return $listofthings;
416+
}
417+
418+
function callToNamespacedNonGlobalFunctions2() {
419+
global $wpdb;
420+
421+
$wpdb->query( 'SELECT X FROM Y' ); // Warning x 2.
422+
MyNamespace\wp_cache_delete( 'key', 'group' );
423+
\MyNamespace\wp_cache_delete( 'key', 'group' );
424+
namespace\wp_cache_delete( 'key', 'group' ); // The sniff should start considering this a valid WP cache function once it can resolve relative namespaces.
425+
}

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,10 @@ public function getWarningList( $testFile = '' ) {
9999
350 => 1,
100100
364 => 2,
101101
376 => 1,
102+
387 => 1,
103+
397 => 1,
104+
409 => 2,
105+
421 => 2,
102106
);
103107
default:
104108
return array();

0 commit comments

Comments
 (0)