8000 [Symfony61] Add rules for upgrade by JohJohan · Pull Request #266 · rectorphp/rector-symfony · GitHub
[go: up one dir, main page]

Skip to content

[Symfony61] Add rules for upgrade #266

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

Merged
merged 6 commits into from
Nov 7, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 11 additions & 0 deletions config/sets/symfony/level/up-to-symfony-61.php
8000
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Set\SymfonyLevelSetList;
use Rector\Symfony\Set\SymfonySetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SymfonySetList::SYMFONY_61, SymfonyLevelSetList::UP_TO_SYMFONY_60]);
};
2 changes: 1 addition & 1 deletion config/sets/symfony/level/up-to-symfony-62.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
use Rector\Symfony\Set\SymfonySetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->sets([SymfonySetList::SYMFONY_62, SymfonyLevelSetList::UP_TO_SYMFONY_60]);
$rectorConfig->sets([SymfonySetList::SYMFONY_62, SymfonyLevelSetList::UP_TO_SYMFONY_61]);
};
25 changes: 25 additions & 0 deletions config/sets/symfony/symfony61.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Renaming\Rector\Name\RenameClassRector;
use Rector\Symfony\Rector\Class_\CommandDescriptionToPropertyRector;

# https://github.com/symfony/symfony/blob/6.1/UPGRADE-6.1.md

return static function (RectorConfig $rectorConfig): void {
// @see https://symfony.com/blog/new-in-symfony-5-3-lazy-command-description
$rectorConfig->rule(CommandDescriptionToPropertyRector::class);

$rectorConfig->ruleWithConfiguration(
RenameClassRector::class,
[
// @see https://github.com/symfony/symfony/pull/43982
'Symfony\Component\Serializer\Normalizer\ContextAwareDecoderInterface' => 'Symfony\Component\Serializer\Normalizer\DecoderInterface',
'Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\DenormalizerInterface',
'Symfony\Component\Serializer\Normalizer\ContextAwareEncoderInterface' => 'Symfony\Component\Serializer\Normalizer\EncoderInterface',
'Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface' => 'Symfony\Component\Serializer\Normalizer\NormalizerInterface',
],
);
};
5 changes: 5 additions & 0 deletions src/Set/SymfonyLevelSetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,11 @@ final class SymfonyLevelSetList implements SetListInterface
*/
final public const UP_TO_SYMFONY_60 = __DIR__ . '/../../config/sets/symfony/level/up-to-symfony-60.php';

/**
* @var string
*/
final public const UP_TO_SYMFONY_61 = __DIR__ . '/../../config/sets/symfony/level/up-to-symfony-61.php';

/**
* @var string
*/
Expand Down
5 changes: 5 additions & 0 deletions src/Set/SymfonySetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ final class SymfonySetList implements SetListInterface
*/
final public const SYMFONY_60 = __DIR__ . '/../../config/sets/symfony/symfony60.php';

/**
* @var string
*/
final public const SYMFONY_61 = __DIR__ . '/../../config/sets/symfony/symfony61.php';

/**
* @var string
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php

namespace Rector\Symfony\Tests\Set\Symfony61\Fixture;

use Symfony\Component\Serializer\Normalizer\ContextAwareDecoderInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareEncoderInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DecoderInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\EncoderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class ReplaceDepricatedInterfacesSerializer implements DecoderInterface, DenormalizerInterface, EncoderInterface, NormalizerInterface, ContextAwareDecoderInterface, ContextAwareEncoderInterface, ContextAwareNormalizerInterface, ContextAwareDenormalizerInterface
{
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Set\Symfony61\Fixture;


use Symfony\Component\Serializer\Normalizer\ContextAwareDenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareEncoderInterface;
use Symfony\Component\Serializer\Normalizer\ContextAwareNormalizerInterface;
use Symfony\Component\Serializer\Normalizer\DecoderInterface;
use Symfony\Component\Serializer\Normalizer\DenormalizerInterface;
use Symfony\Component\Serializer\Normalizer\EncoderInterface;
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;

class ReplaceDepricatedInterfacesSerializer implements DecoderInterface, DenormalizerInterface, EncoderInterface, NormalizerInterface
{
}

?>
57 changes: 57 additions & 0 deletions tests/Set/Symfony61/Fixture/set_description_at_top.php.inc
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Rector\Symfony\Tests\Set\Symfony61\Fixture;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SetDescriptionAtTop extends \Symfony\Component\Console\Command\Command
{
public function configure()
{
$this
->setName('some:command')
->setDescription('Description');
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
return 0;
}
}

?>
-----
<?php

namespace Rector\Symfony\Tests\Set\Symfony61\Fixture;

use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;

class SetDescriptionAtTop extends \Symfony\Component\Console\Command\Command
{
protected static $defaultDescription = 'Description';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why add this test for 6.1? It is just a copy of one of the CommandDescriptionToPropertyRector tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay so i added CommandPropertyToAttributeRector should i add a fixture of that test or should this be skipped as this is tested elsewhere?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you ask me, the expected refactor for this deprecation should be a static property to an attribute.

Using setName() and setDescription() is not deprecated. The Symfony documentation just prefers it the "new" way for optimizations:

Defining the $defaultDescription static property instead of using the setDescription() method allows to get the command description without instantiating its class. This makes the php bin/console list command run much faster.

https://symfony.com/doc/current/console.html#configuring-the-command

So my personal expectation for using this set would be a refactors like this:

-#[\Symfony\Component\Console\Attribute\AsCommand(name: 'some:command']
+#[\Symfony\Component\Console\Attribute\AsCommand(name: 'some:command', description: 'Description')]
class SetDescriptionAtTop extends \Symfony\Component\Console\Command\Command
{
-    protected static $defaultDescription = 'Description';
}

and

+#[\Symfony\Component\Console\Attribute\AsCommand(name: 'some:command', description: 'Description')]
class SetDescriptionAtTop extends \Symfony\Component\Console\Command\Command
{
-    protected static $defaultName = 'some:command';
-    protected static $defaultDescription = 'Description';
}

But that last example has already been covered by the tests for CommandPropertyToAttributeRector, however we want to test changes using SymfonySetList::SYMFONY_61.

Maybe @TomasVotruba can guide us here. Do we need duplicate tests when a Rector is used in a set to verify behaviour of the set?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe @TomasVotruba can guide us here. Do we need duplicate tests when a Rector is used in a set to verify behaviour of the set?

That's not necessary indeed. But the bug should be fixed, so CI pass.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Check i added missing stubs

public function configure()
{
$this
->setName('some:command');
}

protected function initialize(InputInterface $input, OutputInterface $output): void
{
parent::initialize($input, $output);
}

protected function execute(InputInterface $input, OutputInterface $output): int
{
return 0;
}
}

?>
29 changes: 29 additions & 0 deletions tests/Set/Symfony61/Symfony61Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony\Tests\Set\Symfony61;

use Iterator;
use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class Symfony61Test extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(string $filePath): void
{
$this->doTestFile($filePath);
}

public function provideData(): Iterator
{
return $this->yieldFilesFromDirectory(__DIR__ . '/Fixture');
}

public function provideConfigFilePath(): string
{
return __DIR__ . '/config/symfony61.php';
}
}
11 changes: 11 additions & 0 deletions tests/Set/Symfony61/config/symfony61.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Symfony\Set\SymfonySetList;

return static function (RectorConfig $rectorConfig): void {
$rectorConfig->import(__DIR__ . '/../../../../config/config.php');
$rectorConfig->sets([SymfonySetList::SYMFONY_61]);
};
0