-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathSlowMetaQuerySniff.php
More file actions
329 lines (288 loc) · 9.19 KB
/
SlowMetaQuerySniff.php
File metadata and controls
329 lines (288 loc) · 9.19 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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
<?php
namespace HM\Sniffs\Performance;
use PHP_CodeSniffer\Files\File;
use PHP_CodeSniffer\Util\Tokens;
use PHPCSUtils\Utils\Arrays;
use PHPCSUtils\Utils\MessageHelper;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
/**
* Flag slow meta queries.
*/
class SlowMetaQuerySniff extends AbstractArrayAssignmentRestrictionsSniff {
/**
* Indicates a dynamic value.
*/
const DYNAMIC_VALUE = '__dynamic';
/**
* Current stack pointer.
*
* @var int
*/
protected $stackPtr;
/**
* Groups of variables to restrict.
*
* @return array
*/
public function getGroups() {
return array(
'slow_query' => array(
'type' => 'warning',
'message' => 'Querying by %s is not performant.',
'keys' => array(
'meta_query',
'meta_value',
),
),
);
}
/**
* Process a token.
*
* Overrides the parent to store the stackPtr for later use.
*
* @param int $stackPtr
*/
public function process_token( $stackPtr ) {
$this->stackPtr = $stackPtr;
parent::process_token( $stackPtr );
unset( $this->stackPtr );
}
/**
* Callback to process each confirmed key, to check value.
* This must be extended to add the logic to check assignment value.
*
* @param string $key Array index / key.
* @param mixed $val Assigned value.
* @param int $line Token line.
* @param array $group Group definition.
* @return mixed FALSE if no match, TRUE if matches, STRING if matches
* with custom error message passed to ->process().
*/
public function callback( $key, $val, $line, $group ) {
switch ( $key ) {
case 'meta_value':
// When meta_value is specified, the query operates on the value,
// and is hence expensive. (UNLESS: meta_compare is set)
return true;
case 'meta_query':
return $this->check_meta_query();
default:
// Unknown key, assume it's an error.
return true;
}
}
/**
* Recursively check a meta_query value.
*/
protected function check_meta_query() {
// Grab the token we're detecting.
$token = $this->tokens[ $this->stackPtr ];
// Find the value of meta_query, and check it.
$array_open = $this->phpcsFile->findNext( array_merge( Tokens::$emptyTokens, [ T_COMMA, T_CLOSE_SHORT_ARRAY ] ), $this->stackPtr + 1, null, true );
$this->check_meta_query_item( $array_open );
// Disable the built-in warnings.
return false;
}
/**
* Check an individual meta_query item.
*
* @param int $array_open Token pointer for the array open token.
*/
protected function check_meta_query_item( int $array_open ) {
$array_open_token = $this->tokens[ $array_open ];
if ( $array_open_token['code'] !== T_ARRAY && $array_open_token['code'] !== T_OPEN_SHORT_ARRAY ) {
// Dynamic value, we can't check.
MessageHelper::addMessage(
$this->phpcsFile,
'meta_query is dynamic, cannot be checked.',
$array_open,
'warning',
'dynamic_query'
);
return;
}
$array_bounds = Arrays::getOpenClose( $this->phpcsFile, $array_open );
$elements = $this->get_array_indices( $array_bounds['opener'], $array_bounds['closer'] );
// Is this a "first-order" query?
// @see WP_Meta_Query::is_first_order_clause
$first_order_key = $this->find_key_in_array( $elements, 'key' );
$first_order_value = $this->find_key_in_array( $elements, 'value' );
if ( $first_order_key || $first_order_value ) {
$compare_element = $this->find_key_in_array( $elements, 'compare' );
if ( ! empty( $compare_element ) ) {
$compare = $this->get_static_value_for_element( $compare_element );
}
if ( empty( $compare ) ) {
// The default is either IN or = depending on whether value is
// set, but this only matters for the message.
$compare = 'default';
}
$this->check_compare_value( $compare, $compare_element ? $compare_element['value_start'] : null );
return;
}
foreach ( $elements as $element ) {
if ( isset( $element['index_start'] ) ) {
$index = TextStrings::stripQuotes( $this->tokens[ $element['index_start'] ]['content'] );
if ( strtolower( $index ) === 'relation' ) {
// Skip 'relation' element.
continue;
}
}
// Otherwise, recurse.
$this->check_meta_query_item( $element['value_start'] );
}
}
/**
* Get a static value from an array.
*
* @param array $elements Elements from the array (from get_array_indices())
* @param string $array_key Key to find in the array.
* @return string|null Static value if available, null otherwise.
*/
protected function get_static_value_for_element( array $element ) : ?string {
// Got the compare, grab the value.
$value_start = $element['value_start'];
if ( $this->tokens[ $value_start ]['code'] !== T_CONSTANT_ENCAPSED_STRING ) {
// Dynamic value.
return static::DYNAMIC_VALUE;
}
$maybe_value_end = $this->phpcsFile->findNext( Tokens::$emptyTokens, $value_start + 1, null, true );
$expected_next = [
T_CLOSE_PARENTHESIS,
T_CLOSE_SHORT_ARRAY,
T_COMMA,
];
if ( ! in_array( $this->tokens[ $maybe_value_end ]['code'], $expected_next, true ) ) {
// Dynamic value.
return static::DYNAMIC_VALUE;
}
return TextStrings::stripQuotes( $this->tokens[ $value_start ]['content'] );
}
/**
* Find a given key in an array.
*
* Searches a list of elements for a given (static) index.
*
* @param array $elements Elements from the array (from get_array_indices())
* @param string $array_key Key to find in the array.
* @return string|null Static value if available, null otherwise.
*/
protected function find_key_in_array( array $elements, string $array_key ) : ?array {
foreach ( $elements as $element ) {
if ( ! isset( $element['index_start'] ) ) {
// Numeric item, skip.
continue;
}
// Ensure the index is a static string first.
$start = $element['index_start'];
if ( $this->tokens[ $start ]['code'] !== T_CONSTANT_ENCAPSED_STRING ) {
// Dynamic key.
continue;
}
$maybe_index_end = $this->phpcsFile->findNext( Tokens::$emptyTokens, $start + 1, null, true );
if ( $this->tokens[ $maybe_index_end ]['code'] !== T_DOUBLE_ARROW ) {
// Dynamic key, maybe? This is probably not valid syntax.
continue;
}
$index = TextStrings::stripQuotes( $this->tokens[ $start ]['content'] );
if ( $index !== $array_key ) {
// Not the item we want, skip.
continue;
}
return $element;
}
return null;
}
/**
* Get array indices information.
*
* @internal From phpcs' AbstractArraySniff::get_array_indices
*
* @param integer $array_start
* @param integer $array_end
* @return array
*/
protected function get_array_indices( int $array_start, int $array_end ) : array {
$indices = [];
$current = $array_start;
while ( ( $next = $this->phpcsFile->findNext( Tokens::$emptyTokens, ( $current + 1 ), $array_end, true ) ) !== false ) {
$end = $this->get_next( $this->phpcsFile, $next, $array_end );
if ( $this->tokens[ $end ]['code'] === T_DOUBLE_ARROW ) {
$indexEnd = $this->phpcsFile->findPrevious( T_WHITESPACE, $end - 1, null, true );
$value_start = $this->phpcsFile->findNext( Tokens::$emptyTokens, $end + 1, null, true);
$indices[] = [
'index_start' => $next,
'index_end' => $indexEnd,
'arrow' => $end,
'value_start' => $value_start,
];
} else {
$value_start = $next;
$indices[] = [
'value_start' => $value_start,
];
}
$current = $this->get_next( $this->phpcsFile, $value_start, $array_end );
}
return $indices;
}
/**
* Add an error if the comparison isn't allowed.
*
* @param string $compare Comparison value
*/
protected function check_compare_value( string $compare, int $stackPtr = null ) : void {
if ( empty( $stackPtr ) ) {
$stackPtr = $this->stackPtr;
}
if ( $compare === static::DYNAMIC_VALUE ) {
MessageHelper::addMessage(
$this->phpcsFile,
'meta_query is using a dynamic comparison; this cannot be checked automatically, and may be non-performant.',
$stackPtr,
'warning',
'dynamic_compare'
);
} elseif ( $compare !== 'EXISTS' && $compare !== 'NOT EXISTS' ) {
// Add a message ourselves.
MessageHelper::addMessage(
$this->phpcsFile,
'meta_query is using %s comparison, which is non-performant.',
$stackPtr,
'warning',
'nonperformant_comparison',
[ $compare ]
);
}
}
/**
* Find next separator in array - either: comma or double arrow.
*
* @internal From phpcs' AbstractArraySniff::getNext
*
* @param File $phpcsFile The current file being checked.
* @param int $ptr The position of current token.
* @param int $arrayEnd The token that ends the array definition.
*
* @return int
*/
protected function get_next( File $phpcsFile, $ptr, $arrayEnd ) {
$tokens = $phpcsFile->getTokens();
while ( $ptr < $arrayEnd ) {
if ( isset( $tokens[ $ptr ]['scope_closer']) === true ) {
$ptr = $tokens[ $ptr ]['scope_closer'];
} elseif ( isset( $tokens[ $ptr ]['parenthesis_closer'] ) === true ) {
$ptr = $tokens[ $ptr ]['parenthesis_closer'];
} elseif ( isset( $tokens[ $ptr ]['bracket_closer'] ) === true ) {
$ptr = $tokens[ $ptr ]['bracket_closer'];
}
if ( $tokens[ $ptr ]['code'] === T_COMMA || $tokens[ $ptr ]['code'] === T_DOUBLE_ARROW ) {
return $ptr;
}
++$ptr;
}
return $ptr;
}
}