Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some simplifications of sniffs extending the WPCS AbstractArrayAssignmentRestrictionsSniff #758

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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