Skip to content

Commit

Permalink
WP/PostsPerPage: simplification
Browse files Browse the repository at this point in the history
The WPCS `AbstractArrayAssignmentRestrictionsSniff` expects an array from `getGroups()` which includes a `'message'` key.
The `callback()` method subsequently allows to override that message with a higher priority message if needed in specific circumstances.

This sniff, however, doesn't need to override the message, so the `callback()` method can just use the default behaviour of returning `true` or `false` and use the `'message'` as declared in the group.
  • Loading branch information
jrfnl committed Jun 25, 2023
1 parent e9cfeab commit 55315ad
Showing 1 changed file with 6 additions and 11 deletions.
17 changes: 6 additions & 11 deletions WordPress/Sniffs/WP/PostsPerPageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ final class PostsPerPageSniff extends AbstractArrayAssignmentRestrictionsSniff {
public function getGroups() {
return array(
'posts_per_page' => array(
'type' => 'warning',
'keys' => array(
'type' => 'warning',
'message' => 'Detected high pagination limit, `%s` is set to `%s`',
'keys' => array(
'posts_per_page',
'numberposts',
),
Expand All @@ -62,16 +63,10 @@ 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 ) {
$this->posts_per_page = (int) $this->posts_per_page;

if ( $val > $this->posts_per_page ) {
return 'Detected high pagination limit, `%s` is set to `%s`';
}

return false;
return ( $val > (int) $this->posts_per_page );
}
}

0 comments on commit 55315ad

Please sign in to comment.