Preserve conditional expressions in invalidateExpression when requireMoreCharacters is true#5560
Merged
ondrejmirtes merged 1 commit intophpstan:2.1.xfrom Apr 28, 2026
Conversation
…ireMoreCharacters` is true - In `MutatingScope::invalidateExpression()`, the target expression check for conditional expressions was hardcoded to `requireMoreCharacters=false`, causing conditional expressions to be removed even when the expression type itself was preserved (e.g. after a method call on the variable) - Pass the caller's `$requireMoreCharacters` flag to the `shouldInvalidateExpression` check for conditional expression targets, consistent with the expression type check - This fixes stored `instanceof` results losing their type narrowing after the first use when the if-block body contains impure calls (method calls, var_dump, etc.) - The bug only manifested for concrete class types (e.g. `ObjectClass`) where `createConditionalExpressions` could not reconstruct the conditional from type differences, unlike abstract `object` types where it could
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 an
instanceofcheck result is stored in a variable (e.g.$is_interface = $obj instanceof SomeInterface) and used in multiple consecutiveifblocks, the type narrowing was lost after the first check when the if-block body contained impure calls (method calls,var_dump, etc.). This happened becauseinvalidateExpression()incorrectly removed the conditional expressions that encode the stored type guard.Changes
src/Analyser/MutatingScope.phpline 2872: changed therequireMoreCharactersparameter from hardcodedfalseto$requireMoreCharacterswhen checking whether to invalidate conditional expression targetstests/PHPStan/Analyser/nsrt/bug-14545.phpcovering:instanceofwith generic return type + method call in bodyinstanceofwith generic return type +var_dump()in bodyinstanceofwith a different concrete classinstanceofwith abstractobjectreturn type (already worked)is_array()result with impure call in bodyRoot cause
In
MutatingScope::invalidateExpression(), when a method call like$obj->test()triggers invalidation withrequireMoreCharacters=true, the expression type for$objis correctly preserved (becauseshouldInvalidateExpressionreturnsfalsefor exact matches whenrequireMoreCharacters=true). However, the conditional expression check for$objusedfalseinstead of$requireMoreCharacters, causing it to remove the conditional expressions that link$is_interfaceto$obj's narrowed type.After the conditional was removed from the truthy branch scope,
intersectConditionalExpressions()during the if-block merge would find no matching conditional in the other scope, so the conditional was dropped entirely. For abstractobjecttypes,createConditionalExpressions()could reconstruct the conditional from type differences (becauseobject~SomeInterface≠object), masking the bug. For concrete types likeObjectClass,ObjectClassminusSomeInterfaceis stillObjectClass, so reconstruction was impossible.Analogous cases probed
false, but this is correct — guard expressions that reference sub-expressions of the invalidated variable (e.g.$obj->prop) should be invalidated because the property value may have changedis_array()results: Same mechanism, tested and works with the fixinstanceofwith different concrete classes: Tested, worksobjectreturn type: Already worked before the fix viacreateConditionalExpressionsreconstructionTest
Regression test at
tests/PHPStan/Analyser/nsrt/bug-14545.phpwith 6 test functions covering the reported bug and analogous cases. All tests pass with the fix and the failing ones fail without it.Fixes phpstan/phpstan#14545