Fix phpstan/phpstan#14413: Narrowing generic union via ::class comparison discards type parameters#5370
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Conversation
…owing via ::class comparison - Added determineExactClassType() helper in TypeSpecifier that intersects the current scope type with a plain ObjectType to extract generic parameters - When generics are found (narrowed type is more specific than plain ObjectType), uses the narrowed type; otherwise falls back to ObjectType with asFinal() - Updated all 3 ::class/get_class narrowing sites to use the new helper - New regression test in tests/PHPStan/Analyser/nsrt/bug-14413.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
When narrowing a generic union type like
Cat<string>|Dog<string>via$a::class === Cat::class, PHPStan was discarding the generic type parameters. The narrowed type becameCat&Cat<string>(a redundant intersection) instead ofCat<string>.Changes
src/Analyser/TypeSpecifier.php:determineExactClassType()helper method that intersects the expression's current scope type with a plainObjectTypeto extract generic parameters, then returns the narrowed type if it's more specific, or falls back toObjectTypewithasFinal()get_class($a) === 'Foo',$a::class === 'Foo', and'Foo' === $a::classto use the new helpertests/PHPStan/Analyser/nsrt/bug-14413.phpregression testRoot cause
Commit 772f297 introduced
asFinal()on theObjectTypecreated for::class/get_class()narrowing to prevent subclass matches. However,ObjectType('Cat', asFinal).isSuperTypeOf(GenericObjectType('Cat', [StringType]))returnsMaybe(becausehasFinalByKeywordOverrideis true on ObjectType but false on GenericObjectType). This preventedTypeCombinator::intersectfrom simplifying the intersection, leaving bothCatandCat<string>in the result asCat&Cat<string>.The fix pre-narrows the expression type by intersecting with a plain
ObjectType(which correctly simplifies generics), then checks if the result is more specific. If it has generic parameters, it uses the narrowed type directly. For non-generic cases, it falls back to theasFinal()behavior.Test
Added
tests/PHPStan/Analyser/nsrt/bug-14413.phpcovering:matchexpression with$a::classpreserving generic parametersif/elsewith$a::class ===preserving generic parametersCat::class === $a::classmatchwith method call using generic return type*NEVER*Fixes phpstan/phpstan#14413