Skip to content

Commit df070e7

Browse files
authored
Merge pull request #758 from Automattic/feature/various-improvements-to-sniff-extending-abstractarrayassignment
2 parents 31aaf41 + ae735aa commit df070e7

File tree

5 files changed

+27
-65
lines changed

5 files changed

+27
-65
lines changed

WordPressVIPMinimum/Sniffs/Performance/NoPagingSniff.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class NoPagingSniff extends AbstractArrayAssignmentRestrictionsSniff {
3030
public function getGroups() {
3131
return [
3232
'nopaging' => [
33-
'type' => 'error',
34-
'keys' => [
33+
'type' => 'error',
34+
'message' => 'Disabling pagination is prohibited in VIP context, do not set `%s` to `%s` ever.',
35+
'keys' => [
3536
'nopaging',
3637
],
3738
],
@@ -45,16 +46,12 @@ public function getGroups() {
4546
* @param mixed $val Assigned value.
4647
* @param int $line Token line.
4748
* @param array $group Group definition.
48-
* @return mixed FALSE if no match, TRUE if matches, STRING if matches
49-
* with custom error message passed to ->process().
49+
*
50+
* @return bool FALSE if no match, TRUE if matches.
5051
*/
5152
public function callback( $key, $val, $line, $group ) {
5253
$key = strtolower( $key );
5354

54-
if ( $key === 'nopaging' && ( $val === 'true' || $val === 1 ) ) {
55-
return 'Disabling pagination is prohibited in VIP context, do not set `%s` to `%s` ever.';
56-
}
57-
58-
return false;
55+
return ( $key === 'nopaging' && ( $val === 'true' || $val === 1 ) );
5956
}
6057
}

WordPressVIPMinimum/Sniffs/Performance/OrderByRandSniff.php

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,9 @@ class OrderByRandSniff extends AbstractArrayAssignmentRestrictionsSniff {
3030
public function getGroups() {
3131
return [
3232
'orderby' => [
33-
'type' => 'error',
34-
'keys' => [
33+
'type' => 'error',
34+
'message' => 'Detected forbidden query_var "%s" of "%s". Use vip_get_random_posts() instead.',
35+
'keys' => [
3536
'orderby',
3637
],
3738
],
@@ -40,19 +41,15 @@ public function getGroups() {
4041

4142
/**
4243
* Callback to process each confirmed key, to check value
43-
* This must be extended to add the logic to check assignment value
4444
*
4545
* @param string $key Array index / key.
4646
* @param mixed $val Assigned value.
4747
* @param int $line Token line.
4848
* @param array $group Group definition.
49-
* @return mixed FALSE if no match, TRUE if matches, STRING if matches with custom error message passed to ->process().
49+
*
50+
* @return bool FALSE if no match, TRUE if matches.
5051
*/
5152
public function callback( $key, $val, $line, $group ) {
52-
if ( strtolower( $val ) === 'rand' ) {
53-
return 'Detected forbidden query_var "%s" of "%s". Use vip_get_random_posts() instead.';
54-
}
55-
56-
return false;
53+
return strtolower( $val ) === 'rand';
5754
}
5855
}

WordPressVIPMinimum/Sniffs/Performance/RegexpCompareSniff.php

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,14 @@ class RegexpCompareSniff extends AbstractArrayAssignmentRestrictionsSniff {
1919
/**
2020
* Groups of variables to restrict.
2121
*
22-
* Example: groups => array(
23-
* 'groupname' => array(
24-
* 'type' => 'error' | 'warning',
25-
* 'message' => 'Dont use this one please!',
26-
* 'keys' => array( 'key1', 'another_key' ),
27-
* 'callback' => array( 'class', 'method' ), // Optional.
28-
* )
29-
* )
30-
*
3122
* @return array
3223
*/
3324
public function getGroups() {
3425
return [
3526
'compare' => [
36-
'type' => 'error',
37-
'keys' => [
27+
'type' => 'error',
28+
'message' => 'Detected regular expression comparison. `%s` is set to `%s`.',
29+
'keys' => [
3830
'compare',
3931
'meta_compare',
4032
],
@@ -44,21 +36,18 @@ public function getGroups() {
4436

4537
/**
4638
* Callback to process each confirmed key, to check value.
47-
* This must be extended to add the logic to check assignment value.
4839
*
4940
* @param string $key Array index / key.
5041
* @param mixed $val Assigned value.
5142
* @param int $line Token line.
5243
* @param array $group Group definition.
53-
* @return mixed FALSE if no match, TRUE if matches, STRING if matches
54-
* with custom error message passed to ->process().
44+
*
45+
* @return bool FALSE if no match, TRUE if matches.
5546
*/
5647
public function callback( $key, $val, $line, $group ) {
57-
if ( strpos( $val, 'NOT REGEXP' ) === 0
48+
return ( strpos( $val, 'NOT REGEXP' ) === 0
5849
|| strpos( $val, 'REGEXP' ) === 0
5950
|| in_array( $val, [ 'REGEXP', 'NOT REGEXP' ], true ) === true
60-
) {
61-
return 'Detected regular expression comparison. `%s` is set to `%s`.';
62-
}
51+
);
6352
}
6453
}

WordPressVIPMinimum/Sniffs/Performance/RemoteRequestTimeoutSniff.php

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,22 +19,14 @@ class RemoteRequestTimeoutSniff extends AbstractArrayAssignmentRestrictionsSniff
1919
/**
2020
* Groups of variables to restrict.
2121
*
22-
* Example: groups => array(
23-
* 'groupname' => array(
24-
* 'type' => 'error' | 'warning',
25-
* 'message' => 'Dont use this one please!',
26-
* 'keys' => array( 'key1', 'another_key' ),
27-
* 'callback' => array( 'class', 'method' ), // Optional.
28-
* )
29-
* )
30-
*
3122
* @return array
3223
*/
3324
public function getGroups() {
3425
return [
3526
'timeout' => [
36-
'type' => 'error',
37-
'keys' => [
27+
'type' => 'error',
28+
'message' => 'Detected high remote request timeout. `%s` is set to `%d`.',
29+
'keys' => [
3830
'timeout',
3931
],
4032
],
@@ -43,18 +35,15 @@ public function getGroups() {
4335

4436
/**
4537
* Callback to process each confirmed key, to check value.
46-
* This must be extended to add the logic to check assignment value.
4738
*
4839
* @param string $key Array index / key.
4940
* @param mixed $val Assigned value.
5041
* @param int $line Token line.
5142
* @param array $group Group definition.
52-
* @return mixed FALSE if no match, TRUE if matches, STRING if matches
53-
* with custom error message passed to ->process().
43+
*
44+
* @return bool FALSE if no match, TRUE if matches.
5445
*/
5546
public function callback( $key, $val, $line, $group ) {
56-
if ( (int) $val > 3 ) {
57-
return 'Detected high remote request timeout. `%s` is set to `%d`.';
58-
}
47+
return (int) $val > 3;
5948
}
6049
}

WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,16 +34,6 @@ public function register() {
3434

3535
/**
3636
* Groups of variables to restrict.
37-
* This should be overridden in extending classes.
38-
*
39-
* Example: groups => array(
40-
* 'groupname' => array(
41-
* 'type' => 'error' | 'warning',
42-
* 'message' => 'Dont use this one please!',
43-
* 'keys' => array( 'key1', 'another_key' ),
44-
* 'callback' => array( 'class', 'method' ), // Optional.
45-
* )
46-
* )
4737
*
4838
* @return array
4939
*/
@@ -94,8 +84,8 @@ public function process_token( $stackPtr ) {
9484
* @param mixed $val Assigned value.
9585
* @param int $line Token line.
9686
* @param array $group Group definition.
97-
* @return mixed FALSE if no match, TRUE if matches, STRING if matches
98-
* with custom error message passed to ->process().
87+
*
88+
* @return bool FALSE if no match, TRUE if matches.
9989
*/
10090
public function callback( $key, $val, $line, $group ) {
10191
return true;

0 commit comments

Comments
 (0)