-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathLowExpiryCacheTimeSniff.php
More file actions
114 lines (102 loc) · 3.22 KB
/
LowExpiryCacheTimeSniff.php
File metadata and controls
114 lines (102 loc) · 3.22 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
*/
namespace WordPressVIPMinimum\Sniffs\Performance;
use WordPressCS\WordPress\AbstractFunctionParameterSniff;
/**
* This sniff throws a warning when low cache times are set.
*
* @package VIPCS\WordPressVIPMinimum
*
* @since 0.4.0
*/
class LowExpiryCacheTimeSniff extends AbstractFunctionParameterSniff {
/**
* The group name for this group of functions.
*
* @var string
*/
protected $group_name = 'cache_functions';
/**
* Functions this sniff is looking for.
*
* @var array The only requirement for this array is that the top level
* array keys are the names of the functions you're looking for.
* Other than that, the array can have arbitrary content
* depending on your needs.
*/
protected $target_functions = [
'wp_cache_set' => true,
'wp_cache_add' => true,
'wp_cache_replace' => true,
];
/**
* List of WP time constants, see https://codex.wordpress.org/Easier_Expression_of_Time_Constants.
*
* @var array
*/
protected $wp_time_constants = [
'MINUTE_IN_SECONDS' => 60,
'HOUR_IN_SECONDS' => 3600,
'DAY_IN_SECONDS' => 86400,
'WEEK_IN_SECONDS' => 604800,
'MONTH_IN_SECONDS' => 2592000,
'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.
*
* @param int $stackPtr The position of the current token in the stack.
* @param array $group_name The name of the group which was matched.
* @param string $matched_content The token content (function name) which was matched.
* @param array $parameters Array with information about the parameters.
* @return int|void Integer stack pointer to skip forward or void to continue
* normal file processing.
*/
public function process_parameters( $stackPtr, $group_name, $matched_content, $parameters ) {
if ( false === isset( $parameters[4] ) ) {
// If no cache expiry time, bail (i.e. we don't want to flag for something like feeds where it is cached indefinitely until a hook runs).
return;
}
// 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'] );
$rand_function = false;
foreach ( $this->rand_functions as $fn ) {
if ( false !== strpos( $time, $fn ) ) {
$rand_function = $fn;
break;
}
}
$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;
}
}
}
}