Skip to content
Merged
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
13 changes: 10 additions & 3 deletions WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,18 +229,19 @@ public function process_token( $stackPtr ) {

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

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

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

$wp_cache_get = true;

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

if ( $wp_cache_get ) {
$cached = true;
Expand Down Expand Up @@ -277,6 +278,8 @@ protected function mergeFunctionLists() {
$this->cacheGetFunctions
);

$this->cacheGetFunctions = array_change_key_case( $this->cacheGetFunctions );

$this->addedCustomFunctions['cacheget'] = $this->customCacheGetFunctions;
}

Expand All @@ -286,6 +289,8 @@ protected function mergeFunctionLists() {
$this->cacheSetFunctions
);

$this->cacheSetFunctions = array_change_key_case( $this->cacheSetFunctions );

$this->addedCustomFunctions['cacheset'] = $this->customCacheSetFunctions;
}

Expand All @@ -295,6 +300,8 @@ protected function mergeFunctionLists() {
$this->cacheDeleteFunctions
);

$this->cacheDeleteFunctions = array_change_key_case( $this->cacheDeleteFunctions );

$this->addedCustomFunctions['cachedelete'] = $this->customCacheDeleteFunctions;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,13 @@ function bar() {

function baz() {
global $wpdb;
$baz = wp_cache_get( 'baz' );
$baz = WP_CACHE_GET( 'baz' );
if ( false !== $baz ) {
$wpdb->query( 'ALTER TABLE TO ADD SOME FIELDS' ); // Warning x 2.
$wpdb->query( $wpdb->prepare( 'CREATE TABLE ' ) ); // Warning x 2.
$wpdb->query( 'SELECT QUERY' ); // Warning.
$baz = $wpdb->get_results( $wpdb->prepare( 'SELECT X FROM Y ' ) ); // Warning.
wp_cache_set( 'baz', $baz );
WP_cache_SET( 'baz', $baz );
}
}

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

wp_cache_delete( 'key', 'group' );
WP_CACHE_DELETE( 'key', 'group' );
}

// It is OK to use the wp_cache_add() function instead of wp_cache_set().
Expand Down Expand Up @@ -333,6 +333,24 @@ function method_names_are_caseinsensitive() {
$autoload = $wpdb->Get_Var( $wpdb->Prepare( "SELECT autoload FROM $wpdb->options WHERE option_name = %s", $option_name ) ); // Warning x 2.
}

// Live coding/parse error test.
// This must be the last test in the file.
$wpdb->get_col( '
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheGetFunctions[] MY_cacheget
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheSetFunctions[] my_CACHESET
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheDeleteFunctions[] MY_cachedel
function cache_custom_mixed_case_A() {
global $wpdb;
$quux = my_cacheget( 'quux' );
if ( false !== $quux ) {
$wpdb->get_results( 'SELECT X FROM Y' ); // Warning direct DB call.
my_cacheset( 'key', 'group' );
}
}

function cache_custom_mixed_case_B() {
global $wpdb;
$wpdb->query( 'SELECT X FROM Y' ); // Warning direct DB call.
my_cachedel( 'key', 'group' );
}

// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheGetFunctions[]
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheSetFunctions[]
// phpcs:set WordPress.DB.DirectDatabaseQuery customCacheDeleteFunctions[]
8 changes: 8 additions & 0 deletions WordPress/Tests/DB/DirectDatabaseQueryUnitTest.2.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

/*
* Intentional parse error (unclosed string).
* This should be the only test in this file.
*/

$wpdb->get_col( '
119 changes: 64 additions & 55 deletions WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,62 +35,71 @@ public function getErrorList() {
/**
* Returns the lines where warnings should occur.
*
* @param string $testFile The name of the test file being run.
*
* @return array<int, int> Key is the line number, value is the number of expected warnings.
*/
public function getWarningList() {
return array(
5 => 2,
12 => 1,
26 => 2,
27 => 2,
28 => 1,
29 => 1,
38 => 2,
44 => 2,
60 => 1,
61 => 1,
62 => 1,
63 => 1,
65 => 2,
66 => 2,
67 => 2,
80 => 1,
81 => 1,
82 => 1,
83 => 1,
84 => 1,
85 => 1,
86 => 1,
97 => 1,
114 => 1,
123 => 1,
130 => 1,
141 => 1,
150 => 2,
157 => 2,
168 => 2,
175 => 1,
180 => 1,
185 => 1,
190 => 1,
195 => 1,
200 => 1,
205 => 1,
210 => 1,
215 => 1,
220 => 1,
228 => 2,
235 => 2,
251 => 1,
252 => 1,
265 => 1,
269 => 1,
281 => 1,
287 => 2,
288 => 1,
300 => 1,
306 => 2,
333 => 2,
);
public function getWarningList( $testFile = '' ) {
switch ( $testFile ) {
case 'DirectDatabaseQueryUnitTest.1.inc':
return array(
5 => 2,
12 => 1,
26 => 2,
27 => 2,
28 => 1,
29 => 1,
38 => 2,
44 => 2,
60 => 1,
61 => 1,
62 => 1,
63 => 1,
65 => 2,
66 => 2,
67 => 2,
80 => 1,
81 => 1,
82 => 1,
83 => 1,
84 => 1,
85 => 1,
86 => 1,
97 => 1,
114 => 1,
123 => 1,
130 => 1,
141 => 1,
150 => 2,
157 => 2,
168 => 2,
175 => 1,
180 => 1,
185 => 1,
190 => 1,
195 => 1,
200 => 1,
205 => 1,
210 => 1,
215 => 1,
220 => 1,
228 => 2,
235 => 2,
251 => 1,
252 => 1,
265 => 1,
269 => 1,
281 => 1,
287 => 2,
288 => 1,
300 => 1,
306 => 2,
333 => 2,
343 => 1,
350 => 1,
);
default:
return array();
}
}
}
Loading