rector icon indicating copy to clipboard operation
rector copied to clipboard

Incorrect behavior of StringExtensionToConfigBuilderRector

Open Perf opened this issue 1 year ago • 2 comments

Bug Report

Subject Details
Rector version 1.0.0

I am trying to convert config/packages/security.php file from array based to ConfigBuilder based config.

Minimal PHP Code Causing Issue

See https://getrector.com/demo/b2db57ed-dd0e-4601-8dc8-0b2929cc037e

<?php

declare(strict_types=1);

use App\Consenta\Infrastructure\Symfony\Security\SecurityUserProvider;
use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface;

return static function (ContainerConfigurator $containerConfigurator): void {
    $containerConfigurator->extension('security', [
        'password_hashers' => [
            PasswordAuthenticatedUserInterface::class => 'auto',
        ],
        'providers' => [
            'app_user_provider' => [
                'id' => SecurityUserProvider::class,
            ],
        ],
        'firewalls' => [
            'dev' => [
                'pattern' => '^/(_(profiler|wdt)|css|images|js)/',
                'security' => false,
                'stateless' => false,
            ],
            'main' => [
                'lazy' => true,
                'provider' => 'app_user_provider',
                'stateless' => false,
                'json_login' => [
                    'check_path' => 'api_login',
                    'username_path' => 'email',
                    'password_path' => 'password',
                ],
            ],
        ],
        'access_control' => null,
    ]);
};

Responsible rules

  • StringExtensionToConfigBuilderRector

Produced code is invalid.

<?php

declare(strict_types=1);

return static function (\Symfony\Config\SecurityConfig $securityConfig) : void {
    $securityConfig->passwordHashers()->symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface('auto');
    $securityConfig->provider()->id('App\Consenta\Infrastructure\Symfony\Security\SecurityUserProvider');
    $securityConfig->firewall()->pattern('^/(_(profiler|wdt)|css|images|js)/')->security(false)->stateless(false);
    $securityConfig->firewall()->lazy(true)->provider('app_user_provider')->stateless(false)->jsonLogin()->checkPath('api_login')->usernamePath('email')->passwordPath('password');
};

For example, there is no method $securityConfig->passwordHashers(). The code following passwordHashers looks so wierd: ->symfony\Component\Security\Core\User\PasswordAuthenticatedUserInterface('auto'); Also, methods provider() and firewall() requires a parameter.

Expected Behavior

I do expect a valid ConfigBuilder based config should be generated. I am still trying to follow https://getrector.com/blog/modernize-symfony-configs

Perf avatar Feb 15 '24 11:02 Perf

Could you create a failing fixture with expected output on rector-symfony? Thank you.

samsonasik avatar Feb 17 '24 06:02 samsonasik

hey @samsonasik, potentially I could, but the thing is I never did this before. I was just trying to follow the guide https://getrector.com/blog/modernize-symfony-configs I never used Symfony ConfigBuilder class before.

Perf avatar Feb 20 '24 10:02 Perf

I think the safest is to skip this complex config, I created PR:

  • https://github.com/rectorphp/rector-symfony/pull/599

for it.

samsonasik avatar Apr 20 '24 19:04 samsonasik