Fix callable array intersection type offset narrowing#5548
Merged
VincentLanglet merged 5 commits intophpstan:2.1.xfrom Apr 28, 2026
Merged
Fix callable array intersection type offset narrowing#5548VincentLanglet merged 5 commits intophpstan:2.1.xfrom
VincentLanglet merged 5 commits intophpstan:2.1.xfrom
Conversation
Comment on lines
+943
to
+945
| if ((new ConstantIntegerType(0))->isSuperTypeOf($arrayKeyOffsetType)->yes()) { | ||
| $result = TypeCombinator::intersect($result, TypeCombinator::union(new ClassStringType(), new ObjectWithoutClassType())); | ||
| } elseif ((new ConstantIntegerType(1))->isSuperTypeOf($arrayKeyOffsetType)->yes()) { |
Contributor
There was a problem hiding this comment.
What if I access to the offset 0|1 ?
This could be narrowed to object|string.
VincentLanglet
approved these changes
Apr 27, 2026
VincentLanglet
approved these changes
Apr 27, 2026
…rsections
When is_array() narrows a callable parameter, the resulting
array&callable intersection type now correctly reports that offset 0
is class-string|object and offset 1 is string. This fixes false
positives when passing the narrowed value to functions expecting
array{string|object, string} or similar callable-array shapes.
Address review feedback: use a single UnionType(0|1)->isSuperTypeOf check instead of separate ConstantIntegerType checks, and handle the case where the offset is itself a union (e.g. 0|1) by narrowing to the union of both offset types (object|string). Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
…ngType|ObjectWithoutClassType Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
Use direct UnionType construction instead of building an array and calling TypeCombinator::union, as suggested in review. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
The rule test now references the nsrt test file directly instead of maintaining a separate duplicate data file. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
f7f94c6 to
35a6959
Compare
staabm
reviewed
Apr 28, 2026
staabm
approved these changes
Apr 28, 2026
Contributor
staabm
left a comment
There was a problem hiding this comment.
lgtm
btw: this PR fixes error on line 19, but not 27 of https://phpstan.org/r/2137cc31-c330-43b4-93f3-574dd2507b56
Contributor
Which is expected to me, cause the line 27 produce |
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
Fixes phpstan/phpstan#3842
When
is_array()narrows acallableparameter, the resulting type isarray<mixed, mixed>&callable(): mixed. Previously, accessing offsets on this intersection type returnedmixedfor all offsets, causing false positives when passing the value to functions expectingarray{string|object, string}orarray{class-string|object, string}.This PR teaches
IntersectionTypethat when an intersection is both callable and an array (i.e., a callable array), offsets 0 and 1 are guaranteed to exist with specific types:class-string|object(the class or object the method belongs to)string(the method name)Changes
src/Type/IntersectionType.php: EnhancedhasOffsetValueType()to returnYesfor offsets 0 and 1 on callable array intersections. EnhancedgetOffsetValueType()to narrow the result type for those offsets.Test plan
$value[0]isclass-string|objectand$value[1]isstringafteris_array()narrowing on acallablecallable-arrayPHPDoc typearray{string|object, string}parameter