Skip to content

[BetterPhpDocParser] Keep import used only in an annotation array key - #8429

Merged
TomasVotruba merged 1 commit into
mainfrom
fix-annotation-array-key-const-import
Sep 2, 2026
Merged

[BetterPhpDocParser] Keep import used only in an annotation array key#8429
TomasVotruba merged 1 commit into
mainfrom
fix-annotation-array-key-const-import

Conversation

@TomasVotruba

Copy link
Copy Markdown
Member

Fixes wrong removal of a use import that is referenced only in the key side of a Doctrine annotation array, e.g. @ORM\DiscriminatorMap({ SomeEnum::TOTP = ... }).

Reported via demo: https://getrector.com/demo/a835e33e-668f-47f5-8cf8-604988f8b0de

Problem

ArrayItemClassNameDecorator resolved the class name only from the array item value ($node->value). A class used solely in the key (SomeEnum::TOTP) got no RESOLVED_CLASS, so getArrayItemNodeClassNames() never reported it. With removeUnusedImports() on, UnusedImportRemovingPostRector then treated the import as unused and removed it.

Before:

use Doctrine\ORM\Mapping as ORM;
use App\Enum\SomeEnum; // <- wrongly removed

/**
 * @ORM\DiscriminatorMap({
 *     SomeEnum::TOTP = "totp"
 * })
 */
class Foo {}

Fix

  • Resolve a class from the key side too, stored under a new RESOLVED_KEY_CLASS attribute.
  • getArrayItemNodeClassNames() now collects both value and key resolved classes.

Covered by a skip fixture in tests/Issues/NamespacedUseAutoImport.

… like @Orm\DiscriminatorMap({ SomeEnum::TOTP = ... })

The ArrayItemClassNameDecorator only resolved the class from the value side
of an array item, so a class referenced solely in the key of a Doctrine
annotation map was not counted as used. With removeUnusedImports() enabled
the import was then wrongly removed.

Claude-Session: https://claude.ai/code/session_01DnwXQN191Le8TVieRZnWv6
@TomasVotruba
TomasVotruba force-pushed the fix-annotation-array-key-const-import branch from e7ecd0f to a9a90cd Compare September 2, 2026 09:03
@TomasVotruba
TomasVotruba merged commit 96fd595 into main Sep 2, 2026
44 checks passed
@TomasVotruba
TomasVotruba deleted the fix-annotation-array-key-const-import branch September 2, 2026 09:05
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.

1 participant