[BetterPhpDocParser] Keep import referenced by `@see`/`@uses` tag with a trailing description by ruudk · Pull Request #8039 · rectorphp/rector-src · GitHub
Skip to content

[BetterPhpDocParser] Keep import referenced by @see/@uses tag with a trailing description#8039

Merged
samsonasik merged 1 commit into
rectorphp:mainfrom
ruudk:ruudk/fix-see
Jun 16, 2026
Merged

[BetterPhpDocParser] Keep import referenced by @see/@uses tag with a trailing description#8039
samsonasik merged 1 commit into
rectorphp:mainfrom
ruudk:ruudk/fix-see

Conversation

@ruudk

@ruudk ruudk commented Jun 16, 2026

Copy link
Copy Markdown
Contributor

When removeUnusedImports() is enabled, UnusedImportRemovingPostRector collects the class names used inside doc blocks through PhpDocInfo::getGenericTagClassNames(). For generic tags such as @see, @uses and @link it only registered the full tag value.

A tag like @see Foo some description therefore produced the single candidate "Foo some description", which never matches the imported short name "Foo". As a result the use import for Foo was considered unused and removed, leaving an unresolved reference (or, for a class in another namespace, one that silently resolves to the wrong class).

Per the phpDocumentor syntax @see [FQSEN] [<description>], the class reference is the leading token of the value. Register that leading token in addition to the full value, so the import is correctly kept when a description follows the class name.

@ruudk ruudk changed the title [BetterPhpDocParser] Keep import referenced by @see/@uses tag with a trailing description [BetterPhpDocParser] Keep import referenced by @see/@uses tag with a trailing description Jun 16, 2026
@ruudk

ruudk commented Jun 16, 2026

Copy link
Copy Markdown
Contributor Author

Comment thread src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php Outdated
…trailing description

When `removeUnusedImports()` is enabled, UnusedImportRemovingPostRector
collects the class names used inside doc blocks through
PhpDocInfo::getGenericTagClassNames(). For generic tags such as @see,
@uses and @link it only registered the full tag value.

A tag like `@see Foo some description` therefore produced the single
candidate "Foo some description", which never matches the imported short
name "Foo". As a result the `use` import for Foo was considered unused
and removed, leaving an unresolved reference (or, for a class in another
namespace, one that silently resolves to the wrong class).

Per the phpDocumentor syntax `@see [FQSEN] [<description>]`, the class
reference is the leading token of the value. Register that leading token
in addition to the full value, so the import is correctly kept when a
description follows the class name.

@samsonasik samsonasik left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good 👍

@samsonasik

Copy link
Copy Markdown
Member

@samsonasik samsonasik merged commit 5dfb29c into rectorphp:main Jun 16, 2026
64 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants