Skip to content

Commit

Permalink
WP/PostsPerPage: bug fix - allow for explicitly positive numbers
Browse files Browse the repository at this point in the history
This prevents false negatives when the number given is explicitly positive.

Includes tests.
  • Loading branch information
jrfnl committed Jul 4, 2023
1 parent 0d37c0c commit 0ececb6
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 1 deletion.
2 changes: 1 addition & 1 deletion WordPress/Sniffs/WP/PostsPerPageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ public function getGroups() {
*/
public function callback( $key, $val, $line, $group ) {
$val = TextStrings::stripQuotes( $val );
if ( preg_match( '`^[-]?[0-9]+$`', $val ) !== 1 ) {
if ( preg_match( '`^[+-]?[0-9]+$`', $val ) !== 1 ) {
// Not a purely numeric value, so any comparison would be a false comparison.
return false;
}
Expand Down
6 changes: 6 additions & 0 deletions WordPress/Tests/WP/PostsPerPageUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -102,3 +102,9 @@ $query_args['posts_per_page'] = '1e3'; // Should be ignored as undetermined. Wou
$query_args['posts_per_page'] = '50'; // OK.
$query_args['posts_per_page'] = '200'; // Bad.
$query_args['posts_per_page'] = "200"; // Bad.

// Verify handling of explicitly positive numbers.
$args = array(
'posts_per_page' => +50, // OK.
'posts_per_page' => +200, // Bad.
);
1 change: 1 addition & 0 deletions WordPress/Tests/WP/PostsPerPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function getWarningList() {
82 => 1,
103 => 1,
104 => 1,
109 => 1,
);
}
}

0 comments on commit 0ececb6

Please sign in to comment.