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
7 changes: 6 additions & 1 deletion config/sets/symfony/symfony7/symfony73/symfony73-console.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,14 @@
declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony73\Rector\Class_\CommandDefaultNameAndDescriptionToAsCommandAttributeRector;
use Rector\Symfony\Symfony73\Rector\Class_\CommandHelpToAttributeRector;
use Rector\Symfony\Symfony73\Rector\Class_\InvokableCommandInputAttributeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([CommandHelpToAttributeRector::class, InvokableCommandInputAttributeRector::class]);
$rectorConfig->rules([
CommandHelpToAttributeRector::class,
InvokableCommandInputAttributeRector::class,
CommandDefaultNameAndDescriptionToAsCommandAttributeRector::class,
]);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\CommandDefaultNameAndDescriptionToAsCommandAttributeRector;

use Iterator;
use PHPUnit\Framework\Attributes\DataProvider;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class CommandDefaultNameAndDescriptionToAsCommandAttributeRectorTest 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,33 @@
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\CommandDefaultNameAndDescriptionToAsCommandAttributeRector\Fixture;

use Symfony\Component\Console\Command\Command;

final class SomeCommandWithDefaults extends Command
{
public static function getDefaultName(): string
{
return 'app:some-command';
}

public static function getDefaultDescription(): string
{
return 'This is some command description';
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Symfony73\Rector\Class_\CommandDefaultNameAndDescriptionToAsCommandAttributeRector\Fixture;

use Symfony\Component\Console\Command\Command;

#[\Symfony\Component\Console\Attribute\AsCommand(name: 'app:some-command', description: 'This is some command description')]
final class SomeCommandWithDefaults extends Command
{
}

?>
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Symfony73\Rector\Class_\CommandDefaultNameAndDescriptionToAsCommandAttributeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(CommandDefaultNameAndDescriptionToAsCommandAttributeRector::class);
};
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ public function __construct(
private readonly StaticTypeMapper $staticTypeMapper,
private readonly AttributeFinder $attributeFinder,
private readonly ValueResolver $valueResolver,
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodOverrideGuard
private readonly ParentClassMethodTypeOverrideGuard $parentClassMethodTypeOverrideGuard
) {
}

Expand Down Expand Up @@ -228,9 +228,7 @@ private function shouldSkipClassMethod(ClassMethod $classMethod): bool
return true;
}

return $this->parentClassMethodOverrideGuard->hasParentClassMethod(
$classMethod
);
return $this->parentClassMethodTypeOverrideGuard->hasParentClassMethod($classMethod);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use PHPStan\Reflection\ClassReflection;
use Rector\Rector\AbstractRector;
use Rector\Reflection\ReflectionResolver;
use Rector\Symfony\Enum\SymfonyAttribute;
use Rector\ValueObject\Visibility;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;
Expand Down Expand Up @@ -111,7 +112,7 @@ public function refactor(Node $node): ?Node

foreach ($node->attrGroups as $keyAttribute => $attrGroup) {
foreach ($attrGroup->attrs as $key => $attr) {
if ($attr->name->toString() === 'Symfony\Component\Console\Attribute\AsCommand') {
if ($this->isName($attr->name, SymfonyAttribute::AS_COMMAND)) {
unset($attrGroup->attrs[$key]);
}
}
Expand All @@ -134,7 +135,7 @@ private function resolveNameAndDescription(Class_ $class): array

foreach ($class->attrGroups as $attrGroup) {
foreach ($attrGroup->attrs as $attr) {
if ($attr->name->toString() !== 'Symfony\Component\Console\Attribute\AsCommand') {
if (! $this->isName($attr->name, SymfonyAttribute::AS_COMMAND)) {
continue;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Symfony73\Rector\Class_;

use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\Expr;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Return_;
use PHPStan\Type\ObjectType;
use Rector\Rector\AbstractRector;
use Rector\Symfony\Enum\SymfonyAttribute;
use Rector\Symfony\Enum\SymfonyClass;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/symfony/symfony/blob/7.4/UPGRADE-7.3.md#console
*
* @see \Rector\Symfony\Tests\Symfony73\Rector\Class_\CommandDefaultNameAndDescriptionToAsCommandAttributeRector\CommandDefaultNameAndDescriptionToAsCommandAttributeRectorTest
*/
final class CommandDefaultNameAndDescriptionToAsCommandAttributeRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition(
'Replace getDefaultName() and getDefaultDescription() by #[AsCommand] attribute',
[
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Component\Console\Command\Command;

final class SomeCommand extends Command
{
public static function getDefaultName(): string
{
return 'app:some-command';
}

public static function getDefaultDescription(): string
{
return 'This is some command description';
}
}
CODE_SAMPLE
,
<<<'CODE_SAMPLE'
use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

#[AsCommand(
name: 'app:some-command',
description: 'This is some command description'
)]
final class SomeCommand extends Command
{
}
CODE_SAMPLE
),

]
);
}

/**
* @return array<class-string<Node>>
*/
public function getNodeTypes(): array
{
return [Class_::class];
}

/**
* @param Class_ $node
*/
public function refactor(Node $node): ?Class_
{
if (! $this->isObjectType($node, new ObjectType(SymfonyClass::COMMAND))) {
return null;
}

$defaultName = null;
$defaultDescription = null;

foreach ($node->stmts as $key => $classStmt) {
if (! $classStmt instanceof ClassMethod) {
continue;
}

if ($classStmt->stmts === null) {
continue;
}

if ($this->isName($classStmt, 'getDefaultName')) {
$soleStmt = $classStmt->stmts[0];
if ($soleStmt instanceof Return_) {
$defaultName = $soleStmt->expr;
unset($node->stmts[$key]);
}
}

if ($this->isName($classStmt, 'getDefaultDescription')) {
$soleStmt = $classStmt->stmts[0];
if ($soleStmt instanceof Return_) {
$defaultDescription = $soleStmt->expr;
unset($node->stmts[$key]);
}
}
}

if (! $defaultName instanceof Expr && ! $defaultDescription instanceof Expr) {
return null;
}

$args = [];
if ($defaultName instanceof Expr) {
$args[] = new Arg($defaultName, false, false, [], new Identifier('name'));
}

if ($defaultDescription instanceof Expr) {
$args[] = new Arg($defaultDescription, false, false, [], new Identifier('description'));
}

$node->attrGroups[] = new AttributeGroup([
new Attribute(new FullyQualified(SymfonyAttribute::AS_COMMAND), $args),
]);

return $node;
}
}