|
5 | 5 | namespace Rector\CodingStyle\Rector\Closure; |
6 | 6 |
|
7 | 7 | use PhpParser\Node; |
8 | | -use PhpParser\Node\Expr\CallLike; |
9 | 8 | use PhpParser\Node\Expr\Closure; |
10 | | -use PhpParser\Node\Expr\FuncCall; |
11 | | -use PhpParser\Node\Expr\MethodCall; |
12 | | -use PhpParser\Node\Expr\StaticCall; |
13 | 9 | use Rector\CodingStyle\Guard\StaticGuard; |
14 | | -use Rector\NodeAnalyzer\CallLikeExpectsThisBindedClosureArgsAnalyzer; |
| 10 | +use Rector\NodeTypeResolver\Node\AttributeKey; |
| 11 | +use Rector\NodeTypeResolver\PHPStan\Scope\NodeVisitor\CallLikeThisBoundClosureArgsNodeVisitor; |
15 | 12 | use Rector\Rector\AbstractRector; |
16 | 13 | use Symplify\RuleDocGenerator\ValueObject\CodeSample\CodeSample; |
17 | 14 | use Symplify\RuleDocGenerator\ValueObject\RuleDefinition; |
|
21 | 18 | */ |
22 | 19 | final class StaticClosureRector extends AbstractRector |
23 | 20 | { |
24 | | - private const CLOSURE_USES_THIS = 'has_this_closure'; |
25 | | - |
26 | 21 | public function __construct( |
27 | 22 | private readonly StaticGuard $staticGuard, |
28 | | - private readonly CallLikeExpectsThisBindedClosureArgsAnalyzer $callLikeExpectsThisBindedClosureArgsAnalyzer |
29 | 23 | ) { |
30 | 24 | } |
31 | 25 |
|
@@ -67,47 +61,12 @@ public function getNodeTypes(): array |
67 | 61 | return [Closure::class]; |
68 | 62 | } |
69 | 63 |
|
70 | | - public function beforeTraverse(array $nodes): array |
71 | | - { |
72 | | - parent::beforeTraverse($nodes); |
73 | | - |
74 | | - $this->traverseNodesWithCallable($nodes, function (Node $node): ?CallLike { |
75 | | - if ( |
76 | | - ! $node instanceof MethodCall |
77 | | - && ! $node instanceof StaticCall |
78 | | - && ! $node instanceof FuncCall |
79 | | - ) { |
80 | | - return null; |
81 | | - } |
82 | | - |
83 | | - if ($node->isFirstClassCallable()) { |
84 | | - return null; |
85 | | - } |
86 | | - |
87 | | - $args = $this->callLikeExpectsThisBindedClosureArgsAnalyzer->getArgsUsingThisBindedClosure($node); |
88 | | - |
89 | | - if ($args === []) { |
90 | | - return null; |
91 | | - } |
92 | | - |
93 | | - foreach ($args as $arg) { |
94 | | - if ($arg->value instanceof Closure && ! $arg->hasAttribute(self::CLOSURE_USES_THIS)) { |
95 | | - $arg->value->setAttribute(self::CLOSURE_USES_THIS, true); |
96 | | - } |
97 | | - } |
98 | | - |
99 | | - return $node; |
100 | | - }); |
101 | | - |
102 | | - return $nodes; |
103 | | - } |
104 | | - |
105 | 64 | /** |
106 | 65 | * @param Closure $node |
107 | 66 | */ |
108 | 67 | public function refactor(Node $node): ?Node |
109 | 68 | { |
110 | | - if ($node->hasAttribute(self::CLOSURE_USES_THIS)) { |
| 69 | + if ($node->hasAttribute(AttributeKey::CLOSURE_USES_THIS)) { |
111 | 70 | return null; |
112 | 71 | } |
113 | 72 |
|
|
0 commit comments