Skip to content

Commit

Permalink
Merge pull request #758 from Automattic/feature/various-improvements-…
Browse files Browse the repository at this point in the history
…to-sniff-extending-abstractarrayassignment
  • Loading branch information
GaryJones authored Jun 24, 2023
2 parents 31aaf41 + ae735aa commit df070e7
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 65 deletions.
15 changes: 6 additions & 9 deletions WordPressVIPMinimum/Sniffs/Performance/NoPagingSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class NoPagingSniff extends AbstractArrayAssignmentRestrictionsSniff {
public function getGroups() {
return [
'nopaging' => [
'type' => 'error',
'keys' => [
'type' => 'error',
'message' => 'Disabling pagination is prohibited in VIP context, do not set `%s` to `%s` ever.',
'keys' => [
'nopaging',
],
],
Expand All @@ -45,16 +46,12 @@ public function getGroups() {
* @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().
*
* @return bool FALSE if no match, TRUE if matches.
*/
public function callback( $key, $val, $line, $group ) {
$key = strtolower( $key );

if ( $key === 'nopaging' && ( $val === 'true' || $val === 1 ) ) {
return 'Disabling pagination is prohibited in VIP context, do not set `%s` to `%s` ever.';
}

return false;
return ( $key === 'nopaging' && ( $val === 'true' || $val === 1 ) );
}
}
15 changes: 6 additions & 9 deletions WordPressVIPMinimum/Sniffs/Performance/OrderByRandSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@ class OrderByRandSniff extends AbstractArrayAssignmentRestrictionsSniff {
public function getGroups() {
return [
'orderby' => [
'type' => 'error',
'keys' => [
'type' => 'error',
'message' => 'Detected forbidden query_var "%s" of "%s". Use vip_get_random_posts() instead.',
'keys' => [
'orderby',
],
],
Expand All @@ -40,19 +41,15 @@ public function getGroups() {

/**
* 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().
*
* @return bool FALSE if no match, TRUE if matches.
*/
public function callback( $key, $val, $line, $group ) {
if ( strtolower( $val ) === 'rand' ) {
return 'Detected forbidden query_var "%s" of "%s". Use vip_get_random_posts() instead.';
}

return false;
return strtolower( $val ) === 'rand';
}
}
25 changes: 7 additions & 18 deletions WordPressVIPMinimum/Sniffs/Performance/RegexpCompareSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ class RegexpCompareSniff extends AbstractArrayAssignmentRestrictionsSniff {
/**
* Groups of variables to restrict.
*
* Example: groups => array(
* 'groupname' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Dont use this one please!',
* 'keys' => array( 'key1', 'another_key' ),
* 'callback' => array( 'class', 'method' ), // Optional.
* )
* )
*
* @return array
*/
public function getGroups() {
return [
'compare' => [
'type' => 'error',
'keys' => [
'type' => 'error',
'message' => 'Detected regular expression comparison. `%s` is set to `%s`.',
'keys' => [
'compare',
'meta_compare',
],
Expand All @@ -44,21 +36,18 @@ public function getGroups() {

/**
* 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().
*
* @return bool FALSE if no match, TRUE if matches.
*/
public function callback( $key, $val, $line, $group ) {
if ( strpos( $val, 'NOT REGEXP' ) === 0
return ( strpos( $val, 'NOT REGEXP' ) === 0
|| strpos( $val, 'REGEXP' ) === 0
|| in_array( $val, [ 'REGEXP', 'NOT REGEXP' ], true ) === true
) {
return 'Detected regular expression comparison. `%s` is set to `%s`.';
}
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,14 @@ class RemoteRequestTimeoutSniff extends AbstractArrayAssignmentRestrictionsSniff
/**
* Groups of variables to restrict.
*
* Example: groups => array(
* 'groupname' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Dont use this one please!',
* 'keys' => array( 'key1', 'another_key' ),
* 'callback' => array( 'class', 'method' ), // Optional.
* )
* )
*
* @return array
*/
public function getGroups() {
return [
'timeout' => [
'type' => 'error',
'keys' => [
'type' => 'error',
'message' => 'Detected high remote request timeout. `%s` is set to `%d`.',
'keys' => [
'timeout',
],
],
Expand All @@ -43,18 +35,15 @@ public function getGroups() {

/**
* 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().
*
* @return bool FALSE if no match, TRUE if matches.
*/
public function callback( $key, $val, $line, $group ) {
if ( (int) $val > 3 ) {
return 'Detected high remote request timeout. `%s` is set to `%d`.';
}
return (int) $val > 3;
}
}
14 changes: 2 additions & 12 deletions WordPressVIPMinimum/Sniffs/Performance/WPQueryParamsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,6 @@ public function register() {

/**
* Groups of variables to restrict.
* This should be overridden in extending classes.
*
* Example: groups => array(
* 'groupname' => array(
* 'type' => 'error' | 'warning',
* 'message' => 'Dont use this one please!',
* 'keys' => array( 'key1', 'another_key' ),
* 'callback' => array( 'class', 'method' ), // Optional.
* )
* )
*
* @return array
*/
Expand Down Expand Up @@ -94,8 +84,8 @@ public function process_token( $stackPtr ) {
* @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().
*
* @return bool FALSE if no match, TRUE if matches.
*/
public function callback( $key, $val, $line, $group ) {
return true;
Expand Down

0 comments on commit df070e7

Please sign in to comment.