Skip to content

Commit 783c30a

Browse files
committed
LowExpiryCacheTimeSniff: Account for random generating number functions used as cache expiry time
1 parent 37c5059 commit 783c30a

File tree

3 files changed

+54
-15
lines changed

3 files changed

+54
-15
lines changed

WordPressVIPMinimum/Sniffs/Performance/LowExpiryCacheTimeSniff.php

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,18 @@ class LowExpiryCacheTimeSniff extends AbstractFunctionParameterSniff {
5353
'YEAR_IN_SECONDS' => 31536000,
5454
];
5555

56+
/**
57+
* List of random generating number functions.
58+
*
59+
* @var array
60+
*/
61+
protected $rand_functions = [
62+
'wp_rand',
63+
'random_int',
64+
'mt_rand',
65+
'rand',
66+
];
67+
5668
/**
5769
* Process the parameters of a matched function.
5870
*
@@ -69,21 +81,34 @@ public function process_parameters( $stackPtr, $group_name, $matched_content, $p
6981
return;
7082
}
7183

72-
$time = $parameters[4]['raw'];
73-
74-
if ( false === is_numeric( $time ) ) {
75-
// If using time constants, we need to convert to a number.
76-
$time = str_replace( array_keys( $this->wp_time_constants ), $this->wp_time_constants, $time );
84+
// If using time constants, we need to convert to a number.
85+
$time = str_replace( array_keys( $this->wp_time_constants ), $this->wp_time_constants, $parameters[4]['raw'] );
7786

78-
if ( preg_match( '#^[\s\d+*/-]+$#', $time ) > 0 ) {
79-
$time = eval( "return $time;" ); // phpcs:ignore Squiz.PHP.Eval -- No harm here.
87+
$rand_function = false;
88+
foreach ( $this->rand_functions as $fn ) {
89+
if ( false !== strpos( $time, $fn ) ) {
90+
$rand_function = $fn;
91+
break;
8092
}
8193
}
8294

83-
if ( $time < 300 ) {
84-
$message = 'Low cache expiry time of "%s", it is recommended to have 300 seconds or more.';
85-
$data = [ $parameters[4]['raw'] ];
86-
$this->phpcsFile->addWarning( $message, $stackPtr, 'LowCacheTime', $data );
95+
$times = [];
96+
if ( false !== $rand_function ) {
97+
$times = explode( ',', preg_replace( '/[( )|\(|\)|(' . $rand_function . ')]/', '', $time ) );
98+
} else {
99+
$times[] = $time;
100+
}
101+
102+
foreach ( $times as $time ) {
103+
if ( preg_match( '#^[\s\d+*\/-]+$#', $time ) > 0 ) {
104+
$time = eval( "return $time;" ); // phpcs:ignore Squiz.PHP.Eval -- No harm here.
105+
}
106+
if ( $time < 300 || is_null( $time ) ) {
107+
$message = 'Low cache expiry time of "%s", it is recommended to have 300 seconds or more.';
108+
$data = [ $parameters[4]['raw'] ];
109+
$this->phpcsFile->addWarning( $message, $stackPtr, 'LowCacheTime', $data );
110+
return;
111+
}
87112
}
88113
}
89114
}

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.inc

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,11 @@ wp_cache_replace( $testing, $data, 'test_group', 8*MINUTE_IN_SECONDS );
2323
wp_cache_replace( 1234, $data, '', 425 );
2424
wp_cache_replace( $testing, $data, null, 350 );
2525

26+
wp_cache_set( 'test', $data, '', wp_rand( 5*MINUTE_IN_SECONDS, 10*MINUTE_IN_SECONDS ) );
27+
wp_cache_add( 'test', $data, '',rand(400, 20*MINUTE_IN_SECONDS ));
28+
wp_cache_replace( 'test', $data, null, mt_rand( 500, 200*HOUR_IN_SECONDS) );
29+
wp_cache_set( 'test', $data,'', random_int( 200 * HOUR_IN_SECONDS , 350 ) );
30+
2631
// Bad.
2732
wp_cache_set( 'test', $data, $group, 100 ); // Lower than 300.
2833
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.
3843
wp_cache_replace( 'test', $data, $group, 2*MINUTE_IN_SECONDS ); // Lower than 300.
3944
wp_cache_replace( 123, $data, null, 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
4045
wp_cache_replace( $testing, $data, '', 1.5 * MINUTE_IN_SECONDS ); // Lower than 300.
46+
47+
wp_cache_set( 'test', $data, '', wp_rand( 5*MINUTE_IN_SECONDS, 1*MINUTE_IN_SECONDS ) ); // Lower than 300.
48+
wp_cache_add( 'test', $data, '',rand(null, 20*MINUTE_IN_SECONDS )); // Lower than 300.
49+
wp_cache_replace( 'test', $data, null, mt_rand( 200 , 200*HOUR_IN_SECONDS) ); // Lower than 300.
50+
wp_cache_set( 'test', $data,'', random_int( 200 * HOUR_IN_SECONDS ,299 ) ); // Lower than 300.

WordPressVIPMinimum/Tests/Performance/LowExpiryCacheTimeUnitTest.php

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,10 +32,6 @@ public function getErrorList() {
3232
*/
3333
public function getWarningList() {
3434
return [
35-
27 => 1,
36-
28 => 1,
37-
29 => 1,
38-
30 => 1,
3935
32 => 1,
4036
33 => 1,
4137
34 => 1,
@@ -44,6 +40,14 @@ public function getWarningList() {
4440
38 => 1,
4541
39 => 1,
4642
40 => 1,
43+
42 => 1,
44+
43 => 1,
45+
44 => 1,
46+
45 => 1,
47+
47 => 1,
48+
48 => 1,
49+
49 => 1,
50+
50 => 1,
4751
];
4852
}
4953
}

0 commit comments

Comments
 (0)