Fix #12250: False positive Property HelloWorld::$bug (WeakMap<stdClass, stdClass>) does not accept WeakMap<object, mixed>|WeakMap<stdClass, stdClass>.#4958
Open
phpstan-bot wants to merge 2 commits into2.1.xfrom
Conversation
- Added handling for AssignOp\Coalesce in NewAssignedToPropertyVisitor - The visitor now marks `new` expressions in `$this->prop ??= new Foo()` with the property attribute, so MutatingScope::exactInstantiation() can infer generic type parameters from the property's declared type - New regression test in tests/PHPStan/Rules/Properties/data/bug-12250.php Closes phpstan/phpstan#12250
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 a generic class like
WeakMapis instantiated and assigned to a typed property using the null coalesce assignment operator (??=), PHPStan failed to infer the generic type parameters from the property's declared type. This caused a false positive like:Changes
src/Parser/NewAssignedToPropertyVisitor.phpto also handleNode\Expr\AssignOp\Coalescein addition toAssignandAssignReftests/PHPStan/Rules/Properties/data/bug-12250.phptestBug12250()intests/PHPStan/Rules/Properties/TypesAssignedToPropertiesRuleTest.phpRoot cause
NewAssignedToPropertyVisitoris a parser visitor that marksnewexpressions assigned to properties with an attribute, so thatMutatingScope::exactInstantiation()can later infer generic type parameters from the property's declared type. The visitor handledAssign(=) andAssignRef(=&), but notAssignOp\Coalesce(??=). Without the attribute,new WeakMap()in$this->bug ??= new WeakMap()was typed asWeakMap<object, mixed>(the default generic bounds) instead ofWeakMap<stdClass, stdClass>(from the property declaration).Test
The regression test verifies that
$this->bug ??= new WeakMap()produces no errors when the property is typed asWeakMap<stdClass, stdClass>, alongside a regular assignment$this->ok = new WeakMap()that already worked correctly.Fixes phpstan/phpstan#12250
Closes phpstan/phpstan#4525