Fix phpstan/phpstan#8985: Expression result remembered on new()#5449
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#8985: Expression result remembered on new()#5449phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Added expressionHasNewInChain() check in MutatingScope::resolveType() to skip stored expression type lookup when the expression's receiver chain contains a New_ node - New regression test in tests/PHPStan/Analyser/nsrt/bug-8985.php - The root cause was that (new Foo())->method() produced the same expression key regardless of source location, so type narrowing from assert() on one new instance incorrectly applied to subsequent ones
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
assert((new Repository())->getAll() === [])narrowed the type of the method call result, the narrowed typearray{}was incorrectly remembered for subsequent(new Repository())->getAll()calls, even though eachnew Repository()creates a fresh object. This caused a false positive "Offset 0 does not exist on array{}".Changes
expressionHasNewInChain()helper method insrc/Analyser/MutatingScope.phpthat checks whether an expression's receiver chain contains aNew_noderesolveType()insrc/Analyser/MutatingScope.phpto skip the stored expression type lookup when the expression hasnewin its receiver chainnewexpression -- it does NOT skip the lookup fornew Foo()itself, preserving@varannotation support onnewexpressionsRoot cause
PHPStan uses expression string keys (like
(new \Foo())->getAll()) to store and retrieve narrowed types inMutatingScope::expressionTypes. Whenassert()narrows a type, it stores the narrowed type under this key. The problem is that two differentnew Foo()AST nodes at different source locations produce the same string key, so the narrowed type from oneassertincorrectly applies to a completely differentnewinstantiation.The fix skips the expression type lookup for expressions whose receiver chain contains
new, since eachnewcreates a fresh object and narrowed types from one instance should not apply to another.Test
Added
tests/PHPStan/Analyser/nsrt/bug-8985.php- an NSRT test that verifies(new Repository())->getAll()returnsarray<int, Entity>even after a priorassert((new Repository())->getAll() === []).Fixes phpstan/phpstan#8985