Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -235,6 +235,15 @@ public function process_token( $stackPtr ) {
continue;
}

// Skip if this is a non-global namespaced function call.
$prevNonEmpty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $i - 1 ), null, true );
if ( \T_NS_SEPARATOR === $this->tokens[ $prevNonEmpty ]['code'] ) {
$prevPrevNonEmpty = $this->phpcsFile->findPrevious( Tokens::$emptyTokens, ( $prevNonEmpty - 1 ), null, true );
if ( \T_STRING === $this->tokens[ $prevPrevNonEmpty ]['code'] || \T_NAMESPACE === $this->tokens[ $prevPrevNonEmpty ]['code'] ) {
continue;
}
}

$content = strtolower( $this->tokens[ $i ]['content'] );

if ( isset( $this->cacheDeleteFunctions[ $content ] ) ) {
Expand Down
44 changes: 44 additions & 0 deletions WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc
Original file line number Diff line number Diff line change
Expand Up @@ -379,3 +379,47 @@ function methodNamesSameAsCacheFunctions() {

return $listofthings;
}

function fullyQualifiedCallsToCacheFunctions1() {
global $wpdb;

if ( ! ( $listofthings = \wp_cache_get( $foo ) ) ) {
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning direct DB call.
\wp_cache_set( 'foo', $listofthings );
}

return $listofthings;
}

function fullyQualifiedCallsToCacheFunctions2() {
global $wpdb;

$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
\wp_cache_delete( 'key', 'group' );
}

function callToNamespacedNonGlobalFunctions1() {
global $wpdb;

$listofthings = MyNamespace\wp_cache_get( $foo );
$listofthings = \MyNamespace\wp_cache_get( $foo );
$listofthings = namespace\wp_cache_get( $foo ); // The sniff should start considering this a valid WP cache function once it can resolve relative namespaces.

if ( ! $listofthings ) {
$listofthings = $wpdb->get_col( 'SELECT something FROM somewhere WHERE someotherthing = 1' ); // Warning x 2.
MyNamespace\wp_cache_set( 'foo', $listofthings );
\MyNamespace\wp_cache_set( 'foo', $listofthings );
namespace\wp_cache_set( 'foo', $listofthings ); // The sniff should start considering this a valid WP cache function once it can resolve relative namespaces.
}

return $listofthings;
}

function callToNamespacedNonGlobalFunctions2() {
global $wpdb;

$wpdb->query( 'SELECT X FROM Y' ); // Warning x 2.
MyNamespace\wp_cache_delete( 'key', 'group' );
\MyNamespace\wp_cache_delete( 'key', 'group' );
namespace\wp_cache_delete( 'key', 'group' ); // The sniff should start considering this a valid WP cache function once it can resolve relative namespaces.
}
4 changes: 4 additions & 0 deletions WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ public function getWarningList( $testFile = '' ) {
350 => 1,
364 => 2,
376 => 1,
387 => 1,
397 => 1,
409 => 2,
421 => 2,
);
default:
return array();
Expand Down
Loading