Skip to content

Commit

Permalink
[AutoImport] Apply @\ auto import on AnnotationToAttributeRector (#5286)
Browse files Browse the repository at this point in the history
  • Loading branch information
samsonasik committed Nov 25, 2023
1 parent 3f42ee9 commit 8b7829f
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 1 deletion.
7 changes: 6 additions & 1 deletion packages/PhpAttribute/NodeFactory/AttributeNameFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Rector\Core\Tests\Issues\AutoImport\Fixture\DocBlock;

/**
* @\Doctrine\ORM\Mapping\Entity()
*/
class SomeEntity
{
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\AutoImport\Fixture\DocBlock;

use Doctrine\ORM\Mapping\Entity;
#[Entity]
class SomeEntity
{
}

?>
5 changes: 5 additions & 0 deletions tests/Issues/AutoImport/config/configured_rule.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -12,4 +14,7 @@
'Some\Exception' => 'Some\Target\Exception',
]);
$rectorConfig->rule(TernaryToNullCoalescingRector::class);
$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
new AnnotationToAttribute('Doctrine\ORM\Mapping\Entity')
]);
};

0 comments on commit 8b7829f

Please sign in to comment.