Support generic trait @use tags in class-level PHPDocs - #6115
Open
misenhower wants to merge 2 commits into
Open
Conversation
Class-level @use tags now participate in trait template type resolution, with statement-level @use tags taking precedence for the same trait. When no statement-level tag matches, FileTypeMapper::getNameScope() reads the doc comment of the class-like using the trait via reflection and consults its @use tags. A new ClassLevelUsedTraitsRule validates class-level @use tags on classes, traits, enums, and interfaces, and UsedTraitsRule no longer reports missing generic types for traits covered by a class-level tag. Closes phpstan/phpstan#7486 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
misenhower
marked this pull request as ready for review
July 27, 2026 18:46
Collaborator
|
This pull request has been marked as ready for review. |
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.
Hey there, I'm looking to add this so I can put all my
@usestatements at the top of the class, rather than on eachusestatement in the class body. Just a nice visual clean-up.This supersedes #2638, which targeted the v1.x branch.
There are a few failing tests, but I think they might be preexisting issues.
This was all from claude/codex so let me know if there are any issues!
Notes from Codex below:
Closes phpstan/phpstan#7486
Summary
Allow generic trait
@useannotations to be placed on the PHPDoc of the class-like using the trait:Previously, PHPStan only used annotations attached directly to the trait-use statement. Some PHPDoc consumers do not support that placement, making it difficult to share these annotations across tools.
This change:
@use,@template-use, and@phpstan-usetags for generic trait type resolution.Statement-level annotations remain fully supported:
Validation
vendor/bin/phpunit tests/PHPStan/Rules/Generics/ClassLevelUsedTraitsRuleTest.php tests/PHPStan/Rules/Generics/UsedTraitsRuleTest.phpvendor/bin/phpunit tests/PHPStan/Analyser/NodeScopeResolverTest.php --filter class-level-use-tag