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

Fix missing check for infinite posts_per_page #1531

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion WordPress/Sniffs/WP/PostsPerPageSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ public function getGroups() {
public function callback( $key, $val, $line, $group ) {
$this->posts_per_page = (int) $this->posts_per_page;

if ( $val > $this->posts_per_page ) {
if ( $val > $this->posts_per_page || '-1' === $val ) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is checking a value that is a string, not a negative integer.

Copy link
Author

@LC43 LC43 Nov 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi, i checked it, and $val is always converted to string. not sure why or where, didn't look into it deeply. see below.

that's why the tests pass, either with -1 and with '-1'

Copy link
Author

@LC43 LC43 Nov 11, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the token is parsed into a string here:

// this->tokens
23: - // "PHPCS_T_MINUS"
24: 1 // "T_LNUMBER"
25: , // "PHPCS_T_COMMA"
// call: WordPress/AbstractArrayAssignmentRestrictionsSniff.php L170
// valStart: 23
// valEnd: 25
$val            = $this->phpcsFile->getTokensAsString( $valStart, ( $valEnd - $valStart ) );

return 'Detected high pagination limit, `%s` is set to `%s`';
}

Expand Down
5 changes: 3 additions & 2 deletions WordPress/Tests/WP/PostsPerPageUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

$args = array(
'posts_per_page' => 999, // Bad.
'posts_per_page' => -1, // OK.
'posts_per_page' => -1, // Bad.
'posts_per_page' => 1, // OK.
'posts_per_page' => '1', // OK.
);
Expand All @@ -13,7 +13,8 @@ _query_posts( 'numberposts=999' ); // Bad.
$query_args['posts_per_page'] = 999; // Bad.
$query_args['posts_per_page'] = 1; // OK.
$query_args['posts_per_page'] = '1'; // OK.
$query_args['numberposts'] = '-1'; // OK.
$query_args['posts_per_page'] = '-1'; // Bad.
$query_args['numberposts'] = '-1'; // Bad.

$query_args['my_posts_per_page'] = -1; // OK.

Expand Down
7 changes: 5 additions & 2 deletions WordPress/Tests/WP/PostsPerPageUnitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ public function getErrorList() {
public function getWarningList() {
return array(
4 => 1,
5 => 1,
10 => 1,
11 => 1,
13 => 1,
22 => 1,
16 => 1,
17 => 1,
23 => 1,
24 => 1,
29 => 1,
25 => 1,
30 => 1,
);
}

Expand Down