Skip to content

Commit

Permalink
[Renaming][AutoImport] Handle after change annotation to attribute wi…
Browse files Browse the repository at this point in the history
…th rename on AnnotationToAttributeRector + RenameClassRector with auto import (#5741)

* [Renaming][AutoImport] Handle after change annotation to attribute with rename on AnnotationToAttributeRector + RenameClassRector with auto import

* Fix

* Fix
  • Loading branch information
samsonasik committed Mar 19, 2024
1 parent 1bf3947 commit 685ba8a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
Expand Up @@ -6,9 +6,12 @@

use Nette\Utils\Strings;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\Name\FullyQualified;
use Rector\CodingStyle\ClassNameImport\ShortNameResolver;
use Rector\CodingStyle\Contract\ClassNameImport\ClassNameImportSkipVoterInterface;
use Rector\Configuration\RenamedClassesDataCollector;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\StaticTypeMapper\ValueObject\Type\FullyQualifiedObjectType;
use Rector\ValueObject\Application\File;

Expand Down Expand Up @@ -38,6 +41,12 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
$className = $fullyQualifiedObjectType->getClassName();
$removedUses = $this->renamedClassesDataCollector->getOldClasses();

$originalName = $node->getAttribute(AttributeKey::ORIGINAL_NAME);
$originalNameToAttribute = null;
if ($originalName instanceof Name && ! $originalName instanceof FullyQualified && $originalName->hasAttribute(AttributeKey::PHP_ATTRIBUTE_NAME)) {
$originalNameToAttribute = $originalName->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
}

foreach ($shortNamesToFullyQualifiedNames as $shortName => $fullyQualifiedName) {
if ($fullyQualifiedObjectTypeShortName !== $shortName) {
$shortName = $this->cleanShortName($shortName);
Expand All @@ -52,7 +61,11 @@ public function shouldSkip(File $file, FullyQualifiedObjectType $fullyQualifiedO
return false;
}

return ! in_array($fullyQualifiedName, $removedUses, true);
if (! in_array($fullyQualifiedName, $removedUses, true)) {
return $originalNameToAttribute == null || ! in_array($originalNameToAttribute, $removedUses, true);
}

return false;
}

return false;
Expand Down
34 changes: 26 additions & 8 deletions src/PostRector/Rector/ClassRenamingPostRector.php
Expand Up @@ -69,14 +69,7 @@ public function enterNode(Node $node): ?Node
if ($node instanceof FullyQualified) {
$result = $this->classRenamer->renameNode($node, $oldToNewClasses, $scope);
} else {
$phpAttributeName = $node->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
if (is_string($phpAttributeName)) {
$result = $this->classRenamer->renameNode(
new FullyQualified($phpAttributeName, $node->getAttributes()),
$oldToNewClasses,
$scope
);
}
$result = $this->resolveResultWithPhpAttributeName($node, $oldToNewClasses, $scope);
}

if (! SimpleParameterProvider::provideBoolParameter(Option::AUTO_IMPORT_NAMES)) {
Expand All @@ -102,4 +95,29 @@ public function afterTraverse(array $nodes): array
$this->renamedNameCollector->reset();
return $nodes;
}

/**
* @param array<string, string> $oldToNewClasses
*/
private function resolveResultWithPhpAttributeName(
Name $name,
array $oldToNewClasses,
?Scope $scope
): ?FullyQualified {
$phpAttributeName = $name->getAttribute(AttributeKey::PHP_ATTRIBUTE_NAME);
if (is_string($phpAttributeName)) {
$result = $this->classRenamer->renameNode(
new FullyQualified($phpAttributeName, $name->getAttributes()),
$oldToNewClasses,
$scope
);
if ($result instanceof FullyQualified) {
$result->setAttribute(AttributeKey::ORIGINAL_NAME, $name);
}

return $result;
}

return null;
}
}
Expand Up @@ -20,11 +20,12 @@ class WithExistingAttribute extends AbstractController

namespace Rector\Tests\Issues\RenameAnnotationToAttributeAutoImport\Fixture;

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

#[Route(path: '/pro/{id}/networks/{networkId}/sectors', name: 'api_network_sectors', requirements: ['id' => '\d+', 'networkId' => '\d+'])]
#[\Symfony\Component\Security\Http\Attribute\IsGranted('TEST')]
#[IsGranted('TEST')]
class WithExistingAttribute extends AbstractController
{
}
Expand Down

0 comments on commit 685ba8a

Please sign in to comment.