Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -39,22 +39,35 @@ public function decorate(PhpDocNode $phpDocNode, PhpNode $phpNode): void
return null;
}

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

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

$className = $this->resolveFullyQualifiedClass($splitScopeResolution[0], $phpNode);
$node->setAttribute(PhpDocAttributeKey::RESOLVED_CLASS, $className);

return $node;
});
}

private function resolveClassFromScopeResolution(mixed $value, PhpNode $phpNode): ?string
{
if (! is_string($value)) {
return null;
}

$splitScopeResolution = explode('::', $value);
if (count($splitScopeResolution) !== 2) {
return null;
}

return $this->resolveFullyQualifiedClass($splitScopeResolution[0], $phpNode);
}

private function resolveFullyQualifiedClass(string $className, PhpNode $phpNode): string
{
$nameScope = $this->nameScopeFactory->createNameScopeFromNodeWithoutTemplateTypes($phpNode);
Expand Down
10 changes: 7 additions & 3 deletions src/BetterPhpDocParser/PhpDocInfo/PhpDocInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -558,11 +558,15 @@ public function getArrayItemNodeClassNames(): array
}

$resolvedClass = $node->getAttribute(PhpDocAttributeKey::RESOLVED_CLASS);
if ($resolvedClass === null) {
return null;
if ($resolvedClass !== null) {
$classNames[] = $resolvedClass;
}

$resolvedKeyClass = $node->getAttribute(PhpDocAttributeKey::RESOLVED_KEY_CLASS);
if ($resolvedKeyClass !== null) {
$classNames[] = $resolvedKeyClass;
}

$classNames[] = $resolvedClass;
return $node;
});

Expand Down
5 changes: 5 additions & 0 deletions src/BetterPhpDocParser/ValueObject/PhpDocAttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,11 @@ final class PhpDocAttributeKey
*/
public const string RESOLVED_CLASS = 'resolved_class';

/**
* Fully qualified name of class referenced in an array item key, e.g. SomeEnum::TOTP
*/
public const string RESOLVED_KEY_CLASS = 'resolved_key_class';

public const string PARENT = NativePhpDocAttributeKey::PARENT;

public const string LAST_PHP_DOC_TOKEN_POSITION = 'last_token_position';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

namespace Rector\Tests\Issues\NamespacedUseAutoImport\Fixture;

use Doctrine\ORM\Mapping as ORM;
use Rector\Tests\Issues\NamespacedUseAutoImport\Source\SomeClass;

/**
* @ORM\DiscriminatorMap({
* SomeClass::TOTP = "totp",
* SomeClass::EMAIL = "email"
* })
*/
class SkipAnnotationArrayKeyConst
{
}
Loading