Fix phpstan/phpstan#3128: array ignored in @param array|stdClass[] $foo#5187
Open
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Open
Fix phpstan/phpstan#3128: array ignored in @param array|stdClass[] $foo#5187phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
phpstan-bot wants to merge 1 commit intophpstan:2.1.xfrom
Conversation
- Changed TypeNodeResolver::resolveUnionTypeNode() to use a two-pass approach for merging [] shorthand types with other iterables in a union - Pass 1 merges non-array iterables (ObjectType, IterableType) with the [] type - Pass 2 merges ArrayType/ConstantArrayType only if a non-array iterable was also merged, preserving existing patterns like Foo[]|Collection|array - Without a non-array iterable, array|Foo[] correctly becomes array|array<Foo> which simplifies to array via TypeCombinator - New regression test in tests/PHPStan/Rules/Methods/data/bug-3128.php - Updated MissingFunctionParameterTypehintRuleTest for array|int[] now resolving to plain array Closes phpstan/phpstan#3128
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 writing
@param array|stdClass[] $foo, PHPStan incorrectly parsed this asarray<stdClass>instead ofarray|array<stdClass>. This caused false positives when passing a plain array (e.g.,[1]) to such a parameter, sincearrayshould accept any array.Changes
TypeNodeResolver::resolveUnionTypeNode()insrc/PhpDoc/TypeNodeResolver.phpto use a two-pass approach for merging[]shorthand types with other iterablesCollection, IterableType) — these get their value type annotated by the[]notation as beforeFoo[]|Collection|array→array<Foo>|(Collection & iterable<Foo>)tests/PHPStan/Rules/Functions/MissingFunctionParameterTypehintRuleTest.phpforarray|int[]now correctly resolving toarraytests/PHPStan/Rules/Methods/data/bug-3128.phpRoot cause
TypeNodeResolver::resolveUnionTypeNode()had a single-pass loop that mergedFoo[]value types into all iterable types in the union, including plainarray. Whenarray(withMixedTypevalue type) was in a union withstdClass[], the merge replacedarraywitharray<stdClass>, incorrectly narrowing the type. The fix introduces a two-pass approach: first merge with non-array iterables, then only merge with array types if a non-array merge occurred — indicating the[]notation is annotating a collection pattern rather than being a standalone array type.Test
Added
tests/PHPStan/Rules/Methods/data/bug-3128.phptesting both@param array|stdClass[] $fooand@param stdClass[]|array $barwith a plain array argument[1]. Both should produce no errors at level 5.Fixes phpstan/phpstan#3128