Fix phpstan/phpstan#13247: iterable is equal to array|Traversable#5143
Closed
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Closed
Fix phpstan/phpstan#13247: iterable is equal to array|Traversable#5143phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
iterable is equal to array|Traversable#5143phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Decompose IterableType to array|Traversable union in UnionType::accepts() so that iterable<K,V> is properly accepted where array<K,V>|Traversable<K,V> is expected - Decompose IterableType in UnionType::inferTemplateTypes() so template type parameters are correctly resolved across the iterable equivalence - Updated phpstan-baseline.neon to account for new instanceof IterableType usages - New regression test in tests/PHPStan/Rules/Functions/data/bug-13247.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
iterable<K, V>is semantically equivalent toarray<K, V>|Traversable<K, V>, but PHPStan was not acceptingiterable<K, V>as an argument wherearray<K, V>|Traversable<K, V>was expected. This caused false positiveargument.typeerrors and failed template type resolution (argument.templateTypeerrors).Changes
src/Type/UnionType.php: Added iterable-to-union decomposition in two methods:accepts(): ConvertsIterableType<K,V>toUnionType([ArrayType<K,V>, GenericObjectType(Traversable, [K,V])])before checking acceptance, so thatiterable<K,V>is properly accepted byarray<K,V>|Traversable<K,V>inferTemplateTypes(): Same decomposition so template type parameters (K, V) are correctly inferred from an iterable argument when the parameter type is a union of array and Traversablephpstan-baseline.neon: Updatedinstanceof IterableTypeignore count from 1 to 3 forUnionType.phptests/PHPStan/Rules/Functions/CallToFunctionParametersRuleTest.php: AddedtestBug13247test methodtests/PHPStan/Rules/Functions/data/bug-13247.php: Regression test data fileRoot cause
UnionType::inferTemplateTypes()iterates through each union member and callsinferTemplateTypes()on them. When the received type wasIterableType, neitherArrayType::inferTemplateTypes()(which requiresisArray()->yes()) norGenericObjectType::inferTemplateTypes()could extract template information from it, resulting in empty template maps. Similarly,UnionType::accepts()checked each union member individually, none of which could fully accept the iterable type, and the fallback viaIterableType::isAcceptedBy()→isSubTypeOf()decomposed to a form that didn't match properly against generic Traversable types.The fix decomposes
IterableType<K,V>into its equivalentarray<K,V>|Traversable<K,V>union at the start of both methods, allowing the existing union-handling logic to process it correctly.Test
Added a regression test that defines a function accepting
array<K,V>|Traversable<K,V>and another function that passesiterable<K,V>to it. The test expects no errors, confirming that the iterable type is correctly accepted and templates are properly resolved.Fixes phpstan/phpstan#13247