Repository navigation
Fix two PreGetPosts false positives in if/else branches - #910
Merged
Merged
Conversation
Outside a conditional, the sniff only flags set() calls. Inside an if without a main query check, though, it flagged any use of the query, so reading a value with get() was reported as modifying the main query. Since #906 the same happened inside an elseif.
A set() in the else of `if ( ! $query->is_main_query() )` was flagged, although only the main query reaches that else. The same applied to later elseif branches. A later branch now counts as checked when an earlier condition in its chain catches every query other than the main one: a negated is_main_query() check, alone or combined with `||`. A positive check does not count, since only secondary queries reach the branches after it. Nor does a negated check combined with `&&`, since other queries can still get through.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This follows on from #906 and fixes two false positives in
PreGetPosts, one of which #906 made worse.Outside a conditional, the sniff only flags
set()calls. Inside anifwithout a main query check, though, it flagged any use of the query, so reading a value was reported as modifying the main query:#906 extended the conditional handling to
elseif, so the same started happening there too. The first commit makes the sniff flag onlyset()calls inside conditionals, as it already did outside them. Every warning the existing tests expect is on aset()call.The second commit deals with an
elseafter a negated check. Only the main query reaches thiselse, but itsset()was flagged:A later branch of an
ifchain, whetherelse,elseiforelse if, with braces or the alternative syntax, now counts as checked when an earlier condition in the chain catches every query other than the main one. That means a negatedis_main_query()check on its own or combined with||. Two cases deliberately still warn. A positive check such asif ( $query->is_main_query() ) { … } else { $query->set(); }does, because only secondary queries reach thatelse, and catching changes to secondary queries is what the sniff is for; the existing tests already expect that warning. A negated check combined with&&does as well, because other queries can still reach the later branches.I ran the old and new sniff over the 21 Automattic plugins I have locally and the results did not change, though this sniff only reported four issues across them.