Skip to content
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
33 changes: 30 additions & 3 deletions WordPressVIPMinimum/Sniffs/Hooks/PreGetPostsSniff.php
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,36 @@ private function isEarlyMainQueryCheck( $stackPtr ) {
return false;
}

$nestedParenthesisEnd = array_shift( $this->tokens[ $stackPtr ]['nested_parenthesis'] );
if ( true === in_array( 'PHPCS_T_CLOSURE', $this->tokens[ $stackPtr ]['conditions'], true ) ) {
$nestedParenthesisEnd = array_shift( $this->tokens[ $stackPtr ]['nested_parenthesis'] );
$parentheses = $this->tokens[ $stackPtr ]['nested_parenthesis'];
do {
$nestedParenthesisEnd = array_shift( $parentheses );
if ( null === $nestedParenthesisEnd ) {
// Nothing left in the array. No parenthesis found with a non-closure owner.
return false;
}

if ( isset( $this->tokens[ $nestedParenthesisEnd ]['parenthesis_owner'] )
&& T_CLOSURE !== $this->tokens[ $this->tokens[ $nestedParenthesisEnd ]['parenthesis_owner'] ]['code']
) {
break;
}
} while ( true );

$owner = $this->tokens[ $nestedParenthesisEnd ]['parenthesis_owner'];
if ( isset( $this->tokens[ $owner ]['scope_opener'], $this->tokens[ $owner ]['scope_closer'] ) === false ) {
// This may be an inline control structure (no braces).
$next = $this->phpcsFile->findNext(
Tokens::$emptyTokens,
( $nestedParenthesisEnd + 1 ),
null,
true
);

if ( false !== $next && T_RETURN === $this->tokens[ $next ]['code'] ) {
return true;
}

return false;
}

$next = $this->phpcsFile->findNext(
Expand Down
26 changes: 26 additions & 0 deletions WordPressVIPMinimum/Tests/Hooks/PreGetPostsUnitTest.inc
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,29 @@ add_action( 'pre_get_posts', function( $wp_query ) {
}

} );

class undefined_index_issue_499 {

public function __construct() {
add_action( 'pre_get_posts', array( $this, 'pre_get_posts_499' ) );
}

public function pre_get_posts_499( $wp_query ) {

if ( function() { return ( $wp_query->is_main_query() === false ) }() === false ) {
return;
}

$wp_query->set( 'cat', '-5' );
}
}

add_action('pre_get_posts', 'inline_control_structures', 10, 1);

function inline_control_structures( $query ) {
if( !$query->is_main_query() && !is_front_page()) return;
if(is_single() || is_search() || is_archive()) return;

$query->set('meta_query', 'foo');
return $query;
}