Fix phpstan/phpstan#14413: Narrowing generic union via ::class comparison discards type parameters#5366
Open
mhert wants to merge 1 commit intophpstan:2.1.xfrom
Open
Conversation
… ::class comparison Narrowing a generic union like Cat<string>|Dog<string> via $a::class === Cat::class discarded the generic type parameters — the type became plain Cat instead of Cat<string>. This happened because resolveNormalizedIdentical() constructed a plain ObjectType without consulting the variable's known type for generic information. Extracts the duplicated $a::class === 'Foo' / 'Foo' === $a::class handling into a single resolveClassStringComparison() helper and adds generic-aware narrowing: it first tries TypeCombinator::intersect() with the current variable type (which preserves generics from unions), and falls back to template inference via getAncestorWithClassName() + inferTemplateTypes() for single generic parents.
Member
|
Please open an issue with a reproducing code example first. |
Collaborator
|
This pull request has been marked as ready for review. |
Contributor
Author
|
Friendly ping for review when you get a chance 😊 |
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
Narrowing a generic union like
Cat<string>|Dog<string>via$a::class === Cat::classdiscarded the generic type parameters — the inferred type became plainCatinstead ofCat<string>. This caused downstream type errors when accessing generic methods on the narrowed variable.Motivation
When using
::classcomparisons to narrow generic union types, PHPStan'sTypeSpecifier::resolveNormalizedIdentical()constructed a plainObjectTypewithout consulting the variable's known type for generic information. This meant that code like:lost all generic type information after narrowing.
Changes
src/Analyser/TypeSpecifier.php— Extracted the duplicated$a::class === 'Foo'/'Foo' === $a::classhandling into a singleresolveClassStringComparison()helper. Added generic-aware narrowing that infers template type parameters from the variable's current type viagetAncestorWithClassName()+inferTemplateTypes().Test Cases
tests/PHPStan/Analyser/nsrt/class-string-generic-narrowing.php— Multiple scenarios:Cat<string>|Dog<string>→Cat<string>in match arm)::classcomparison preserves genericsCat::class === $a::class) preserves generics*NEVER*Fixes phpstan/phpstan#14413
Co-Authored-By: Claude Code