forked from rectorphp/rector-src
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCallLikeThisBoundClosureArgsNodeVisitor.php
More file actions
52 lines (42 loc) · 1.48 KB
/
CallLikeThisBoundClosureArgsNodeVisitor.php
File metadata and controls
52 lines (42 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<?php
declare(strict_types=1);
namespace Rector\PhpParser\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\FuncCall;
use PhpParser\Node\Expr\MethodCall;
use PhpParser\Node\Expr\StaticCall;
use PhpParser\NodeVisitorAbstract;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeAnalyzer\CallLikeExpectsThisBoundClosureArgsAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
final class CallLikeThisBoundClosureArgsNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
public function __construct(
private readonly CallLikeExpectsThisBoundClosureArgsAnalyzer $callLikeExpectsThisBindedClosureArgsAnalyzer
) {
}
public function enterNode(Node $node): ?Node
{
if (
! $node instanceof MethodCall
&& ! $node instanceof StaticCall
&& ! $node instanceof FuncCall
) {
return null;
}
if ($node->isFirstClassCallable()) {
return null;
}
$args = $this->callLikeExpectsThisBindedClosureArgsAnalyzer->getArgsUsingThisBoundClosure($node);
if ($args === []) {
return null;
}
foreach ($args as $arg) {
if ($arg->value instanceof Closure && ! $arg->hasAttribute(AttributeKey::IS_CLOSURE_USES_THIS)) {
$arg->value->setAttribute(AttributeKey::IS_CLOSURE_USES_THIS, true);
}
}
return $node;
}
}