Skip to content

Commit

Permalink
LowExpiryCacheTimeSniff: Account for random generating number functio…
Browse files Browse the repository at this point in the history
…ns used as cache expiry time
  • Loading branch information
rebeccahum committed Aug 29, 2019
1 parent 37c5059 commit 783c30a
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 15 deletions.
47 changes: 36 additions & 11 deletions WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand All @@ -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;
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
];
}
}

0 comments on commit 783c30a

Please sign in to comment.