diff --git a/packages/PhpAttribute/NodeFactory/AttributeNameFactory.php b/packages/PhpAttribute/NodeFactory/AttributeNameFactory.php index 5f03d1c550d..08dd63f61aa 100644 --- a/packages/PhpAttribute/NodeFactory/AttributeNameFactory.php +++ b/packages/PhpAttribute/NodeFactory/AttributeNameFactory.php @@ -27,11 +27,16 @@ public function create( DoctrineAnnotationTagValueNode $doctrineAnnotationTagValueNode, array $uses ): FullyQualified|Name { - // A. attribute and class name are the same, so we re-use the short form to keep code compatible with previous one + // A. attribute and class name are the same, so we re-use the short form to keep code compatible with previous one, + // except start with \ if ($annotationToAttribute->getAttributeClass() === $annotationToAttribute->getTag()) { $attributeName = $doctrineAnnotationTagValueNode->identifierTypeNode->name; $attributeName = ltrim($attributeName, '@'); + if (str_starts_with($attributeName, '\\')) { + return new FullyQualified(ltrim($attributeName, '\\')); + } + return new Name($attributeName); } diff --git a/tests/Issues/AutoImport/Fixture/DocBlock/annotation_to_attribute.php.inc b/tests/Issues/AutoImport/Fixture/DocBlock/annotation_to_attribute.php.inc new file mode 100644 index 00000000000..b53fb5a002c --- /dev/null +++ b/tests/Issues/AutoImport/Fixture/DocBlock/annotation_to_attribute.php.inc @@ -0,0 +1,24 @@ + +----- + \ No newline at end of file diff --git a/tests/Issues/AutoImport/config/configured_rule.php b/tests/Issues/AutoImport/config/configured_rule.php index 32301aa2d43..bdcd39467b5 100644 --- a/tests/Issues/AutoImport/config/configured_rule.php +++ b/tests/Issues/AutoImport/config/configured_rule.php @@ -4,6 +4,8 @@ use Rector\Config\RectorConfig; use Rector\Php70\Rector\Ternary\TernaryToNullCoalescingRector; +use Rector\Php80\Rector\Class_\AnnotationToAttributeRector; +use Rector\Php80\ValueObject\AnnotationToAttribute; use Rector\Renaming\Rector\Name\RenameClassRector; return static function (RectorConfig $rectorConfig): void { @@ -12,4 +14,7 @@ 'Some\Exception' => 'Some\Target\Exception', ]); $rectorConfig->rule(TernaryToNullCoalescingRector::class); + $rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [ + new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity') + ]); };