-
Notifications
You must be signed in to change notification settings - Fork 569
Fix ReflectionGetAttributesMethodReturnTypeExtension for UnionTypes #5463
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
staabm
merged 11 commits into
phpstan:2.1.x
from
phpstan-bot:create-pull-request/patch-7l9f0pj
Apr 17, 2026
+101
−4
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
4a7c911
Use per-class method reflection when resolving dynamic return type ex…
phpstan-bot b95b996
Iterate over union inner types instead of constructing new ObjectType…
phpstan-bot b77c951
Fix declaring class check in ReflectionGetAttributesMethodReturnTypeE…
phpstan-bot 68abeeb
Skip extension for non-native reflection classes instead of removing …
phpstan-bot d039f90
Resolve ReflectionAttribute class from method return type instead of …
phpstan-bot 7dcf3a9
Handle multiple ReflectionAttribute subclasses in return type resolution
phpstan-bot dde7229
Return union of GenericObjectType when multiple ReflectionAttribute s…
phpstan-bot 1b42b47
Rename resolveReflectionAttributeClassNames to resolveReflectionAttri…
phpstan-bot a0c7ed7
Use early exit in resolveReflectionAttributeType to reduce nesting
phpstan-bot 2066040
Use ParametersAcceptorSelector instead of hardcoded getVariants()[0]
phpstan-bot 2069466
Update bug-14484.php
staabm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| <?php // lint >= 8.0 | ||
|
|
||
| declare(strict_types = 1); | ||
|
|
||
| namespace Bug14466; | ||
|
|
||
| use ReflectionAttribute; | ||
| use ReflectionClass; | ||
| use ReflectionMethod; | ||
| use function PHPStan\Testing\assertType; | ||
|
|
||
| interface I | ||
| { | ||
|
|
||
| } | ||
|
|
||
| class Bug | ||
| { | ||
| /** | ||
| * @param ReflectionClass<*> $object | ||
| */ | ||
| protected function c(ReflectionClass $object): void | ||
| { | ||
| $requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF); | ||
|
|
||
| assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements); | ||
| } | ||
|
|
||
| /** | ||
| * @param ReflectionMethod $object | ||
| */ | ||
| protected function m(ReflectionMethod $object): void | ||
| { | ||
| $requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF); | ||
|
|
||
| assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements); | ||
| } | ||
|
|
||
| /** | ||
| * @param ReflectionClass<*>|ReflectionMethod $object | ||
| */ | ||
| protected function classOrMethod(ReflectionClass|ReflectionMethod $object): void | ||
| { | ||
| $requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF); | ||
|
|
||
| assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements); | ||
| } | ||
|
|
||
| /** | ||
| * @param ReflectionClass<*>|\ReflectionProperty $object | ||
| */ | ||
| protected function classOrProperty(ReflectionClass|\ReflectionProperty $object): void | ||
| { | ||
| $requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF); | ||
|
|
||
| assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements); | ||
| } | ||
|
|
||
| /** | ||
| * @param ReflectionMethod|\ReflectionProperty $object | ||
| */ | ||
| protected function methodOrProperty(ReflectionMethod|\ReflectionProperty $object): void | ||
| { | ||
| $requirements = $object->getAttributes(I::class, ReflectionAttribute::IS_INSTANCEOF); | ||
|
|
||
| assertType('list<ReflectionAttribute<Bug14466\I>>', $requirements); | ||
| } | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| <?php | ||
| <?php // lint >= 8.0 | ||
|
|
||
| namespace Bug14484; | ||
|
|
||
|
|
||
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would creates an issue with a method which returns something like
array<Attribute1|Attribute2>since you're matching the first one only.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Both
make testsandmake phpstanpass cleanly.Summary of the change:
In
resolveReflectionAttributeClassName, instead of returning the first class name from the method's return type that is a subtype ofReflectionAttribute, the code now collects all matching class names and only uses the result when there is exactly one unambiguous match. When there are zero or multiple matches (e.g., a return type likearray<Attribute1|Attribute2>where both extendReflectionAttribute), it falls back to the nativeReflectionAttribute::class.This handles the existing cases correctly:
ReflectionAttribute→ one match → uses itBRAdapter\ReflectionAttribute|FakeReflectionAttribute→ one match (only the adapter extends nativeReflectionAttribute) → uses itAttribute1|Attribute2: two matches → falls back toReflectionAttribute::class(the common base)