Skip to content

Commit 0ab9037

Browse files
committed
Performance/LowExpiryCacheTime: cleaner error message
The PHPCSUtils `PassedParameters::getParameters()` return value includes a `'clean'` array index, which contains the contents of the parameter stripped of surrounding whitespace and comments. The `LowExpiryCacheTime` sniff uses the parameter contents in the error message, so using the `clean` value makes for a more readable error message. Includes updating two pre-existing tests. Before: ``` 76 | WARNING | Cache expiry time could not be determined. Please inspect that the fourth parameter passed to wp_cache_set() evaluates to 300 | | seconds or more. Found: "20 * $time /*comment*/" (WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined) ``` After: ``` 76 | WARNING | Cache expiry time could not be determined. Please inspect that the fourth parameter passed to wp_cache_set() evaluates to 300 | | seconds or more. Found: "20 * $time" (WordPressVIPMinimum.Performance.LowExpiryCacheTime.CacheTimeUndetermined) ```
1 parent 58578d1 commit 0ab9037

File tree

2 files changed

+3
-3
lines changed

2 files changed

+3
-3
lines changed

WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
8080

8181
$message = 'Cache expiry time could not be determined. Please inspect that the fourth parameter passed to %s() evaluates to 300 seconds or more. Found: "%s"';
8282
$error_code = 'CacheTimeUndetermined';
83-
$data = [ $matched_content, $parameters[4]['raw'] ];
83+
$data = [ $matched_content, $parameters[4]['clean'] ];
8484

8585
for ( $i = $param['start']; $i <= $param['end']; $i++ ) {
8686
if ( isset( Tokens::$emptyTokens[ $this->tokens[ $i ]['code'] ] ) === true ) {

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.1.inc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ wp_cache_add(
7373
// Test variable/constant with or without calculation being passed.
7474
WP_Cache_Set( $key, $data, '', $time ); // Manual inspection warning.
7575
wp_cache_set( $key, $data, '', PREFIX_FIVE_MINUTES ); // Manual inspection warning.
76-
wp_cache_set( $key, $data, '', 20 * $time ); // Manual inspection warning.
76+
wp_cache_set( $key, $data, '', 20 * $time /*comment*/ ); // Manual inspection warning.
7777
wp_cache_set( $key, $data, '', $base + $extra, ); // Manual inspection warning.
7878
\wp_cache_set( $key, $data, '', 300 + $extra ); // Manual inspection warning.
7979
wp_cache_set( $key, $data, '', PREFIX_CUSTOM_TIME * 5); // Manual inspection warning.
@@ -110,7 +110,7 @@ wp_cache_set( 'test', $data, $group, \MONTH_IN_SECONDS ); // OK.
110110

111111
// Test passing something which may look like one of the time constants, but isn't.
112112
wp_cache_set( 'test', $data, $group, month_in_seconds ); // Bad - constants are case-sensitive.
113-
wp_cache_set( 'test', $data, $group, HOUR_IN_SECONDS::methodName() ); // Bad - not a constant.
113+
wp_cache_set( 'test', $data, $group, /*comment*/ HOUR_IN_SECONDS::methodName() ); // Bad - not a constant.
114114
wp_cache_set( 'test', $data, $group, $obj->MONTH_IN_SECONDS ); // Bad - not a constant.
115115
wp_cache_set( 'test', $data, $group, $obj::MONTH_IN_SECONDS ); // Bad - not the WP constant.
116116
WP_Cache_Set( 'test', $data, $group, PluginNamespace\SubLevel\DAY_IN_SECONDS, ); // Bad - not the WP constant.

0 commit comments

Comments
 (0)