Skip to content
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
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Rector\Symfony\Tests\Symfony61\Rector\Class_\CommandConfigureToAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

#[AsCommand(
name: 'app:accounting',
description: 'accounting',
)]
class DoNotReAddToAttributeWhenExists extends Command
{

protected function configure(): void
{
parent::configure();

$this->setName('app:accounting')
->setDescription('accounting')
;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony61\Rector\Class_\CommandConfigureToAttributeRector\Fixture;

use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

#[AsCommand(
name: 'app:accounting',
description: 'accounting',
)]
class DoNotReAddToAttributeWhenExists extends Command
{

protected function configure(): void
{
parent::configure();
}
}

?>
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down
4 changes: 2 additions & 2 deletions rules/Symfony73/NodeFactory/CommandInvokeParamsFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down