Skip to content

Allow configuring custom PHPDoc tags that reference classes#7895

Open
ruudk wants to merge 1 commit intorectorphp:mainfrom
ruudk:custom-phpdoc-tags-with-class-reference
Open

Allow configuring custom PHPDoc tags that reference classes#7895
ruudk wants to merge 1 commit intorectorphp:mainfrom
ruudk:custom-phpdoc-tags-with-class-reference

Conversation

@ruudk
Copy link
Contributor

@ruudk ruudk commented Feb 14, 2026

In our project we use a custom PHPDoc tag @throwable-from paired with a PHPStan rule that validates the referenced class and method exist. For example:

    /**
     * @throwable-from NotificationHandler::createPayload
     * @throwable-from UserSpecificPayloadNotificationHandler::createPayloadForUser
     */
    final class MessageShouldBeIgnoredException extends Exception

Rector's unused import removal does not recognize class references in custom PHPDoc tags and removes the corresponding use statements. The class resolver in PhpDocTagGenericUsesDecorator was hardcoded to only @uses, @used-by, and @see.

This adds a configurable list of additional PHPDoc tag names that should be treated as containing class references, so their imports are preserved:

    RectorConfig::configure()
        ->withPhpDocTagsWithClassReference(['@throwable-from']);

/cc @samsonasik

In our project we use a custom PHPDoc tag `@throwable-from` paired with a
PHPStan rule that validates the referenced class and method exist. For example:

```php
    /**
     * @throwable-from NotificationHandler::createPayload
     * @throwable-from UserSpecificPayloadNotificationHandler::createPayloadForUser
     */
    final class MessageShouldBeIgnoredException extends Exception
```

Rector's unused import removal does not recognize class references in custom
PHPDoc tags and removes the corresponding use statements. The class resolver
in PhpDocTagGenericUsesDecorator was hardcoded to only `@uses`, `@used-by`,
and `@see`.

This adds a configurable list of additional PHPDoc tag names that should be
treated as containing class references, so their imports are preserved:

```php
    RectorConfig::configure()
        ->withPhpDocTagsWithClassReference(['@throwable-from']);
```
@TomasVotruba
Copy link
Member

I'd prefer generic solution that would not require configuration per project.

@ruudk
Copy link
Contributor Author

ruudk commented Feb 14, 2026

How would you like to do that with a custom phpdoc like this? I believe this was done for performance reasons?

@samsonasik
Copy link
Member

I am on mobile, how about just remove the : if (! in_array() check? The next line already check on explode on :.

This of course need to verify other decorator that doing similar logic on different docblock node to avoid overwritten.

@ruudk
Copy link
Contributor Author

ruudk commented Feb 14, 2026

Yeah i think that would be fine too!

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.

3 participants