diff --git a/WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php b/WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php index 590f6478ea..1f30ce1740 100644 --- a/WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php +++ b/WordPress/Sniffs/DB/DirectDatabaseQuerySniff.php @@ -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; @@ -277,6 +278,8 @@ protected function mergeFunctionLists() { $this->cacheGetFunctions ); + $this->cacheGetFunctions = array_change_key_case( $this->cacheGetFunctions ); + $this->addedCustomFunctions['cacheget'] = $this->customCacheGetFunctions; } @@ -286,6 +289,8 @@ protected function mergeFunctionLists() { $this->cacheSetFunctions ); + $this->cacheSetFunctions = array_change_key_case( $this->cacheSetFunctions ); + $this->addedCustomFunctions['cacheset'] = $this->customCacheSetFunctions; } @@ -295,6 +300,8 @@ protected function mergeFunctionLists() { $this->cacheDeleteFunctions ); + $this->cacheDeleteFunctions = array_change_key_case( $this->cacheDeleteFunctions ); + $this->addedCustomFunctions['cachedelete'] = $this->customCacheDeleteFunctions; } } diff --git a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.inc b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc similarity index 91% rename from WordPress/Tests/DB/DirectDatabaseQueryUnitTest.inc rename to WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc index b08d6bea2f..2691023a8c 100644 --- a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.inc +++ b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.1.inc @@ -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 ); } } @@ -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(). @@ -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[] diff --git a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.2.inc b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.2.inc new file mode 100644 index 0000000000..ed8c6b75a2 --- /dev/null +++ b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.2.inc @@ -0,0 +1,8 @@ +get_col( ' diff --git a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php index e12828a1aa..d5543bafa5 100644 --- a/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php +++ b/WordPress/Tests/DB/DirectDatabaseQueryUnitTest.php @@ -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 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(); + } } }