Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Symfony 6.1] Add CommandConfigureToAttributeRector #619

Merged
merged 6 commits into from
Jun 22, 2024
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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
"symplify/easy-coding-standard": "^12.3",
"symplify/phpstan-extensions": "^11.1",
"symplify/phpstan-rules": "^13.0",
"symplify/rule-doc-generator": "^12.0",
"symplify/rule-doc-generator": "^12.2.2",
"symplify/vendor-patches": "^11.2",
"tomasvotruba/class-leak": "^0.2",
"tracy/tracy": "^2.10"
Expand Down
21 changes: 10 additions & 11 deletions config/sets/symfony/symfony61.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Symfony\Symfony61\Rector\Class_\CommandConfigureToAttributeRector;
use Rector\Symfony\Symfony61\Rector\Class_\CommandPropertyToAttributeRector;
use Rector\Symfony\Symfony61\Rector\Class_\MagicClosureTwigExtensionToNativeMethodsRector;
use Rector\Symfony\Symfony61\Rector\StaticPropertyFetch\ErrorNamesPropertyToConstantRector;
Expand All @@ -12,20 +13,18 @@

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rules([
CommandConfigureToAttributeRector::class,
CommandPropertyToAttributeRector::class,
ErrorNamesPropertyToConstantRector::class,
MagicClosureTwigExtensionToNativeMethodsRector::class,
]);

$rectorConfig->ruleWithConfiguration(
RenameClassRector::class,
[
// @see https://github.com/symfony/symfony/pull/43982
'Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\DenormalizerInterface',
'Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface',
// @see https://github.com/symfony/symfony/pull/45623
'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntax' => 'Symfony\Component\Validator\Constraints\ExpressionSyntax',
'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator' => 'Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator',
],
);
$rectorConfig->ruleWithConfiguration(RenameClassRector::class, [
// @see https://github.com/symfony/symfony/pull/43982
'Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\DenormalizerInterface',
'Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface',
// @see https://github.com/symfony/symfony/pull/45623
'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntax' => 'Symfony\Component\Validator\Constraints\ExpressionSyntax',
'Symfony\Component\Validator\Constraints\ExpressionLanguageSyntaxValidator' => 'Symfony\Component\Validator\Constraints\ExpressionSyntaxValidator',
]);
};
10 changes: 10 additions & 0 deletions config/sets/symfony/symfony70.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;

// @see https://github.com/symfony/symfony/blob/7.0/UPGRADE-7.0.md
return static function (RectorConfig $rectorConfig): void {
// @todo next
};
71 changes: 27 additions & 44 deletions docs/rector_rules_overview.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# 86 Rules Overview
# 85 Rules Overview

## ActionSuffixRemoverRector

Expand Down Expand Up @@ -220,43 +220,43 @@ Change type in CollectionType from alias string to class reference

<br>

## CommandConstantReturnCodeRector
## CommandConfigureToAttributeRector

Changes int return from execute to use Symfony Command constants.
Add `Symfony\Component\Console\Attribute\AsCommand` to Symfony Commands from `configure()`

- class: [`Rector\Symfony\Symfony51\Rector\ClassMethod\CommandConstantReturnCodeRector`](../rules/Symfony51/Rector/ClassMethod/CommandConstantReturnCodeRector.php)
- class: [`Rector\Symfony\Symfony61\Rector\Class_\CommandConfigureToAttributeRector`](../rules/Symfony61/Rector/Class_/CommandConfigureToAttributeRector.php)

```diff
class SomeCommand extends Command
{
protected function execute(InputInterface $input, OutputInterface $output): int
{
- return 0;
+ return \Symfony\Component\Console\Command\Command::SUCCESS;
}
+use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

+#[AsCommand('sunshine')]
final class SunshineCommand extends Command
{
- public function configure()
- {
- $this->setName('sunshine');
- }
}
```

<br>

## CommandDescriptionToPropertyRector
## CommandConstantReturnCodeRector

Symfony Command description setters are moved to properties
Changes int return from execute to use Symfony Command constants.

- class: [`Rector\Symfony\Symfony53\Rector\Class_\CommandDescriptionToPropertyRector`](../rules/Symfony53/Rector/Class_/CommandDescriptionToPropertyRector.php)
- class: [`Rector\Symfony\Symfony51\Rector\ClassMethod\CommandConstantReturnCodeRector`](../rules/Symfony51/Rector/ClassMethod/CommandConstantReturnCodeRector.php)

```diff
use Symfony\Component\Console\Command\Command

final class SunshineCommand extends Command
class SomeCommand extends Command
{
+ protected static $defaultDescription = 'sunshine description';
+
public function configure()
protected function execute(InputInterface $input, OutputInterface $output): int
{
- $this->setDescription('sunshine description');
- return 0;
+ return \Symfony\Component\Console\Command\Command::SUCCESS;
}

}
```

Expand All @@ -272,11 +272,15 @@ Add `Symfony\Component\Console\Attribute\AsCommand` to Symfony Commands and remo
+use Symfony\Component\Console\Attribute\AsCommand;
use Symfony\Component\Console\Command\Command;

+#[AsCommand('sunshine')]
+#[AsCommand(
+ name: 'sunshine',
+ description: 'some description'
+)]
final class SunshineCommand extends Command
{
- /** @var string|null */
- public static $defaultName = 'sunshine';
-
- public static $defaultDescription = 'Ssome description';
}
```

Expand Down Expand Up @@ -918,27 +922,6 @@ Change TwigExtension function/filter magic closures to inlined and clear callabl

<br>

## MakeCommandLazyRector

Make Symfony commands lazy

- class: [`Rector\Symfony\CodeQuality\Rector\Class_\MakeCommandLazyRector`](../rules/CodeQuality/Rector/Class_/MakeCommandLazyRector.php)

```diff
use Symfony\Component\Console\Command\Command

final class SunshineCommand extends Command
{
+ protected static $defaultName = 'sunshine';
public function configure()
{
- $this->setName('sunshine');
}
}
```

<br>

## MakeDispatchFirstArgumentEventRector

Make event object a first argument of `dispatch()` method, event name as second
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

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

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

final class CommandConfigureToAttributeRectorTest 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,28 @@
<?php

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

final class NameAndAliases extends \Symfony\Component\Console\Command\Command
{
public function configure()
{
$this->setName('sunshine');
$this->setAliases(['first', 'second']);
}
}

?>
-----
<?php

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

#[\Symfony\Component\Console\Attribute\AsCommand(name: 'sunshine', aliases: ['first', 'second'])]
final class NameAndAliases extends \Symfony\Component\Console\Command\Command
{
public function configure()
{
}
}

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

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

final class NameAndDescriptionFluent extends \Symfony\Component\Console\Command\Command
{
public function configure()
{
$this->setName('sunshine')
->setDescription('rising above');
}
}

?>
-----
<?php

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

#[\Symfony\Component\Console\Attribute\AsCommand(name: 'sunshine', description: 'rising above')]
final class NameAndDescriptionFluent extends \Symfony\Component\Console\Command\Command
{
public function configure()
{
}
}

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

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

final class SomeCommand extends \Symfony\Component\Console\Command\Command
{
public function configure()
{
$this->setName('sunshine');
}
}

?>
-----
<?php

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

#[\Symfony\Component\Console\Attribute\AsCommand(name: 'sunshine')]
final class SomeCommand extends \Symfony\Component\Console\Command\Command
{
public function configure()
{
}
}

?>
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\Symfony61\Rector\Class_\CommandConfigureToAttributeRector;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->rule(CommandConfigureToAttributeRector::class);
};

This file was deleted.

This file was deleted.

This file was deleted.

Loading
Loading