Skip to content

[bug] FirstClassCallableRector invalid change in Symfony–related code #9412

@MacDada

Description

@MacDada

Bug Report

Subject Details
Rector version e.g. v2.1.7

It changes [RequestContext::class, 'fromUri'] into RequestContext::fromUri(...), but unfortunately in this case it is invalid.

After the change I'm getting this error:

Symfony\Component\DependencyInjection\Loader\Configurator\ServiceConfigurator::factory(): Argument #1 ($factory) must be of type Symfony\Component\DependencyInjection\Loader\Configurator\ReferenceConfigurator|Symfony\Component\ExpressionLanguage\Expression|array|string, Closure given, called in […]/routing.php on line 17

Minimal PHP Code Causing Issue

<?php

declare(strict_types=1);

use Symfony\Component\DependencyInjection\Loader\Configurator\ContainerConfigurator;
use Symfony\Component\Routing\RequestContext;

/**
 * routing.php
 */
return static function (ContainerConfigurator $container): void {
    $container->services()
        ->set('router.request_context', RequestContext::class)
        ->factory([RequestContext::class, 'fromUri'])
        ->args([
            '',
        ])
        ->alias(RequestContext::class, 'router.request_context');
};

Notice that factory method takes an array, but not \Closure nor callable: https://github.com/symfony/symfony/blob/v7.3.4/src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php#L25

Actually they allow \Closure in the newest (not released yet) 7.4 version: https://github.com/symfony/symfony/blob/866757551299596f8dfb778394da5bc27b53df95/src/Symfony/Component/DependencyInjection/Loader/Configurator/Traits/FactoryTrait.php#L25

But for now, the Rector's change is invalid.

Expected Behaviour

Keep [RequestContext::class, 'fromUri'] in this particular case.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions