Skip to content

ArrowFunctionDelegatingCallToFirstClassCallableRector breaks code that relies on $this rebinding via Closure::call() #9964

Closed
rectorphp/rector-src
#8931

Description

@vjik
Subject Details
Rector version v2.8.4 (invoke vendor/bin/rector --version)
PHP version 8.3 (the rule requires PHP 8.1+)

The ArrowFunctionDelegatingCallToFirstClassCallableRector rule rewrites an arrow function that only delegates to a method call into a first-class callable. When the delegated method is called on $this and the arrow function is rebound with Closure::call() (or Closure::bind()), the rewrite silently changes the semantics: a first-class callable such as $this->method(...) is bound to the current $this at creation time, while the original arrow function had its $this rebound by ->call($object, ...).

This pattern is typically used to invoke a private method of another object:

(fn (string $id, string $definition) => $this->addDefinition($id, $definition))->call(
    $container,
    'test',
    'x',
);

After Rector runs, the code throws a fatal error.

Minimal PHP Code Causing Issue

rector.php:

<?php

declare(strict_types=1);

use Rector\Config\RectorConfig;
use Rector\Php81\Rector\Array_\ArrayToFirstClassCallableRector;

return RectorConfig::configure()
    ->withPhpSets(php81: true)
    ->withSkip([
        ArrayToFirstClassCallableRector::class,
    ]);

before.php:

<?php

declare(strict_types=1);

final class Container
{
    private array $definitions = [];

    public function has(string $id): bool
    {
        return isset($this->definitions[$id]);
    }

    private function addDefinition(string $id, string $definition): void
    {
        $this->definitions[$id] = $definition;
    }
}

final class ContainerTest
{
    public function test(): bool
    {
        $container = new Container();

        // Invoke private method on $container by rebinding `$this`:
        (fn (string $id, string $definition) => $this->addDefinition($id, $definition))->call(
            $container,
            'test',
            'x',
        );

        return $container->has('test');
    }
}

var_dump((new ContainerTest())->test());

Rector produces:

-        (fn (string $id, string $definition) => $this->addDefinition($id, $definition))->call(
+        ($this->addDefinition(...))->call(
             $container,
             'test',
             'x',
         );

Running the rewritten code:

Fatal error: Uncaught Error: Call to undefined method ContainerTest::addDefinition()

The first-class callable $this->addDefinition(...) is resolved against ContainerTest (the class where the code is written) at creation time. The subsequent ->call($container, ...) rebinds $this of the wrapper closure, not of the already-created method closure, so Container::addDefinition() is never reached. Even in cases where the method happens to exist on the original class, the rewritten code would call the wrong object's method.

Expected Behaviour

Rector should skip such code. Running the original code prints bool(true); the rewrite breaks it.

The rule should not rewrite an arrow function whose delegated call targets $this when the arrow function itself is (or may be) rebound — for example when it is the direct receiver of a ->call(...) / ->bindTo(...) method call, or is passed to Closure::bind().

Extra context

This broke the CI of https://github.com/yiisoft/di PR #411: the Rector + PHP CS Fixer workflow auto-committed this rewrite into two existing tests that use exactly this pattern to call the private Container::addDefinition()
method:

https://github.com/yiisoft/di/blob/ef542e698436d30c3801869fe8e980063dcd3798/tests/Unit/ContainerTest.php#L678-L683

Resulting CI failure: Error: Call to undefined method Yiisoft\Di\Tests\Unit\ContainerTest::addDefinition()
(https://github.com/yiisoft/di/actions/runs/43491941216/job/132115527581).

Activity

  1. TomasVotruba commented on Nov 13, 2026

    @TomasVotruba
    Member

    Thank you 👍

    Fixed in rectorphp/rector-src#8931

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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