rectorphp / rector-symfony

Rector upgrade rules for Symfony
http://getrector.com
MIT License
179 stars 86 forks source link

Update readme with RectorConfigBuilder #580

Closed stefantalen closed 7 months ago

stefantalen commented 7 months ago

I'm currently migrating from Rector 0.19.2 to 1.0 (🎉 ) and implementing the ConfigBuilder.

Unfortunately I'm running into issues with the AddRouteAnnotationRector because the singleton and alias methods are not available in the ConfigBuilder 🙂

Can the readme be updated with the use of the ConfigBuilder?

samsonasik commented 7 months ago

There is no shortcut yet for alias and singleton from RectorConfigBuilder for now, old config is still working, so you can use old config for now.

TomasVotruba commented 7 months ago

Hi @stefantalen , thanks for reporting :+1:

For running the AddRouteAnnotationRector it should not be needed to register any other services. We should fix that :) How does your Rector config look like now?

stefantalen commented 7 months ago

@samsonasik Thanks for the info, I'll keep this issue open as a reminder for the readme 😉

@TomasVotruba

return RectorConfig::configure()
    ->withSymfonyContainerXml(__DIR__ . '/var/cache/dev/srcApp_KernelDevDebugContainer.xml')
    ->withSymfonyContainerPhp(__DIR__ . '/tests/symfony-container.php')
    ->withPaths([
        //        __DIR__ . '/spec', // Skipping for now
        __DIR__ . '/src',
        //        __DIR__ . '/tests', // Skipping for now
        __DIR__ . '/public',
        __DIR__ . '/rector.php',
        __DIR__ . '/ecs.php',
    ])
    ->withImportNames(false, false, false)
    ->withPreparedSets(false, true, true)
    ->withPhpSets()
    ->withRules([
        InlineConstructorDefaultToPropertyRector::class,
        AddRouteAnnotationRector::class
    ])
    ->withSets([
        DoctrineSetList::DOCTRINE_CODE_QUALITY,
        DoctrineSetList::DOCTRINE_COMMON_20,
        DoctrineSetList::DOCTRINE_DBAL_211,
        DoctrineSetList::DOCTRINE_ORM_25,
        SymfonySetList::SYMFONY_41,
        SymfonySetList::SYMFONY_42,
        SymfonySetList::SYMFONY_43,
        SymfonySetList::SYMFONY_44,
        TwigSetList::TWIG_112,
        TwigSetList::TWIG_127,
        TwigSetList::TWIG_134,
        TwigSetList::TWIG_140,
    ])
    ->withConfiguredRule(RenameMethodRector::class, [
        new MethodCallRename('Doctrine\\DBAL\\Statement', 'execute', 'executeQuery'),
    ])
    ->withSkip([
        TypedPropertyFromAssignsRector::class,
        StringClassNameToClassConstantRector::class  => [
            __DIR__ . '/rector.php', # Keep class strings for BC
        ],
        OptionalParametersAfterRequiredRector::class => [
            __DIR__ . '/src/View/ViewHandler.php', # Type error: Argument 3 passed to App\View\ViewHandler::__construct() must be an instance of Symfony\Component\HttpFoundation\RequestStack, instance of Symfony\Bundle\TwigBundle\TwigEngine given
        ],
    ]);

This results in

/srv/app # vendor/bin/rector process src/Controller/ --dry-run --clear-cache
PHP Fatal error:  Uncaught RectorPrefix202402\Illuminate\Contracts\Container\BindingResolutionException: Target [Rector\Symfony\Contract\Bridge\Symfony\Routing\SymfonyRoutesProviderInterface] is not instantiable while building [Rector\Console\ConsoleApplication, Rector\Console\Command\ProcessCommand, Rector\Configuration\ConfigInitializer, Rector\Symfony\Configs\Rector\ClassMethod\AddRouteAnnotationRector]. in /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php:958
Stack trace:
#0 /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php(796): RectorPrefix202402\Illuminate\Container\Container->notInstantiable('Rector\\Symfony\\...')
#1 /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php(687): RectorPrefix202402\Illuminate\Container\Container->build('Rector\\Symfony\\...')
#2 /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php(634): RectorPrefix202402\Illuminate\Container\Container->resolve('Rector\\Symfony\\...', Array)
#3 /srv/app/vendor/rector/rector/v in /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php on line 958
Fatal error: Uncaught RectorPrefix202402\Illuminate\Contracts\Container\BindingResolutionException: Target [Rector\Symfony\Contract\Bridge\Symfony\Routing\SymfonyRoutesProviderInterface] is not instantiable while building [Rector\Console\ConsoleApplication, Rector\Console\Command\ProcessCommand, Rector\Configuration\ConfigInitializer, Rector\Symfony\Configs\Rector\ClassMethod\AddRouteAnnotationRector]. in /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php:958
Stack trace:
#0 /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php(796): RectorPrefix202402\Illuminate\Container\Container->notInstantiable('Rector\\Symfony\\...')
#1 /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php(687): RectorPrefix202402\Illuminate\Container\Container->build('Rector\\Symfony\\...')
#2 /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php(634): RectorPrefix202402\Illuminate\Container\Container->resolve('Rector\\Symfony\\...', Array)
#3 /srv/app/vendor/rector/rector/v in /srv/app/vendor/rector/rector/vendor/illuminate/container/Container.php on line 958
TomasVotruba commented 7 months ago

I see. Thanks :+1:

I'll check today if and how we can register the services for you, so the AddRouteAnnotationRector can be used without any extra load

TomasVotruba commented 7 months ago

@stefantalen I've added registerService() method to Rector dev-main. You can try it now :)

This is how it works: https://github.com/rectorphp/rector-symfony/pull/581/files#diff-15ac594ec2fd54c8cb3420119439ebd563938c10b37974cd8a2799d087cb6938

stefantalen commented 7 months ago

@TomasVotruba Works like charm, no more errors! Great service!

Too bad the rule turns out to be useless for my case (Refactoring FOSRestBundle "automatic" routes to Symfony Routes) 😭

TomasVotruba commented 7 months ago

Too bad the rule turns out to be useless for my case (Refactoring FOSRestBundle "automatic" routes to Symfony Routes) 😭

Ah, that's a pitty :cry: That would require a custom rule. But I think you can create it in less than an hour :muscle: https://getrector.com/documentation/custom-rule

stefantalen commented 7 months ago

Figured out how to get the most of it, routes are defined with a controller as a service definition.

Replacing the service with the actual class refactors most routes 👍

acme_users:
    type: rest
-    resource: "acme.controller.users"
+    resource: App\Controller\UserController