Skip to content

Commit 990b1c9

Browse files
committed
Revert how the sniff handles namespaced functions that mirror the name of WP global cache functions
As discussed during the PR review, this reverts changes from the previous commit which filtered out namespaced cache function calls. After discussion, it was decided to maintain the existing behavior where namespaced cache functions like `MyNamespace\wp_cache_get()` are treated as valid custom cache implementations, consistent with how method calls like `$obj->wp_cache_get()` are handled. The tests have been updated to test the new behavior.
1 parent 4569963 commit 990b1c9

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -235,15 +235,6 @@ 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-
247238
$content = strtolower( $this->tokens[ $i ]['content'] );
248239

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

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -398,28 +398,56 @@ function fullyQualifiedCallsToCacheFunctions2() {
398398
\wp_cache_delete( 'key', 'group' );
399399
}
400400

401-
function callToNamespacedNonGlobalFunctions1() {
401+
function callToQualifiedNamespacedCacheGetSet() {
402402
global $wpdb;
403403

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.
404+
if ( ! ( $listofthings = MyNamespace\wp_cache_get( $foo ) ) ) {
405+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
410406
MyNamespace\wp_cache_set( 'foo', $listofthings );
407+
}
408+
409+
return $listofthings;
410+
}
411+
412+
function callToFullyQualifiedNamespacedCacheGetSet() {
413+
global $wpdb;
414+
415+
if ( ! ( $listofthings = \MyNamespace\wp_cache_get( $foo ) ) ) {
416+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
411417
\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.
413418
}
414419

415420
return $listofthings;
416421
}
417422

418-
function callToNamespacedNonGlobalFunctions2() {
423+
function callToRelativeNamespacedCacheGetSet() {
419424
global $wpdb;
420425

421-
$wpdb->query( 'SELECT X FROM Y' ); // Warning x 2.
426+
if ( ! ( $listofthings = namespace\wp_cache_get( $foo ) ) ) {
427+
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
428+
namespace\wp_cache_set( 'foo', $listofthings );
429+
}
430+
431+
return $listofthings;
432+
}
433+
434+
function callToQualifiedNamespacedCacheDelete() {
435+
global $wpdb;
436+
437+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
422438
MyNamespace\wp_cache_delete( 'key', 'group' );
439+
}
440+
441+
function callToFullyQualifiedNamespacedCacheDelete() {
442+
global $wpdb;
443+
444+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
423445
\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.
446+
}
447+
448+
function callToRelativeNamespacedCacheDelete() {
449+
global $wpdb;
450+
451+
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
452+
namespace\wp_cache_delete( 'key', 'group' );
425453
}

WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,12 @@ public function getWarningList( $testFile = '' ) {
101101
376 => 1,
102102
387 => 1,
103103
397 => 1,
104-
409 => 2,
105-
421 => 2,
104+
405 => 1,
105+
416 => 1,
106+
427 => 1,
107+
437 => 1,
108+
444 => 1,
109+
451 => 1,
106110
);
107111
default:
108112
return array();

0 commit comments

Comments
 (0)