Fix phpstan/phpstan#9519: Moving (instanceof A || instanceof B) from if statement causes error#5372
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Conversation
- Changed MutatingScope::addConditionalExpressions to merge instead of replace, so multiple instanceof assignments on the same target don't overwrite each other's conditional expression holders - Added resolveConditionalExpressions in TypeSpecifier to resolve scope conditional expressions during BooleanOr processing, enabling type narrowing to flow through variable-stored instanceof results - Added getConditionalExpressions() public getter to MutatingScope - Updated test expectations in multi-assign.php and bug-7716.php to reflect the now-correct transitive type narrowing - New regression test in tests/PHPStan/Analyser/nsrt/bug-9519.php Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
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
When
instanceofresults were stored in variables and then used inifconditions (especially OR conditions), PHPStan failed to narrow the target variable's type. For example:This worked correctly when
instanceofwas used inline:if ($obj instanceof ClassA || $obj instanceof ClassB).Changes
src/Analyser/MutatingScope.php: ChangedaddConditionalExpressions()to merge new conditional expression holders with existing ones instead of replacing them. This was the root cause of the secondinstanceofassignment overwriting the first's type narrowing information. Also added a publicgetConditionalExpressions()getter.src/Analyser/TypeSpecifier.php: AddedresolveConditionalExpressions()method that resolves a scope's conditional expressions for a given set of specified types. Used in the BooleanOr handler to augment each branch's specified types with resolved conditional expressions before intersecting, enabling proper type narrowing through variable-stored conditions.tests/PHPStan/Analyser/nsrt/bug-9519.php: New regression test covering the reported scenarios.tests/PHPStan/Analyser/nsrt/multi-assign.php: Updated expectations to reflect now-correct transitive type narrowing through chained assignments ($foo = $bar = $baz = $b).tests/PHPStan/Analyser/nsrt/bug-7716.php: Updated expectations to reflect now-correct narrowing of optional array keys when theirisset()results are stored in variables.Root cause
Two issues combined to cause this bug:
addConditionalExpressionsused replacement instead of merge: When processing$isB = $obj instanceof ClassBafter$isA = $obj instanceof ClassA, the conditional expression holders for$objfrom the first assignment were completely replaced by the second's. This meant only the last instanceof assignment's type narrowing was preserved.BooleanOr handler didn't resolve conditional expressions: Even with the merge fix,
$isA || $isBin truthy context produced empty sure types (becauseintersectWithonly keeps expressions present in both branches, and$isA/$isBare different expression keys). The scope's conditional expressions mapping$isA→$obj:ClassAand$isB→$obj:ClassBwere never resolved during the BooleanOr processing. The newresolveConditionalExpressions()method resolves these before the intersection, so$obj:ClassAfrom the left branch and$obj:ClassBfrom the right branch properly intersect to produce$obj:ClassA|ClassB.Test
The regression test (
tests/PHPStan/Analyser/nsrt/bug-9519.php) covers:instanceofresults stored in variables used in OR conditions (test1)instanceofin OR conditions as control (test2)instanceofchecks (test3)instanceofwith template types where second assignment doesn't clobber first (test4,test5)Fixes phpstan/phpstan#9519