Skip to content

Commit e7ecd0f

Browse files
committed
[BetterPhpDocParser] Keep import used only in an annotation array key 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
1 parent 28a4645 commit e7ecd0f

4 files changed

Lines changed: 49 additions & 11 deletions

File tree

‎src/BetterPhpDocParser/NodeDecorator/ArrayItemClassNameDecorator.php‎

Lines changed: 21 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,22 +39,35 @@ public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode): void
3939
return null;
4040
}
4141

42-
if (! is_string($node->value)) {
43-
return null;
42+
$valueClassName = $this->resolveClassFromScopeResolution($node->value, $phpNode);
43+
if ($valueClassName !== null) {
44+
$node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $valueClassName);
4445
}
4546

46-
$splitScopeResolution = explode('::', $node->value);
47-
if (count($splitScopeResolution) !== 2) {
48-
return null;
47+
// e.g. @ORM\DiscriminatorMap({ SomeEnum::TOTP = "..." }), the class is in the key
48+
$keyClassName = $this->resolveClassFromScopeResolution($node->key, $phpNode);
49+
if ($keyClassName !== null) {
50+
$node->setAttribute(PhpDocAttributeKey::RESOLVED_KEY_CLASS, $keyClassName);
4951
}
5052

51-
$className = $this->resolveFullyQualifiedClass($splitScopeResolution[0], $phpNode);
52-
$node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className);
53-
5453
return $node;
5554
});
5655
}
5756

57+
private function resolveClassFromScopeResolution(mixed $value, PhpNode $phpNode): ?string
58+
{
59+
if (! is_string($value)) {
60+
return null;
61+
}
62+
63+
$splitScopeResolution = explode('::', $value);
64+
if (count($splitScopeResolution) !== 2) {
65+
return null;
66+
}
67+
68+
return $this->resolveFullyQualifiedClass($splitScopeResolution[0], $phpNode);
69+
}
70+
5871
private function resolveFullyQualifiedClass(string $className, PhpNode $phpNode): string
5972
{
6073
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode);

‎src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -558,11 +558,15 @@ public function getArrayItemNodeClassNames(): array
558558
}
559559

560560
$resolvedClass = $node->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
561-
if ($resolvedClass === null) {
562-
return null;
561+
if ($resolvedClass !== null) {
562+
$classNames[] = $resolvedClass;
563+
}
564+
565+
$resolvedKeyClass = $node->getAttribute(PhpDocAttributeKey::RESOLVED_KEY_CLASS);
566+
if ($resolvedKeyClass !== null) {
567+
$classNames[] = $resolvedKeyClass;
563568
}
564569

565-
$classNames[] = $resolvedClass;
566570
return $node;
567571
});
568572

‎src/BetterPhpDocParser/ValueObject/PhpDocAttributeKey.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ final class PhpDocAttributeKey
1515
*/
1616
public const string RESOLVED_CLASS = 'resolved_class';
1717

18+
/**
19+
* Fully qualified name of class referenced in an array item key, e.g. SomeEnum::TOTP
20+
*/
21+
public const string RESOLVED_KEY_CLASS = 'resolved_key_class';
22+
1823
public const string PARENT = NativePhpDocAttributeKey::PARENT;
1924

2025
public const string LAST_PHP_DOC_TOKEN_POSITION = 'last_token_position';
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;
4+
5+
use Doctrine\ORM\Mapping as ORM;
6+
use Rector\Tests\Issues\NamespacedUseAutoImport\Source\SomeClass;
7+
8+
/**
9+
* @ORM\DiscriminatorMap({
10+
* SomeClass::TOTP = "totp",
11+
* SomeClass::EMAIL = "email"
12+
* })
13+
*/
14+
class SkipAnnotationArrayKeyConst
15+
{
16+
}

0 commit comments

Comments
 (0)