Skip to content

Commit cc2f664

Browse files
authored
[CodingStyle] Skip closure with rebound $this on ArrowFunction/ClosureDelegatingCallToFirstClassCallableRector (#8931)
1 parent 9e6021d commit cc2f664

7 files changed

Lines changed: 129 additions & 0 deletions

File tree

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\ArrowFunctionDelegatingCallToFirstClassCallableRector\Fixture;
4+
5+
final class Container9964
6+
{
7+
private array $definitions = [];
8+
9+
private function addDefinition(string $id, string $definition): void
10+
{
11+
$this->definitions[$id] = $definition;
12+
}
13+
}
14+
15+
final class ContainerTest9964
16+
{
17+
public function test(): void
18+
{
19+
$container = new Container9964();
20+
21+
(fn (string $id, string $definition) => $this->addDefinition($id, $definition))->call(
22+
$container,
23+
'test',
24+
'x',
25+
);
26+
}
27+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\ArrowFunction\ArrowFunctionDelegatingCallToFirstClassCallableRector\Fixture;
4+
5+
final class SkipReboundClosure
6+
{
7+
public function run(object $container)
8+
{
9+
(fn (string $id, string $definition) => $this->addDefinition($id, $definition))->call($container, 'test', 'x');
10+
11+
$bound = (fn (string $id, string $definition) => $this->addDefinition($id, $definition))->bindTo($container, $container);
12+
13+
$bound = \Closure::bind(fn (string $id, string $definition) => $this->addDefinition($id, $definition), $container, $container);
14+
}
15+
16+
private function addDefinition(string $id, string $definition): void
17+
{
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
3+
namespace Rector\Tests\CodingStyle\Rector\Closure\ClosureDelegatingCallToFirstClassCallableRector\Fixture;
4+
5+
use Closure;
6+
7+
final class SkipReboundClosure
8+
{
9+
public function run(object $container)
10+
{
11+
(function (string $id) {
12+
return $this->addDefinition($id);
13+
})->call($container, 'test');
14+
15+
$bound = (function (string $id) {
16+
return $this->addDefinition($id);
17+
})->bindTo($container, $container);
18+
19+
$bound = Closure::bind(function (string $id) {
20+
return $this->addDefinition($id);
21+
}, $container, $container);
22+
}
23+
24+
private function addDefinition(string $id): void
25+
{
26+
}
27+
}

‎rules/CodingStyle/Guard/ArrowFunctionAndClosureFirstClassCallableGuard.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,11 @@ public function shouldSkip(
5050
return true;
5151
}
5252

53+
// $this is rebound, e.g. (fn () => $this->method())->call($object)
54+
if ($arrowFunctionOrClosure->getAttribute(AttributeKey::IS_REBOUND_CLOSURE) === true) {
55+
return true;
56+
}
57+
5358
if ($arrowFunctionOrClosure->getAttribute(
5459
AttributeKey::IS_ASSIGNED_TO
5560
) === true || $arrowFunctionOrClosure->getAttribute(AttributeKey::IS_BEING_ASSIGNED)) {

‎src/DependencyInjection/LazyContainerFactory.php‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
use Rector\PhpParser\NodeVisitor\LocalVariableScopeNodeVisitor;
5353
use Rector\PhpParser\NodeVisitor\NameAndArgNodeVisitor;
5454
use Rector\PhpParser\NodeVisitor\PhpVersionConditionNodeVisitor;
55+
use Rector\PhpParser\NodeVisitor\ReboundClosureNodeVisitor;
5556
use Rector\PostRector\Application\PostFileProcessor;
5657
use Rector\Rector\AbstractRector;
5758
use Symfony\Component\Console\Application;
@@ -72,6 +73,7 @@ final class LazyContainerFactory
7273
LocalVariableScopeNodeVisitor::class,
7374
NameAndArgNodeVisitor::class,
7475
DefaultValueNodeVisitor::class,
76+
ReboundClosureNodeVisitor::class,
7577
];
7678

7779
/**

‎src/NodeTypeResolver/Node/AttributeKey.php‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,4 +169,9 @@ final class AttributeKey
169169
* e.g. an array callable passed to a "string|array|null" parameter
170170
*/
171171
public const string IS_ARG_NOT_ACCEPTING_CLOSURE = 'is_arg_not_accepting_closure';
172+
173+
/**
174+
* Closure or arrow function with rebound $this, e.g. (fn () => ...)->call($object)
175+
*/
176+
public const string IS_REBOUND_CLOSURE = 'is_rebound_closure';
172177
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Rector\PhpParser\NodeVisitor;
6+
7+
use PhpParser\Node;
8+
use PhpParser\Node\Expr\ArrowFunction;
9+
use PhpParser\Node\Expr\Closure;
10+
use PhpParser\Node\Expr\MethodCall;
11+
use PhpParser\Node\Identifier;
12+
use PhpParser\NodeVisitorAbstract;
13+
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
14+
use Rector\NodeTypeResolver\Node\AttributeKey;
15+
16+
/**
17+
* Marks closure/arrow function with rebound $this, e.g. (fn () => ...)->call($object)
18+
* @see https://github.com/rectorphp/rector/issues/9964
19+
*/
20+
final class ReboundClosureNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
21+
{
22+
public function enterNode(Node $node): ?Node
23+
{
24+
if (! $node instanceof MethodCall) {
25+
return null;
26+
}
27+
28+
if (! $node->var instanceof Closure && ! $node->var instanceof ArrowFunction) {
29+
return null;
30+
}
31+
32+
if (! $node->name instanceof Identifier) {
33+
return null;
34+
}
35+
36+
if (! in_array($node->name->toLowerString(), ['call', 'bindto'], true)) {
37+
return null;
38+
}
39+
40+
$node->var->setAttribute(AttributeKey::IS_REBOUND_CLOSURE, true);
41+
42+
return null;
43+
}
44+
}

0 commit comments

Comments
 (0)