Do not create conditional expression when holder and guard have the same type and guard overlaps with other branch#5724
Open
phpstan-bot wants to merge 1 commit into
Conversation
…ame type and guard overlaps with other branch When merging if/elseif scopes, createConditionalExpressions could create a spurious CE linking two variables that happened to have the same type in one branch (e.g. $aa = $R['aa'] in an elseif branch). This CE would later fire incorrectly when the guard variable was narrowed, falsely narrowing the holder variable's type too. The fix adds a third skip path: when the expression does not exist in the other scope, the holder type equals the guard type, and the guard type overlaps with the other scope's guard type (isSuperTypeOf is not No), the CE is redundant and should be skipped. Closes phpstan/phpstan#14469
VincentLanglet
approved these changes
May 20, 2026
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.
Summary
Fixes phpstan/phpstan#14469
When merging if/elseif scopes,
createConditionalExpressions()could create a spurious conditional expression (CE) linking two variables that happened to have the same type in one branch. For example, in:In the
elseifbranch, both$aaand$R['aa']have typemixed~falsy. The merge created a CE saying "when$aaismixed~falsy,$R['aa']ismixed~falsy". This CE later fired whenif ($aa)narrowed$aa, incorrectly narrowing$R['aa']to truthy — making!$R['aa']a false positive "always false".The fix adds a third skip condition in the inner loop of
createConditionalExpressions(): when the expression does not exist in the other scope, the holder type equals the guard type, and the guard type overlaps with the other scope's guard type (isSuperTypeOfreturns Maybe), the CE is skipped as unreliable. This correctly identifies cases where two variables were correlated only within a single branch, not causally linked.Test plan
testBug14469covering the original report and analogous patterns (if-constant-condition, falsey branch, with-else, multiple-elseif)$R['aa']retains typemixedinsideif ($aa)bug-14411-regression,bug-1306, fullBooleanNotConstantConditionRuleTestmake phpstan: no errorsmake cs-fix: no violations