Fix #4173: Consequest scope with the same condition#5056
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix #4173: Consequest scope with the same condition#5056phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
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 PR fixes phpstan/phpstan#4173
When merging scopes from three or more branches (e.g., if/elseif/elseif/else),
MutatingScope::mergeWith()performs pairwise merges. During each merge,intersectConditionalExpressions()preserves only conditional expressions that exist in both scopes. However, a conditional expression whose guard condition is impossible in the other scope is vacuously true there and should be preserved.For example, with branches
count($row) === 3(defines$value),count($row) === 2(no$value), andcount($row) === 1(no$value):intersectConditionalExpressions()discards this conditional expression because branch 3 doesn't have itcount($row)is1, so the guardcount($row)=3can never be satisfied — the conditional expression is vacuously truecount($row) === 1is eliminated by an early return, the conditional expression is needed to know that$valueis defined whencount($row)is 2 or 3The fix adds
preserveVacuousConditionalExpressions()which recovers conditional expressions from either scope whose guard conditions are provably impossible in the other scope (usingisSuperTypeOf()->no()).Additionally fixes two pre-existing issues in PHPStan's own code that are now detected due to improved analysis precision:
$parametersAcceptor !== nullcheck inNodeScopeResolver(always true within$methodReflection !== nullblock)$operand->getMax()in a local variable inInitializerExprTypeResolverto allow proper null-narrowing