-
Notifications
You must be signed in to change notification settings - Fork 43
Expand file tree
/
Copy pathOrderByRandSniff.php
More file actions
56 lines (51 loc) · 1.41 KB
/
OrderByRandSniff.php
File metadata and controls
56 lines (51 loc) · 1.41 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
<?php
/**
* WordPressVIPMinimum Coding Standard.
*
* @package VIPCS\WordPressVIPMinimum
* @link https://github.com/Automattic/VIP-Coding-Standards
* @license https://opensource.org/license/gpl-2-0 GPL-2.0
* @license https://opensource.org/licenses/MIT MIT
*/
namespace WordPressVIPMinimum\Sniffs\Performance;
use PHPCSUtils\Utils\TextStrings;
use WordPressCS\WordPress\AbstractArrayAssignmentRestrictionsSniff;
/**
* Flag using orderby => rand.
*
* @link https://docs.wpvip.com/technical-references/code-review/vip-errors/#h-order-by-rand
*
* @since 0.5.0
*/
class OrderByRandSniff extends AbstractArrayAssignmentRestrictionsSniff {
/**
* Groups of variables to restrict.
*
* @return array<string, array<string, string|array<string>>>
*/
public function getGroups() {
return [
'orderby' => [
'type' => 'error',
'message' => 'Detected forbidden query_var "%s" of %s. Use vip_get_random_posts() instead.',
'keys' => [
'orderby',
],
],
];
}
/**
* Callback to process each confirmed key, to check value
*
* @param string $key Array index / key.
* @param mixed $val Assigned value.
* @param int $line Token line.
* @param array $group Group definition.
*
* @return bool FALSE if no match, TRUE if matches.
*/
public function callback( $key, $val, $line, $group ) {
$val = TextStrings::stripQuotes( $val );
return strtolower( $val ) === 'rand';
}
}