-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathContextNodeVisitor.php
More file actions
246 lines (206 loc) · 8.24 KB
/
ContextNodeVisitor.php
File metadata and controls
246 lines (206 loc) · 8.24 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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
<?php
declare(strict_types=1);
namespace Rector\PhpParser\NodeVisitor;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\Attribute;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\ArrayDimFetch;
use PhpParser\Node\Expr\BinaryOp\BooleanAnd;
use PhpParser\Node\Expr\CallLike;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\Isset_;
use PhpParser\Node\Expr\PostDec;
use PhpParser\Node\Expr\PostInc;
use PhpParser\Node\Expr\PreDec;
use PhpParser\Node\Expr\PreInc;
use PhpParser\Node\Expr\PropertyFetch;
use PhpParser\Node\Expr\StaticPropertyFetch;
use PhpParser\Node\Expr\Variable;
use PhpParser\Node\Identifier;
use PhpParser\Node\Name\FullyQualified;
use PhpParser\Node\Param;
use PhpParser\Node\Stmt\Break_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\Do_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\For_;
use PhpParser\Node\Stmt\Foreach_;
use PhpParser\Node\Stmt\Function_;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Switch_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\Unset_;
use PhpParser\Node\Stmt\While_;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\Scope;
use PHPStan\Reflection\MethodReflection;
use Rector\Contract\PhpParser\DecoratingNodeVisitorInterface;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\ParametersAcceptorSelectorVariantsWrapper;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\NodeTraverser\SimpleNodeTraverser;
use Rector\Reflection\ReflectionResolver;
final class ContextNodeVisitor extends NodeVisitorAbstract implements DecoratingNodeVisitorInterface
{
public function __construct(
private readonly SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private readonly ReflectionResolver $reflectionResolver
) {
}
public function enterNode(Node $node): ?Node
{
if ($node instanceof For_ || $node instanceof Foreach_ || $node instanceof While_ || $node instanceof Do_ || $node instanceof Switch_) {
$this->processContextInLoop($node);
return null;
}
if ($node instanceof ArrayDimFetch) {
$this->processInsideArrayDimFetch($node);
return null;
}
if ($node instanceof Unset_) {
foreach ($node->vars as $var) {
$var->setAttribute(AttributeKey::IS_UNSET_VAR, true);
}
return null;
}
if ($node instanceof TryCatch) {
SimpleNodeTraverser::decorateWithAttributeValue($node->stmts, AttributeKey::IS_IN_TRY_BLOCK, true);
return null;
}
if ($node instanceof Isset_) {
foreach ($node->vars as $var) {
$var->setAttribute(AttributeKey::IS_ISSET_VAR, true);
}
return null;
}
if ($node instanceof Attribute) {
$this->processContextInAttribute($node);
return null;
}
if ($node instanceof If_ || $node instanceof Else_ || $node instanceof ElseIf_) {
$this->processContextInIf($node);
return null;
}
if ($node instanceof CallLike && ! $node->isFirstClassCallable()) {
$functionReflection = $this->reflectionResolver->resolveFunctionLikeReflectionFromCall($node);
if (! $functionReflection instanceof MethodReflection) {
return null;
}
if ($functionReflection->getDeclaringClass()->getName() !== 'Symfony\Component\DependencyInjection\Loader\Configurator\ServiceConfigurator') {
return null;
}
if ($functionReflection->getName() !== 'factory') {
return null;
}
$scope = $node->getAttribute(AttributeKey::SCOPE);
if (! $scope instanceof Scope) {
return null;
}
$parametersAcceptor = ParametersAcceptorSelectorVariantsWrapper::select(
$functionReflection,
$node,
$scope
);
$args = $node->getArgs();
foreach ($parametersAcceptor->getParameters() as $key => $parameterReflection) {
if ($parameterReflection->getName() !== 'factory') {
continue;
}
// based on name
foreach ($args as $arg) {
if ($arg->name instanceof Identifier && 'factory' === $arg->name->toString()) {
$arg->value->setAttribute(AttributeKey::IS_ARG_VALUE_FACTORY_SERVICECONFIGURATOR, true);
continue 2;
}
}
// based on key
if (isset($args[$key])) {
$args[$key]->value->setAttribute(AttributeKey::IS_ARG_VALUE_FACTORY_SERVICECONFIGURATOR, true);
}
}
}
if ($node instanceof Arg) {
$node->value->setAttribute(AttributeKey::IS_ARG_VALUE, true);
return null;
}
if ($node instanceof Param) {
$node->var->setAttribute(AttributeKey::IS_PARAM_VAR, true);
return null;
}
if ($node instanceof PostDec || $node instanceof PostInc || $node instanceof PreDec || $node instanceof PreInc) {
$node->var->setAttribute(AttributeKey::IS_INCREMENT_OR_DECREMENT, true);
return null;
}
if ($node instanceof BooleanAnd) {
$node->right->setAttribute(AttributeKey::IS_RIGHT_AND, true);
return null;
}
$this->processContextInClass($node);
return null;
}
private function processInsideArrayDimFetch(ArrayDimFetch $arrayDimFetch): void
{
if ($arrayDimFetch->var instanceof PropertyFetch || $arrayDimFetch->var instanceof StaticPropertyFetch) {
$arrayDimFetch->var->setAttribute(AttributeKey::INSIDE_ARRAY_DIM_FETCH, true);
}
}
private function processContextInClass(Node $node): void
{
if ($node instanceof Class_) {
if ($node->extends instanceof FullyQualified) {
$node->extends->setAttribute(AttributeKey::IS_CLASS_EXTENDS, true);
}
foreach ($node->implements as $implement) {
$implement->setAttribute(AttributeKey::IS_CLASS_IMPLEMENT, true);
}
}
}
private function processContextInAttribute(Attribute $attribute): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$attribute->args,
static function (Node $subNode): null {
if ($subNode instanceof Array_) {
$subNode->setAttribute(AttributeKey::IS_ARRAY_IN_ATTRIBUTE, true);
}
if ($subNode instanceof Closure) {
$subNode->setAttribute(AttributeKey::IS_CLOSURE_IN_ATTRIBUTE, true);
}
return null;
}
);
}
private function processContextInIf(If_|Else_|ElseIf_ $node): void
{
foreach ($node->stmts as $stmt) {
if ($stmt instanceof Break_) {
$stmt->setAttribute(AttributeKey::IS_IN_IF, true);
}
}
}
private function processContextInLoop(For_|Foreach_|While_|Do_|Switch_ $node): void
{
if ($node instanceof Foreach_) {
if ($node->keyVar instanceof Variable) {
$node->keyVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
}
$node->valueVar->setAttribute(AttributeKey::IS_VARIABLE_LOOP, true);
}
$stmts = $node instanceof Switch_ ? $node->cases : $node->stmts;
$this->simpleCallableNodeTraverser->traverseNodesWithCallable(
$stmts,
static function (Node $subNode): ?int {
if ($subNode instanceof Class_ || $subNode instanceof Function_ || $subNode instanceof Closure) {
return NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($subNode instanceof If_ || $subNode instanceof Break_) {
$subNode->setAttribute(AttributeKey::IS_IN_LOOP_OR_SWITCH, true);
}
return null;
}
);
}
}