diff --git a/config/sets/symfony/symfony6/symfony62/symfony62-security-http.php b/config/sets/symfony/symfony6/symfony62/symfony62-security-http.php index 832ff71c..f19b12a7 100644 --- a/config/sets/symfony/symfony6/symfony62/symfony62-security-http.php +++ b/config/sets/symfony/symfony6/symfony62/symfony62-security-http.php @@ -3,10 +3,10 @@ declare(strict_types=1); use Rector\Config\RectorConfig; -use Rector\Renaming\Rector\Name\RenameClassRector; -use Rector\Symfony\Symfony62\Rector\Class_\SecurityAttributeToIsGrantedAttributeRector; use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector; +use Rector\Renaming\Rector\Name\RenameClassRector; use Rector\Renaming\ValueObject\RenameClassAndConstFetch; +use Rector\Symfony\Symfony62\Rector\Class_\SecurityAttributeToIsGrantedAttributeRector; return static function (RectorConfig $rectorConfig): void { $rectorConfig->rule(SecurityAttributeToIsGrantedAttributeRector::class); diff --git a/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/Fixture/do_not_re_add_to_attribute_when_exists.php.inc b/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/Fixture/do_not_re_add_to_attribute_when_exists.php.inc new file mode 100644 index 00000000..c92ac851 --- /dev/null +++ b/rules-tests/Symfony61/Rector/Class_/CommandConfigureToAttributeRector/Fixture/do_not_re_add_to_attribute_when_exists.php.inc @@ -0,0 +1,47 @@ +setName('app:accounting') + ->setDescription('accounting') + ; + } +} + +?> +----- + diff --git a/rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php b/rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php index fcbf7d04..d502952d 100644 --- a/rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php +++ b/rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php @@ -25,6 +25,7 @@ use Rector\VersionBonding\Contract\MinPhpVersionInterface; use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; +use Webmozart\Assert\Assert; /** * @see https://symfony.com/doc/current/console.html#registering-the-command @@ -147,11 +148,24 @@ public function refactor(Node $node): ?Node $node->attrGroups[] = $asCommandAttributeGroup; } + $existingAttributeNames = array_map( + function (Arg $arg): string { + Assert::isInstanceOf($arg->name, Identifier::class); + return $arg->name->toString(); + }, + $attributeArgs + ); foreach (self::METHODS_TO_ATTRIBUTE_NAMES as $methodName => $attributeName) { $resolvedExpr = $this->findAndRemoveMethodExpr($configureClassMethod, $methodName); - if ($resolvedExpr instanceof Expr) { - $attributeArgs[] = $this->createNamedArg($attributeName, $resolvedExpr); + if (! $resolvedExpr instanceof Expr) { + continue; + } + + if (in_array($attributeName, $existingAttributeNames, true)) { + continue; } + + $attributeArgs[] = $this->createNamedArg($attributeName, $resolvedExpr); } $asCommandAttribute->args = $attributeArgs; diff --git a/rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php b/rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php index 37ff6b5f..973ea4c5 100644 --- a/rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php +++ b/rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php @@ -4,15 +4,15 @@ namespace Rector\Symfony\Symfony73\NodeFactory; -use PhpParser\Node\Expr\ConstFetch; -use PhpParser\Node\Name; use PhpParser\Node; use PhpParser\Node\Arg; use PhpParser\Node\Attribute; use PhpParser\Node\AttributeGroup; use PhpParser\Node\Expr; +use PhpParser\Node\Expr\ConstFetch; use PhpParser\Node\Expr\Variable; use PhpParser\Node\Identifier; +use PhpParser\Node\Name; use PhpParser\Node\Name\FullyQualified; use PhpParser\Node\NullableType; use PhpParser\Node\Param;