Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\ArrowFunctionDelegatingCallToFirstClassCallableRector\Fixture;

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

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

final class ContainerTest9964
{
public function test(): void
{
$container = new Container9964();

(fn (string $id, string $definition) => $this->addDefinition($id, $definition))->call(
$container,
'test',
'x',
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\ArrowFunctionDelegatingCallToFirstClassCallableRector\Fixture;

final class SkipReboundClosure
{
public function run(object $container)
{
(fn (string $id, string $definition) => $this->addDefinition($id, $definition))->call($container, 'test', 'x');

$bound = (fn (string $id, string $definition) => $this->addDefinition($id, $definition))->bindTo($container, $container);

$bound = \Closure::bind(fn (string $id, string $definition) => $this->addDefinition($id, $definition), $container, $container);
}

private function addDefinition(string $id, string $definition): void
{
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Rector\Tests\CodingStyle\Rector\Closure\ClosureDelegatingCallToFirstClassCallableRector\Fixture;

use Closure;

final class SkipReboundClosure
{
public function run(object $container)
{
(function (string $id) {
return $this->addDefinition($id);
})->call($container, 'test');

$bound = (function (string $id) {
return $this->addDefinition($id);
})->bindTo($container, $container);

$bound = Closure::bind(function (string $id) {
return $this->addDefinition($id);
}, $container, $container);
}

private function addDefinition(string $id): void
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@ public function shouldSkip(
return true;
}

// $this is rebound, e.g. (fn () => $this->method())->call($object)
if ($arrowFunctionOrClosure->getAttribute(AttributeKey::IS_REBOUND_CLOSURE) === true) {
return true;
}

if ($arrowFunctionOrClosure->getAttribute(
AttributeKey::IS_ASSIGNED_TO
) === true || $arrowFunctionOrClosure->getAttribute(AttributeKey::IS_BEING_ASSIGNED)) {
Expand Down
2 changes: 2 additions & 0 deletions src/DependencyInjection/LazyContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
use Rector\PhpParser\NodeVisitor\LocalVariableScopeNodeVisitor;
use Rector\PhpParser\NodeVisitor\NameAndArgNodeVisitor;
use Rector\PhpParser\NodeVisitor\PhpVersionConditionNodeVisitor;
use Rector\PhpParser\NodeVisitor\ReboundClosureNodeVisitor;
use Rector\PostRector\Application\PostFileProcessor;
use Rector\Rector\AbstractRector;
use Symfony\Component\Console\Application;
Expand All @@ -72,6 +73,7 @@ final class LazyContainerFactory
LocalVariableScopeNodeVisitor::class,
NameAndArgNodeVisitor::class,
DefaultValueNodeVisitor::class,
ReboundClosureNodeVisitor::class,
];

/**
Expand Down
5 changes: 5 additions & 0 deletions src/NodeTypeResolver/Node/AttributeKey.php
Original file line number Diff line number Diff line change
Expand Up @@ -169,4 +169,9 @@ final class AttributeKey
* e.g. an array callable passed to a "string|array|null" parameter
*/
public const string IS_ARG_NOT_ACCEPTING_CLOSURE = 'is_arg_not_accepting_closure';

/**
* Closure or arrow function with rebound $this, e.g. (fn () => ...)->call($object)
*/
public const string IS_REBOUND_CLOSURE = 'is_rebound_closure';
}
44 changes: 44 additions & 0 deletions src/PhpParser/NodeVisitor/ReboundClosureNodeVisitor.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace Rector\PhpParser\NodeVisitor;

use PhpParser\Node;
use PhpParser\Node\Expr\ArrowFunction;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Identifier;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;

/**
* Marks closure/arrow function with rebound $this, e.g. (fn () => ...)->call($object)
* @see https://github.com/rectorphp/rector/issues/9964
*/
final class ReboundClosureNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
public function enterNode(Node $node): ?Node
{
if (! $node instanceof MethodCall) {
return null;
}

if (! $node->var instanceof Closure && ! $node->var instanceof ArrowFunction) {
return null;
}

if (! $node->name instanceof Identifier) {
return null;
}

if (! in_array($node->name->toLowerString(), ['call', 'bindto'], true)) {
return null;
}

$node->var->setAttribute(AttributeKey::IS_REBOUND_CLOSURE, true);

return null;
}
}
Loading