diff --git a/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php b/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php index 20ae980f..8364691f 100644 --- a/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php +++ b/WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php @@ -53,6 +53,18 @@ class LowExpiryCacheTimeSniff extends AbstractFunctionParameterSniff { 'YEAR_IN_SECONDS' => 31536000, ]; + /** + * List of random generating number functions. + * + * @var array + */ + protected $rand_functions = [ + 'wp_rand', + 'random_int', + 'mt_rand', + 'rand', + ]; + /** * Process the parameters of a matched function. * @@ -69,21 +81,34 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p return; } - $time = $parameters[4]['raw']; - - if ( false === is_numeric( $time ) ) { - // If using time constants, we need to convert to a number. - $time = str_replace( array_keys( $this->wp_time_constants ), $this->wp_time_constants, $time ); + // If using time constants, we need to convert to a number. + $time = str_replace( array_keys( $this->wp_time_constants ), $this->wp_time_constants, $parameters[4]['raw'] ); - if ( preg_match( '#^[\s\d+*/-]+$#', $time ) > 0 ) { - $time = eval( "return $time;" ); // phpcs:ignore Squiz.PHP.Eval -- No harm here. + $rand_function = false; + foreach ( $this->rand_functions as $fn ) { + if ( false !== strpos( $time, $fn ) ) { + $rand_function = $fn; + break; } } - if ( $time < 300 ) { - $message = 'Low cache expiry time of "%s", it is recommended to have 300 seconds or more.'; - $data = [ $parameters[4]['raw'] ]; - $this->phpcsFile->addWarning( $message, $stackPtr, 'LowCacheTime', $data ); + $times = []; + if ( false !== $rand_function ) { + $times = explode( ',', preg_replace( '/[( )|\(|\)|(' . $rand_function . ')]/', '', $time ) ); + } else { + $times[] = $time; + } + + foreach ( $times as $time ) { + if ( preg_match( '#^[\s\d+*\/-]+$#', $time ) > 0 ) { + $time = eval( "return $time;" ); // phpcs:ignore Squiz.PHP.Eval -- No harm here. + } + if ( $time < 300 || is_null( $time ) ) { + $message = 'Low cache expiry time of "%s", it is recommended to have 300 seconds or more.'; + $data = [ $parameters[4]['raw'] ]; + $this->phpcsFile->addWarning( $message, $stackPtr, 'LowCacheTime', $data ); + return; + } } } } diff --git a/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.inc b/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.inc index eb5a63cd..5204ac06 100644 --- a/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.inc +++ b/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.inc @@ -23,6 +23,11 @@ wp_cache_replace( $testing, $data, 'test_group', 8*MINUTE_IN_SECONDS ); wp_cache_replace( 1234, $data, '', 425 ); wp_cache_replace( $testing, $data, null, 350 ); +wp_cache_set( 'test', $data, '', wp_rand( 5*MINUTE_IN_SECONDS, 10*MINUTE_IN_SECONDS ) ); +wp_cache_add( 'test', $data, '',rand(400, 20*MINUTE_IN_SECONDS )); +wp_cache_replace( 'test', $data, null, mt_rand( 500, 200*HOUR_IN_SECONDS) ); +wp_cache_set( 'test', $data,'', random_int( 200 * HOUR_IN_SECONDS , 350 ) ); + // Bad. wp_cache_set( 'test', $data, $group, 100 ); // Lower than 300. wp_cache_set( 'test', $data, $group, 2*MINUTE_IN_SECONDS ); // Lower than 300. @@ -38,3 +43,8 @@ wp_cache_replace( 'test', $data, $group, 100 ); // Lower than 300. wp_cache_replace( 'test', $data, $group, 2*MINUTE_IN_SECONDS ); // Lower than 300. wp_cache_replace( 123, $data, null, 1.5 * MINUTE_IN_SECONDS ); // Lower than 300. wp_cache_replace( $testing, $data, '', 1.5 * MINUTE_IN_SECONDS ); // Lower than 300. + +wp_cache_set( 'test', $data, '', wp_rand( 5*MINUTE_IN_SECONDS, 1*MINUTE_IN_SECONDS ) ); // Lower than 300. +wp_cache_add( 'test', $data, '',rand(null, 20*MINUTE_IN_SECONDS )); // Lower than 300. +wp_cache_replace( 'test', $data, null, mt_rand( 200 , 200*HOUR_IN_SECONDS) ); // Lower than 300. +wp_cache_set( 'test', $data,'', random_int( 200 * HOUR_IN_SECONDS ,299 ) ); // Lower than 300. diff --git a/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php b/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php index 6befe13e..8386bc45 100644 --- a/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php +++ b/WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php @@ -32,10 +32,6 @@ public function getErrorList() { */ public function getWarningList() { return [ - 27 => 1, - 28 => 1, - 29 => 1, - 30 => 1, 32 => 1, 33 => 1, 34 => 1, @@ -44,6 +40,14 @@ public function getWarningList() { 38 => 1, 39 => 1, 40 => 1, + 42 => 1, + 43 => 1, + 44 => 1, + 45 => 1, + 47 => 1, + 48 => 1, + 49 => 1, + 50 => 1, ]; } }