Repository navigation
Expand file tree
/
Copy pathChangedNodeScopeRefresher.php
More file actions
258 lines (216 loc) · 9.07 KB
/
Copy pathChangedNodeScopeRefresher.php
File metadata and controls
258 lines (216 loc) · 9.07 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
247
248
249
250
251
252
253
254
255
256
257
258
<?php
declare(strict_types=1);
namespace Rector\Application;
use PhpParser\Modifiers;
use PhpParser\Node;
use PhpParser\Node\Arg;
use PhpParser\Node\ArrayItem;
use PhpParser\Node\Attribute;
use PhpParser\Node\AttributeGroup;
use PhpParser\Node\ClosureUse;
use PhpParser\Node\DeclareItem;
use PhpParser\Node\Expr;
use PhpParser\Node\Expr\Array_;
use PhpParser\Node\Expr\Closure;
use PhpParser\Node\Expr\ConstFetch;
use PhpParser\Node\Expr\New_;
use PhpParser\Node\Name;
use PhpParser\Node\Param;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\StaticVar;
use PhpParser\Node\Stmt;
use PhpParser\Node\Stmt\Catch_;
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassLike;
use PhpParser\Node\Stmt\Declare_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Enum_;
use PhpParser\Node\Stmt\Expression;
use PhpParser\Node\Stmt\If_;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Static_;
use PhpParser\Node\Stmt\TryCatch;
use PhpParser\Node\Stmt\Use_;
use PhpParser\Node\UseItem;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Reflection\ClassReflection;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeAnalyzer\ScopeAnalyzer;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\PHPStan\Scope\AstClassReflectionFactory;
use Rector\NodeTypeResolver\PHPStan\Scope\PHPStanNodeScopeResolver;
use Rector\NodeTypeResolver\PHPStan\Scope\ScopeFactory;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
/**
* In case of changed node, we need to re-traverse the PHPStan Scope to make all the new nodes aware of what is going on.
*/
final readonly class ChangedNodeScopeRefresher
{
public function __construct(
private PHPStanNodeScopeResolver $phpStanNodeScopeResolver,
private ScopeAnalyzer $scopeAnalyzer,
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
private AstClassReflectionFactory $astClassReflectionFactory,
private ScopeFactory $scopeFactory
) {
}
public function refresh(Node $node, string $filePath, ?MutatingScope $mutatingScope): void
{
// nothing to refresh
if (! $this->scopeAnalyzer->isRefreshable($node)) {
return;
}
if (! $mutatingScope instanceof MutatingScope) {
$errorMessage = sprintf('Node "%s" with is missing scope required for scope refresh', $node::class);
throw new ShouldNotHappenException($errorMessage);
}
/**
* The reindex is needed to:
* - be used by PHPStan processNodes() that relies on indexed arrays start from 0
* - use traverser to avoid issues when multiples rules apply, and higher node remove deep node,
* which the next rule use deep node, for example:
* - first rule: - Class_ → ClassMethod → remove stmt with index 0
* - second rule: - ClassMethod → here fetch the index 0 that no longer exists
*/
SimpleCallableNodeTraverser::traverse(
$node,
fn (Node $subNode): ?Node => NodeAttributeReIndexer::reIndexNodeAttributes($subNode)
);
// a class-like scope is already inside the class, so PHPStan cannot enter it again and would fail
// on the class node, leaving its methods with the class scope; process the class stmts directly instead
if ($node instanceof ClassLike && $mutatingScope->isInClass()) {
$mutatingScope = $this->refreshClassReflection($node, $mutatingScope);
// the class node itself is not part of its stmts, e.g. an Enum_ replaced by a new Class_ has no scope yet
$node->setAttribute(AttributeKey::SCOPE, $mutatingScope);
$this->phpStanNodeScopeResolver->processNodes($node->stmts, $filePath, $mutatingScope);
return;
}
$stmts = $this->resolveStmts($node);
$this->phpStanNodeScopeResolver->processNodes($stmts, $filePath, $mutatingScope);
}
/**
* The in-class scope holds the class reflection of the original file, which does not know about
* members the rule just added or removed; re-enter the class with the reflection of the changed node
*/
private function refreshClassReflection(ClassLike $classLike, MutatingScope $mutatingScope): MutatingScope
{
if (! $classLike instanceof Class_ && ! $classLike instanceof Interface_ && ! $classLike instanceof Enum_) {
return $mutatingScope;
}
if ($classLike instanceof Class_ && $classLike->isAnonymous()) {
return $mutatingScope;
}
$currentClassReflection = $mutatingScope->getClassReflection();
$className = (string) ($classLike->namespacedName ?? $classLike->name);
if (! $currentClassReflection instanceof ClassReflection || $currentClassReflection->getName() !== $className) {
return $mutatingScope;
}
$classReflection = $this->astClassReflectionFactory->create($classLike, $mutatingScope);
if (! $classReflection instanceof ClassReflection) {
return $mutatingScope;
}
$classOutsideScope = $this->scopeFactory->createFromFile($mutatingScope->getFile());
if ($mutatingScope->isDeclareStrictTypes()) {
$classOutsideScope = $classOutsideScope->enterDeclareStrictTypes();
}
$namespace = $mutatingScope->getNamespace();
if ($namespace !== null) {
$classOutsideScope = $classOutsideScope->enterNamespace($namespace);
}
return $classOutsideScope->enterClass($classReflection);
}
/**
* @return Stmt[]
*/
private function resolveStmts(Node $node): array
{
// Else_/ElseIf_ are not processed standalone by PHPStan, only as part of an If_, so their body
// stays unscoped; wrap into an If_ so processNodes() scopes the real statements they hold
if ($node instanceof Else_) {
return [
new If_(new ConstFetch(new Name('true')), [
'stmts' => $node->stmts,
])];
}
if ($node instanceof ElseIf_) {
return [
new If_($node->cond, [
'stmts' => $node->stmts,
])];
}
// Catch_ is not processed standalone by PHPStan, only as part of a TryCatch; an empty try
// makes it a catch no throw point reaches, which PHPStan still walks with the catch scope
if ($node instanceof Catch_) {
return [new TryCatch([], [$node])];
}
if ($node instanceof Stmt) {
return [$node];
}
if ($node instanceof Expr) {
return [new Expression($node)];
}
// moved from Expr/Stmt to directly under Node on PHPParser 5
if ($node instanceof ArrayItem) {
return [new Expression(new Array_([$node]))];
}
if ($node instanceof ClosureUse) {
$closure = new Closure();
$closure->uses[] = $node;
return [new Expression($closure)];
}
if ($node instanceof DeclareItem) {
return [new Declare_([$node])];
}
if ($node instanceof PropertyItem) {
return [new Property(Modifiers::PUBLIC, [$node])];
}
if ($node instanceof StaticVar) {
return [new Static_([$node])];
}
if ($node instanceof UseItem) {
return [new Use_([$node])];
}
if ($node instanceof Param) {
$closure = new Closure();
$closure->params[] = $node;
return [new Expression($closure)];
}
if ($node instanceof AttributeGroup) {
$class = new Class_(null);
$class->attrGroups[] = $node;
$this->setLineAttributesOnClass($class, $node);
return [$class];
}
if ($node instanceof Attribute) {
$class = new Class_(null);
$class->attrGroups[] = new AttributeGroup([$node]);
$this->setLineAttributesOnClass($class, $node);
return [$class];
}
if ($node instanceof Arg) {
$class = new Class_(null, [], [
'startLine' => $node->getStartLine(),
'endLine' => $node->getEndLine(),
]);
$new = new New_($class, [$node]);
return [new Expression($new)];
}
$errorMessage = sprintf('Complete parent node of "%s" be a stmt.', $node::class);
throw new ShouldNotHappenException($errorMessage);
}
private function setLineAttributesOnClass(Class_ $class, Attribute|AttributeGroup $node): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable([$class], function (Node $subNode) use (
$node
): Node {
if ($subNode->getStartLine() >= 0 && $subNode->getEndLine() >= 0) {
return $subNode;
}
$subNode->setAttribute('startLine', $node->getStartLine());
$subNode->setAttribute('endLine', $node->getEndLine());
return $subNode;
});
}
}