Repository navigation
Description
| 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:
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
TomasVotruba commentedon Nov 13, 2026
Thank you 👍
Fixed in rectorphp/rector-src#8931