Skip to content

Commit

Permalink
[AutoImport] Handle AnnotationToAttributeRector + RenameClassRector t…
Browse files Browse the repository at this point in the history
…hen enable auto import (#5213)

* [AutoImport] Handle AnnotationToAttributeRector + RenameClassRector then enable auto import

* [ci-review] Rector Rectify

* use

* clean unrelated printing change

* Fix

* Fqcn test fixture

* more fixture

---------

Co-authored-by: GitHub Action <actions@github.com>
  • Loading branch information
samsonasik and actions-user committed Oct 29, 2023
1 parent c1657f0 commit 3c526c9
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/PostRector/Rector/NameImportingPostRector.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use Rector\Comments\NodeDocBlock\DocBlockUpdater;
use Rector\Core\Configuration\Option;
use Rector\Core\Configuration\Parameter\SimpleParameterProvider;
use Rector\Core\Configuration\RenamedClassesDataCollector;
use Rector\Core\PhpParser\Node\CustomNode\FileWithoutNamespace;
use Rector\Core\Provider\CurrentFileProvider;
use Rector\Core\ValueObject\Application\File;
Expand All @@ -40,6 +41,7 @@ public function __construct(
private readonly UseImportsResolver $useImportsResolver,
private readonly AliasNameResolver $aliasNameResolver,
private readonly DocBlockUpdater $docBlockUpdater,
private readonly RenamedClassesDataCollector $renamedClassesDataCollector
) {
}

Expand All @@ -60,6 +62,7 @@ public function enterNode(Node $node): ?Node
}

if ($node instanceof Name) {
$node = $this->resolveNameFromAttribute($node);
return $this->processNodeName($node, $file);
}

Expand All @@ -86,6 +89,26 @@ public function enterNode(Node $node): ?Node
return $node;
}

private function resolveNameFromAttribute(Name $name): Name
{
if ($name instanceof FullyQualified) {
return $name;
}

if ($name->hasAttribute(AttributeKey::PHP_ATTRIBUTE_NAME)) {
$oldToNewClasses = $this->renamedClassesDataCollector->getOldToNewClasses();
$phpAttributeName = $name->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);

foreach ($oldToNewClasses as $oldName => $newName) {
if ($oldName === $phpAttributeName) {
return new FullyQualified($newName);
}
}
}

return $name;
}

private function processNodeName(Name $name, File $file): ?Node
{
if ($name->isSpecialClassName()) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Core\Tests\Issues\AnnotationToAttributeRenameAutoImport;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class AnnotationToAttributeRenameAutoImportTest extends AbstractRectorTestCase
{
#[DataProvider('provideData')]
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public static function provideData(): Iterator
{
return self::yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/configured_rule.php';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Core\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
* @IsGranted("TEST")
*/
class IsGrantedController extends AbstractController
{
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

#[IsGranted('TEST')]
class IsGrantedController extends AbstractController
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Rector\Core\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
* @\Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted("TEST")
*/
class NotImportedYet extends AbstractController
{
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

#[IsGranted('TEST')]
class NotImportedYet extends AbstractController
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Rector\Core\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

/**
* @\Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted("TEST")
*/
class NotImportedYet2 extends AbstractController
{
}

?>
-----
<?php

namespace Rector\Core\Tests\Issues\AnnotationToAttributeRenameAutoImport\Fixture;

use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

#[IsGranted('TEST')]
class NotImportedYet2 extends AbstractController
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Config\RectorConfig;
use Rector\Php80\Rector\Class_\AnnotationToAttributeRector;
use Rector\Php80\ValueObject\AnnotationToAttribute;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->importNames();

$rectorConfig->ruleWithConfiguration(AnnotationToAttributeRector::class, [
new AnnotationToAttribute('Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted'),
]);

$rectorConfig->ruleWithConfiguration(
RenameClassRector::class,
[
'Sensio\Bundle\FrameworkExtraBundle\Configuration\IsGranted' => 'Symfony\Component\Security\Http\Attribute\IsGranted',
],
);
};

0 comments on commit 3c526c9

Please sign in to comment.