Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[AutoImport] Handle AnnotationToAttributeRector + RenameClassRector then enable auto import #5213

Merged
merged 7 commits into from
Oct 29, 2023
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
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',
],
);
};