Fix phpstan/phpstan#11844: Invalid property.unusedType report for nullable WeakMap property (broken since 1.12.4)#5316
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
…eric property assigned inside null check - Used declared property type from reflection instead of scope-narrowed type in NewHandler - When `new WeakMap()` was assigned inside `if ($this->map === null)`, scope returned narrowed `null` type - TypeCombinator::removeNull on `null` yielded `never`, making TooWideTypeCheck think WeakMap was never assigned - New regression test in tests/PHPStan/Rules/TooWideTypehints/data/bug-11844.php
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 a false positive
property.unusedTypeerror when a nullable generic property (e.g.,?WeakMap) is assignednew WeakMap()inside a null check likeif ($this->map === null). This regression was introduced in 1.12.4 by PR #3364.Changes
src/Analyser/ExprHandler/NewHandler.phpto resolve the declared property type from reflection instead of using the scope-narrowed typeresolveAssignedPropertyDeclaredType()helper method that handles both instance and static property fetchestests/PHPStan/Rules/TooWideTypehints/data/bug-11844.phpcovering static properties, instance properties, and direct assignmentRoot cause
When
new WeakMap()is assigned to a property inside a null check (if ($this->map === null)), the scope has already narrowed the property type tonull. TheNewHandler::exactInstantiation()method used$scope->getType($assignedToProperty)to infer generic types from the property, but this returned the narrowed type (null) instead of the declared type (WeakMap<object, string>|null). AfterTypeCombinator::removeNull(), this becamenever, causingTooWideTypeCheckto incorrectly conclude thatWeakMap<object, string>was never assigned.The fix resolves the property reflection directly and uses
getReadableType()to get the declared type, which is unaffected by scope narrowing.Test
Added
tests/PHPStan/Rules/TooWideTypehints/data/bug-11844.phpwith three test cases:WeakMapproperty assigned inside null check (original report)WeakMapproperty assigned inside null checkAll three should produce zero
property.unusedTypeerrors.Fixes phpstan/phpstan#11844