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 5.1] Add RouteCollectionBuilderToRoutingConfiguratorRector #5664

Closed
wants to merge 6 commits into from
Closed
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
18 changes: 5 additions & 13 deletions config/set/symfony51.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

declare(strict_types=1);

# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.1.md
use PHPStan\Type\ObjectType;
use Rector\Renaming\Rector\ClassConstFetch\RenameClassConstFetchRector;
use Rector\Renaming\Rector\FuncCall\RenameFunctionRector;
Expand All @@ -12,6 +13,7 @@
use Rector\Renaming\ValueObject\RenameClassAndConstFetch;
use Rector\Symfony5\Rector\Class_\LogoutHandlerToLogoutEventSubscriberRector;
use Rector\Symfony5\Rector\Class_\LogoutSuccessHandlerToLogoutEventSubscriberRector;
use Rector\Symfony5\Rector\ClassMethod\RouteCollectionBuilderToRoutingConfiguratorRector;
use Rector\Transform\Rector\New_\NewArgToMethodCallRector;
use Rector\Transform\Rector\StaticCall\StaticCallToNewRector;
use Rector\Transform\ValueObject\NewArgToMethodCall;
Expand All @@ -21,15 +23,11 @@
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\SymfonyPhpConfig\ValueObjectInliner;

# https://github.com/symfony/symfony/blob/5.x/UPGRADE-5.1.md

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();

// see https://github.com/symfony/symfony/pull/36243
$services->set(LogoutHandlerToLogoutEventSubscriberRector::class);
$services->set(LogoutSuccessHandlerToLogoutEventSubscriberRector::class);

$services->set(RenameClassRector::class)
->call('configure', [[
RenameClassRector::OLD_TO_NEW_CLASSES => [
Expand All @@ -39,7 +37,6 @@
'Symfony\Component\Inflector' => 'Symfony\Component\String\Inflector\InflectorInterface',
],
]]);

$services->set(RenameMethodRector::class)
->call('configure', [[
RenameMethodRector::METHOD_CALL_RENAMES => ValueObjectInliner::inline([
Expand All @@ -60,23 +57,20 @@
),
]),
]]);

$services->set(RenameFunctionRector::class)
->call('configure', [[
RenameFunctionRector::OLD_FUNCTION_TO_NEW_FUNCTION => [
'Symfony\Component\DependencyInjection\Loader\Configuraton\inline' => 'Symfony\Component\DependencyInjection\Loader\Configuraton\inline_service',
'Symfony\Component\DependencyInjection\Loader\Configuraton\ref' => 'Symfony\Component\DependencyInjection\Loader\Configuraton\service',
],
]]);

// https://github.com/symfony/symfony/pull/35308
$services->set(NewArgToMethodCallRector::class)
->call('configure', [[
NewArgToMethodCallRector::NEW_ARGS_TO_METHOD_CALLS => ValueObjectInliner::inline([
new NewArgToMethodCall('Symfony\Component\Dotenv\Dotenv', true, 'usePutenv'),
new NewArgToMethodCall('Symfony\Component\Dotenv\Dotenv', \true, 'usePutenv'),
]),
]]);

$services->set(RenameClassConstFetchRector::class)
->call('configure', [[
RenameClassConstFetchRector::CLASS_CONSTANT_RENAME => ValueObjectInliner::inline([
Expand Down Expand Up @@ -124,7 +118,6 @@
),
]),
]]);

$services->set(AddParamTypeDeclarationRector::class)
->call('configure', [[
// see https://github.com/symfony/symfony/pull/36943
Expand All @@ -133,11 +126,10 @@
'Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait',
'configureRoutes',
0,
new ObjectType('Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator'),
new ObjectType('Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator')
),
]),
]]);

$services->set(StaticCallToNewRector::class)
->call('configure', [[
StaticCallToNewRector::STATIC_CALLS_TO_NEWS => ValueObjectInliner::inline([
Expand All @@ -147,12 +139,12 @@
new StaticCallToNew('Symfony\Component\HttpFoundation\StreamedResponse', 'create'),
]),
]]);

$services->set(RenameStringRector::class)
->call('configure', [[
// @see https://github.com/symfony/symfony/pull/35858
RenameStringRector::STRING_CHANGES => [
'ROLE_PREVIOUS_ADMIN' => 'IS_IMPERSONATOR',
],
]]);
$services->set(RouteCollectionBuilderToRoutingConfiguratorRector::class);
};
13 changes: 5 additions & 8 deletions ecs.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,25 +11,18 @@
use PhpCsFixer\Fixer\ReturnNotation\ReturnAssignmentFixer;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayListItemNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\ArrayOpenerAndCloserNewlineFixer;
use Symplify\CodingStandard\Fixer\ArrayNotation\StandaloneLineInMultilineArrayFixer;
use Symplify\CodingStandard\Fixer\Commenting\RemoveCommentedCodeFixer;
use Symplify\CodingStandard\Fixer\LineLength\LineLengthFixer;
use Symplify\EasyCodingStandard\ValueObject\Option;
use Symplify\EasyCodingStandard\ValueObject\Set\SetList;

return static function (ContainerConfigurator $containerConfigurator): void {
$services = $containerConfigurator->services();
$services->set(StandaloneLineInMultilineArrayFixer::class);
$services->set(ArrayOpenerAndCloserNewlineFixer::class);

$services->set(GeneralPhpdocAnnotationRemoveFixer::class)
->call('configure', [[
'annotations' => ['throws', 'author', 'package', 'group'],
]]);

$services->set(LineLengthFixer::class);

$services->set(NoSuperfluousPhpdocTagsFixer::class)
->call('configure', [[
'allow_mixed' => true,
Expand All @@ -56,13 +49,15 @@
'*/Source/*',
'*/Fixture/*',
'*/Expected/*',
# generated from /vendor

// generated from /vendor
__DIR__ . '/packages/doctrine-annotation-generated/src/ConstantPreservingDocParser.php',
__DIR__ . '/packages/doctrine-annotation-generated/src/ConstantPreservingAnnotationReader.php',
// template files
__DIR__ . '/packages/rector-generator/templates',

UnaryOperatorSpacesFixer::class,

// buggy with specific markdown snippet file in docs/rules_overview.md
ArrayListItemNewlineFixer::class,
BlankLineAfterOpeningTagFixer::class,
Expand All @@ -73,7 +68,9 @@
// breaks on-purpose annotated variables
ReturnAssignmentFixer::class,

// buggy on "Float" class
PhpdocTypesFixer::class => [__DIR__ . '/rules/php74/src/Rector/Double/RealToFloatTypeCastRector.php'],

PhpUnitStrictFixer::class => [
__DIR__ . '/packages/better-php-doc-parser/tests/PhpDocInfo/PhpDocInfo/PhpDocInfoTest.php',
__DIR__ . '/tests/PhpParser/Node/NodeFactoryTest.php',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ public function __construct(ParameterProvider $parameterProvider)
$additionalConfigFiles[] = __DIR__ . '/../../config/phpstan/better-infer.neon';

$existingAdditionalConfigFiles = array_filter($additionalConfigFiles, 'file_exists');

$this->container = $containerFactory->create(sys_get_temp_dir(), $existingAdditionalConfigFiles, []);
}

Expand Down
5 changes: 5 additions & 0 deletions packages/set/src/ValueObject/SetList.php
Original file line number Diff line number Diff line change
Expand Up @@ -618,6 +618,11 @@ final class SetList implements SetListInterface
*/
public const SYMFONY_50_TYPES = __DIR__ . '/../../../../config/set/symfony50-types.php';

/**
* @var string
*/
public const SYMFONY_51 = __DIR__ . '/../../../../config/set/symfony51.php';

/**
* @var string
*/
Expand Down
14 changes: 14 additions & 0 deletions packages/testing/src/PHPUnit/AbstractRectorTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,20 @@ protected function doTestFileInfo(SmartFileInfo $fixtureFileInfo, array $extraFi
$dynamicDirectoryLocatorProvider = $this->getService(DynamicSourceLocatorProvider::class);
$dynamicDirectoryLocatorProvider->setFileInfo($inputFileInfo);

/** @var ReflectionProvider\MemoizingReflectionProvider $reflectionProvider */
$reflectionProvider = $this->getService(ReflectionProvider::class);

$privatesAccessor = new PrivatesAccessor();
$betterReflectionProvider = $privatesAccessor->getPrivateProperty($reflectionProvider, 'provider');
if (! $betterReflectionProvider instanceof BetterReflectionProvider) {
throw new ShouldNotHappenException();
}

$classReflector = $privatesAccessor->getPrivateProperty($betterReflectionProvider, 'classReflector');
if (! $classReflector instanceof MemoizingClassReflector) {
throw new ShouldNotHappenException();
}

$expectedFileInfo = $inputFileInfoAndExpectedFileInfo->getExpectedFileInfo();

$this->doTestFileMatchesExpectedContent($inputFileInfo, $expectedFileInfo, $fixtureFileInfo, $extraFileInfos);
Expand Down
4 changes: 4 additions & 0 deletions phpstan-for-rector.neon
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ parameters:
scanDirectories:
- stubs

# see https://github.com/rectorphp/rector/issues/3490#issue-634342324
featureToggles:
disableRuntimeReflectionProvider: true

includes:
- utils/phpstan-extensions/config/phpstan-extensions.neon
- vendor/phpstan/phpstan-nette/extension.neon
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
<?php

declare(strict_types=1);

namespace Rector\Symfony5\Rector\ClassMethod;

use PhpParser\Node;
use Rector\Core\Rector\AbstractRector;
use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample;
use Symplify\RuleDocGenerator\ValueObject\RuleDefinition;

/**
* @see https://github.com/symfony/symfony/pull/32937/files
*
* @see \Rector\Symfony5\Tests\Rector\ClassMethod\RouteCollectionBuilderToRoutingConfiguratorRector\RouteCollectionBuilderToRoutingConfiguratorRectorTest
*/
final class RouteCollectionBuilderToRoutingConfiguratorRector extends AbstractRector
{
public function getRuleDefinition(): RuleDefinition
{
return new RuleDefinition('Change RouteCollectionBuilder to RoutingConfiguratorRector', [
new CodeSample(
<<<'CODE_SAMPLE'
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

final class ConcreteMicroKernel extends Kernel
{
use MicroKernelTrait;

protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/', 'kernel::halloweenAction');
$routes->add('/danger', 'kernel::dangerousAction');
}
}
CODE_SAMPLE

,
<<<'CODE_SAMPLE'
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

final class ConcreteMicroKernel extends Kernel
{
use MicroKernelTrait;

protected function configureRouting(RoutingConfigurator $routes): void
{
$routes->add('halloween', '/')->controller('kernel::halloweenAction');
$routes->add('danger', '/danger')->controller('kernel::dangerousAction');
}
}
CODE_SAMPLE

),
]);
}

/**
* @return string[]
*/
public function getNodeTypes(): array
{
return [\PhpParser\Node\Stmt\ClassMethod::class];
}

/**
* @param \PhpParser\Node\Stmt\ClassMethod $node
*/
public function refactor(Node $node): ?Node
{
// change the node

return $node;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

namespace Rector\Symfony5\Tests\Rector\ClassMethod\RouteCollectionBuilderToRoutingConfiguratorRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Bundle\BundleInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\RouteCollectionBuilder;

final class ConcreteMicroKernel extends Kernel
{
use MicroKernelTrait;

protected function configureRoutes(RouteCollectionBuilder $routes)
{
$routes->add('/', 'kernel::halloweenAction');
$routes->add('/danger', 'kernel::dangerousAction');
}

public function registerBundles()
{
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
}
}

?>
-----
<?php

namespace Rector\Symfony5\Tests\Rector\ClassMethod\RouteCollectionBuilderToRoutingConfiguratorRector\Fixture;

use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Routing\Loader\Configurator\RoutingConfigurator;

final class ConcreteMicroKernel extends Kernel
{
use MicroKernelTrait;

protected function configureRouting(RoutingConfigurator $routes): void
{
$routes->add('halloween', '/')->controller('kernel::halloweenAction');
$routes->add('danger', '/danger')->controller('kernel::dangerousAction');
}

public function registerBundles()
{
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
}
}

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

declare(strict_types=1);

namespace Rector\Symfony5\Tests\Rector\ClassMethod\RouteCollectionBuilderToRoutingConfiguratorRector;

use Rector\Testing\PHPUnit\AbstractRectorTestCase;

final class RouteCollectionBuilderToRoutingConfiguratorRectorTest extends AbstractRectorTestCase
{
/**
* @dataProvider provideData()
*/
public function test(\Symplify\SmartFileSystem\SmartFileInfo $fileInfo): void
{
$this->doTestFileInfo($fileInfo);
}

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

protected function getRectorClass(): string
{
return \Rector\Symfony5\Rector\ClassMethod\RouteCollectionBuilderToRoutingConfiguratorRector::class;
}
}
13 changes: 13 additions & 0 deletions stubs/Symfony/Bundle/FrameworkBundle/Kernel/MicroKernelTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Symfony\Bundle\FrameworkBundle\Kernel;

if (class_exists('Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait')) {
return;
}

trait MicroKernelTrait
{
}