Skip to content

Commit 3848c83

Browse files
authored
[Symfony61] Handle already exists attributes name on CommandConfigureToAttributeRector (#849)
* [Symfony61] Handle already exists attributes name on CommandConfigureToAttributeRector * Fix
1 parent 99ef2cf commit 3848c83

File tree

4 files changed

+67
-6
lines changed

4 files changed

+67
-6
lines changed

config/sets/symfony/symfony6/symfony62/symfony62-security-http.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
declare(strict_types=1);
44

55
use Rector\Config\RectorConfig;
6-
use Rector\Renaming\Rector\Name\RenameClassRector;
7-
use Rector\Symfony\Symfony62\Rector\Class_\SecurityAttributeToIsGrantedAttributeRector;
86
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
7+
use Rector\Renaming\Rector\Name\RenameClassRector;
98
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;
9+
use Rector\Symfony\Symfony62\Rector\Class_\SecurityAttributeToIsGrantedAttributeRector;
1010

1111
return static function (RectorConfig $rectorConfig): void {
1212
$rectorConfig->rule(SecurityAttributeToIsGrantedAttributeRector::class);
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Rector\Symfony\Tests\Symfony61\Rector\Class_\CommandConfigureToAttributeRector\Fixture;
4+
5+
use Symfony\Component\Console\Attribute\AsCommand;
6+
use Symfony\Component\Console\Command\Command;
7+
8+
#[AsCommand(
9+
name: 'app:accounting',
10+
description: 'accounting',
11+
)]
12+
class DoNotReAddToAttributeWhenExists extends Command
13+
{
14+
15+
protected function configure(): void
16+
{
17+
parent::configure();
18+
19+
$this->setName('app:accounting')
20+
->setDescription('accounting')
21+
;
22+
}
23+
}
24+
25+
?>
26+
-----
27+
<?php
28+
29+
namespace Rector\Symfony\Tests\Symfony61\Rector\Class_\CommandConfigureToAttributeRector\Fixture;
30+
31+
use Symfony\Component\Console\Attribute\AsCommand;
32+
use Symfony\Component\Console\Command\Command;
33+
34+
#[AsCommand(
35+
name: 'app:accounting',
36+
description: 'accounting',
37+
)]
38+
class DoNotReAddToAttributeWhenExists extends Command
39+
{
40+
41+
protected function configure(): void
42+
{
43+
parent::configure();
44+
}
45+
}
46+
47+
?>

rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Rector\VersionBonding\Contract\MinPhpVersionInterface;
2626
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
2727
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
28+
use Webmozart\Assert\Assert;
2829

2930
/**
3031
* @see https://symfony.com/doc/current/console.html#registering-the-command
@@ -147,11 +148,24 @@ public function refactor(Node $node): ?Node
147148
$node->attrGroups[] = $asCommandAttributeGroup;
148149
}
149150

151+
$existingAttributeNames = array_map(
152+
function (Arg $arg): string {
153+
Assert::isInstanceOf($arg->name, Identifier::class);
154+
return $arg->name->toString();
155+
},
156+
$attributeArgs
157+
);
150158
foreach (self::METHODS_TO_ATTRIBUTE_NAMES as $methodName => $attributeName) {
151159
$resolvedExpr = $this->findAndRemoveMethodExpr($configureClassMethod, $methodName);
152-
if ($resolvedExpr instanceof Expr) {
153-
$attributeArgs[] = $this->createNamedArg($attributeName, $resolvedExpr);
160+
if (! $resolvedExpr instanceof Expr) {
161+
continue;
162+
}
163+
164+
if (in_array($attributeName, $existingAttributeNames, true)) {
165+
continue;
154166
}
167+
168+
$attributeArgs[] = $this->createNamedArg($attributeName, $resolvedExpr);
155169
}
156170

157171
$asCommandAttribute->args = $attributeArgs;

rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,15 @@
44

55
namespace Rector\Symfony\Symfony73\NodeFactory;
66

7-
use PhpParser\Node\Expr\ConstFetch;
8-
use PhpParser\Node\Name;
97
use PhpParser\Node;
108
use PhpParser\Node\Arg;
119
use PhpParser\Node\Attribute;
1210
use PhpParser\Node\AttributeGroup;
1311
use PhpParser\Node\Expr;
12+
use PhpParser\Node\Expr\ConstFetch;
1413
use PhpParser\Node\Expr\Variable;
1514
use PhpParser\Node\Identifier;
15+
use PhpParser\Node\Name;
1616
use PhpParser\Node\Name\FullyQualified;
1717
use PhpParser\Node\NullableType;
1818
use PhpParser\Node\Param;

0 commit comments

Comments
 (0)