Fix phpstan/phpstan#14206: Incorrect @var PDOStatement<int,string> contains unresolvable type#5128
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Conversation
…reported as unresolvable - When TypeNodeResolver creates an intersection of a non-generic iterable class with user-provided type arguments (e.g. PDOStatement<int,string>), TypeCombinator::intersect may collapse the result to NeverType if the iterable types are incompatible - This was triggered by the more precise getIterator() return type in the PDOStatement stub (Iterator<mixed, array<int|string, mixed>> instead of plain Iterator) - The fix preserves the IntersectionType directly when the intersection would otherwise collapse to NeverType, maintaining @var override semantics - New regression test in tests/PHPStan/Rules/PhpDoc/data/bug-14206.php
Contributor
VincentLanglet
left a comment
There was a problem hiding this comment.
I don't like this fix, I'll explain on the issue my thought.
Contributor
|
Closed in favor of #5132 |
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 using
@var PDOStatement<int,string>to override a PDOStatement's iteration types (e.g. when usingPDO::FETCH_COLUMN), PHPStan 2.1.39+ incorrectly reports "PHPDoc tag @var contains unresolvable type" (varTag.unresolvableType). This was a regression introduced in 2.1.39.Changes
src/PhpDoc/TypeNodeResolver.php: InresolveGenericTypeNode(), when a non-generic iterable class (like PDOStatement) is parameterized with type arguments andTypeCombinator::intersectcollapses the result toNeverType, the code now falls back to creating theIntersectionTypedirectly instead of returning the collapsedNeverType.tests/PHPStan/Rules/PhpDoc/data/bug-14206.phpandtests/PHPStan/Rules/PhpDoc/InvalidPhpDocVarTagTypeRuleTest.php.Root cause
The PDOStatement stub's
getIterator()return type was changed fromIteratortoIterator<mixed, array<int|string, mixed>>in commit 97288da. This madePDOStatement->getIterableValueType()returnarray<int|string, mixed>instead ofTemplateMixedType(implicit mixed from the unparameterized Iterator).When users write
@var PDOStatement<int,string>, theTypeNodeResolverinterprets this asPDOStatement & iterable<int, string>viaTypeCombinator::intersect. With the more precise iterable value type,IterableType(int, string)->isSuperTypeOfMixed(PDOStatement)now returnsNo(becauseStringType->isSuperTypeOf(ArrayType)isNo), causing the intersection to collapse toNeverType. TheUnresolvableTypeHelperthen detects this non-explicitNeverTypeand reports the "unresolvable type" error.The fix preserves the
IntersectionTypewhenTypeCombinator::intersectwould otherwise produceNeverType, maintaining the@varoverride semantics that users rely on.Test
Added
testBug14206inInvalidPhpDocVarTagTypeRuleTestwith a test file that uses@var PDOStatement<int,string>and expects no errors.Fixes phpstan/phpstan#14206