Skip to content

Support generic trait @use tags in class-level PHPDocs - #6115

Open
misenhower wants to merge 2 commits into
phpstan:2.2.xfrom
misenhower:class-level-use-tags-v2
Open

Support generic trait @use tags in class-level PHPDocs#6115
misenhower wants to merge 2 commits into
phpstan:2.2.xfrom
misenhower:class-level-use-tags-v2

Conversation

@misenhower

@misenhower misenhower commented Jul 27, 2026

Copy link
Copy Markdown

Hey there, I'm looking to add this so I can put all my @use statements at the top of the class, rather than on each use statement 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 @use annotations to be placed on the PHPDoc of the class-like using the trait:

/**
 * @use GenericTrait<string>
 */
final class Example
{
    use GenericTrait;
}

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:

  • Uses class-level @use, @template-use, and @phpstan-use tags for generic trait type resolution.
  • Preserves existing statement-level behavior, with a matching statement-level annotation taking precedence.
  • Adds a separate rule to validate class-level annotations, as proposed in the issue.
  • Includes referenced class-level annotation types in dependency tracking.
  • Covers multiple traits, nested traits, invalid and unmatched annotations, template forwarding, enums, prefixed tags, and imported trait aliases.
  • Adds type-inference and diagnostic regression tests.

Statement-level annotations remain fully supported:

final class Example
{
    /** @use GenericTrait<string> */
    use GenericTrait;
}

Validation

  • vendor/bin/phpunit tests/PHPStan/Rules/Generics/ClassLevelUsedTraitsRuleTest.php tests/PHPStan/Rules/Generics/UsedTraitsRuleTest.php
  • vendor/bin/phpunit tests/PHPStan/Analyser/NodeScopeResolverTest.php --filter class-level-use-tag

misenhower and others added 2 commits July 27, 2026 11:20
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
misenhower marked this pull request as ready for review July 27, 2026 18:46
@phpstan-bot

Copy link
Copy Markdown
Collaborator

This pull request has been marked as ready for review.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow @phpstan-use (and @use) in PHPdoc-block for classes

2 participants