-
-
Notifications
You must be signed in to change notification settings - Fork 439
Expand file tree
/
Copy pathAbstractRector.php
More file actions
377 lines (304 loc) · 12.2 KB
/
AbstractRector.php
File metadata and controls
377 lines (304 loc) · 12.2 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
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
<?php
declare(strict_types=1);
namespace Rector\Rector;
use PhpParser\Node;
use PhpParser\Node\Name;
use PhpParser\Node\PropertyItem;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\InlineHTML;
use PhpParser\Node\Stmt\Interface_;
use PhpParser\Node\Stmt\Nop;
use PhpParser\Node\Stmt\Property;
use PhpParser\Node\Stmt\Trait_;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Analyser\MutatingScope;
use PHPStan\Type\ObjectType;
use PHPStan\Type\Type;
use Rector\Application\ChangedNodeScopeRefresher;
use Rector\Application\NodeAttributeReIndexer;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\BetterPhpDocParser\PhpDocInfo\PhpDocInfo;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Contract\Rector\HTMLAverseRectorInterface;
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeDecorator\CreatedByRuleDecorator;
use Rector\NodeNameResolver\NodeNameResolver;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\NodeTypeResolver\NodeTypeResolver;
use Rector\PhpDocParser\NodeTraverser\SimpleCallableNodeTraverser;
use Rector\PhpParser\Comparing\NodeComparator;
use Rector\PhpParser\Node\NodeFactory;
use Rector\Skipper\Skipper\Skipper;
use Rector\ValueObject\Application\File;
abstract class AbstractRector extends NodeVisitorAbstract implements RectorInterface
{
/**
* @var string
*/
private const EMPTY_NODE_ARRAY_MESSAGE = <<<CODE_SAMPLE
Array of nodes cannot be empty. Ensure "%s->refactor()" returns non-empty array for Nodes.
A) Direct return null for no change:
return null;
B) Remove the Node:
return \PhpParser\NodeVisitor::REMOVE_NODE;
CODE_SAMPLE;
protected NodeNameResolver $nodeNameResolver;
protected NodeTypeResolver $nodeTypeResolver;
protected NodeFactory $nodeFactory;
protected NodeComparator $nodeComparator;
protected File $file;
protected Skipper $skipper;
private ChangedNodeScopeRefresher $changedNodeScopeRefresher;
private SimpleCallableNodeTraverser $simpleCallableNodeTraverser;
private CurrentFileProvider $currentFileProvider;
/**
* @var array<int, Node[]>
*/
private array $nodesToReturn = [];
private CreatedByRuleDecorator $createdByRuleDecorator;
private ?int $toBeRemovedNodeId = null;
public function autowire(
NodeNameResolver $nodeNameResolver,
NodeTypeResolver $nodeTypeResolver,
SimpleCallableNodeTraverser $simpleCallableNodeTraverser,
NodeFactory $nodeFactory,
Skipper $skipper,
NodeComparator $nodeComparator,
CurrentFileProvider $currentFileProvider,
CreatedByRuleDecorator $createdByRuleDecorator,
ChangedNodeScopeRefresher $changedNodeScopeRefresher,
): void {
$this->nodeNameResolver = $nodeNameResolver;
$this->nodeTypeResolver = $nodeTypeResolver;
$this->simpleCallableNodeTraverser = $simpleCallableNodeTraverser;
$this->nodeFactory = $nodeFactory;
$this->skipper = $skipper;
$this->nodeComparator = $nodeComparator;
$this->currentFileProvider = $currentFileProvider;
$this->createdByRuleDecorator = $createdByRuleDecorator;
$this->changedNodeScopeRefresher = $changedNodeScopeRefresher;
}
/**
* @return Node[]|null
*/
public function beforeTraverse(array $nodes): ?array
{
// workaround for file around refactor()
$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException(
'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.'
);
}
$this->file = $file;
return parent::beforeTraverse($nodes);
}
final public function enterNode(Node $node): int|Node|null
{
if (! $this->isMatchingNodeType($node)) {
return null;
}
if (is_a($this, HTMLAverseRectorInterface::class, true) && $this->file->containsHTML()) {
return null;
}
$filePath = $this->file->getFilePath();
if ($this->skipper->shouldSkipCurrentNode($this, $filePath, static::class, $node)) {
return null;
}
// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;
NodeAttributeReIndexer::reIndexNodeAttributes($node);
$refactoredNode = $this->refactor($node);
// @see NodeTraverser::* codes, e.g. removal of node of stopping the traversing
if ($refactoredNode === NodeVisitor::REMOVE_NODE) {
$this->toBeRemovedNodeId = spl_object_id($originalNode);
// notify this rule changing code
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
return $originalNode;
}
if (is_int($refactoredNode)) {
$this->createdByRuleDecorator->decorate($node, $originalNode, static::class);
if (! in_array(
$refactoredNode,
[NodeVisitor::DONT_TRAVERSE_CHILDREN, NodeVisitor::DONT_TRAVERSE_CURRENT_AND_CHILDREN],
true
)) {
// notify this rule changing code
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
return $refactoredNode;
}
$this->decorateCurrentAndChildren($node);
return null;
}
// nothing to change → continue
if ($refactoredNode === null) {
return null;
}
if ($refactoredNode === []) {
$errorMessage = sprintf(self::EMPTY_NODE_ARRAY_MESSAGE, static::class);
throw new ShouldNotHappenException($errorMessage);
}
return $this->postRefactorProcess($originalNode, $node, $refactoredNode, $filePath);
}
/**
* Replacing nodes in leaveNode() method avoids infinite recursion
* see"infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
*/
final public function leaveNode(Node $node): array|int|Node|null
{
if ($node->hasAttribute(AttributeKey::ORIGINAL_NODE)) {
return null;
}
$objectId = spl_object_id($node);
if ($this->toBeRemovedNodeId === $objectId) {
$this->toBeRemovedNodeId = null;
return NodeVisitor::REMOVE_NODE;
}
return $this->nodesToReturn[$objectId] ?? $node;
}
protected function isName(Node $node, string $name): bool
{
return $this->nodeNameResolver->isName($node, $name);
}
/**
* @param string[] $names
*/
protected function isNames(Node $node, array $names): bool
{
return $this->nodeNameResolver->isNames($node, $names);
}
/**
* Some nodes have always-known string name. This makes PHPStan smarter.
* @see https://phpstan.org/writing-php-code/phpdoc-types#conditional-return-types
*
* @return ($node is Node\Param ? string :
* ($node is ClassMethod ? string :
* ($node is Property ? string :
* ($node is PropertyItem ? string :
* ($node is Trait_ ? string :
* ($node is Interface_ ? string :
* ($node is Const_ ? string :
* ($node is Node\Const_ ? string :
* ($node is Name ? string :
* string|null )))))))))
*/
protected function getName(Node $node): ?string
{
return $this->nodeNameResolver->getName($node);
}
protected function isObjectType(Node $node, ObjectType $objectType): bool
{
return $this->nodeTypeResolver->isObjectType($node, $objectType);
}
/**
* Use this method for getting expr|node type
*/
protected function getType(Node $node): Type
{
return $this->nodeTypeResolver->getType($node);
}
/**
* @param Node|Node[] $nodes
* @param callable(Node): (int|Node|null|Node[]) $callable
*/
protected function traverseNodesWithCallable(Node | array $nodes, callable $callable): void
{
$this->simpleCallableNodeTraverser->traverseNodesWithCallable($nodes, $callable);
}
protected function mirrorComments(Node $newNode, Node $oldNode): void
{
if ($this->nodeComparator->areSameNode($newNode, $oldNode)) {
return;
}
if ($oldNode instanceof InlineHTML) {
return;
}
$oldPhpDocInfo = $oldNode->getAttribute(AttributeKey::PHP_DOC_INFO);
$newPhpDocInfo = $newNode->getAttribute(AttributeKey::PHP_DOC_INFO);
if ($newPhpDocInfo instanceof PhpDocInfo) {
if (! $oldPhpDocInfo instanceof PhpDocInfo) {
return;
}
if ((string) $oldPhpDocInfo->getPhpDocNode() !== (string) $newPhpDocInfo->getPhpDocNode()) {
return;
}
}
$newNode->setAttribute(AttributeKey::PHP_DOC_INFO, $oldPhpDocInfo);
if (! $newNode instanceof Nop) {
$newNode->setAttribute(AttributeKey::COMMENTS, $oldNode->getAttribute(AttributeKey::COMMENTS));
}
}
private function decorateCurrentAndChildren(Node $node): void
{
// filter only types that
// 1. registered in getNodesTypes() method
// 2. different with current node type, as already decorated above
//
$otherTypes = array_filter(
$this->getNodeTypes(),
static fn (string $nodeType): bool => $nodeType !== $node::class
);
if ($otherTypes === []) {
return;
}
$this->traverseNodesWithCallable($node, static function (Node $subNode) use ($otherTypes): null {
if (in_array($subNode::class, $otherTypes, true)) {
$subNode->setAttribute(AttributeKey::SKIPPED_BY_RECTOR_RULE, static::class);
}
return null;
});
}
/**
* @param Node|Node[] $refactoredNode
*/
private function postRefactorProcess(
Node $originalNode,
Node $node,
Node|array|int $refactoredNode,
string $filePath
): Node {
/** @var non-empty-array<Node>|Node $refactoredNode */
$this->createdByRuleDecorator->decorate($refactoredNode, $originalNode, static::class);
$rectorWithLineChange = new RectorWithLineChange(static::class, $originalNode->getStartLine());
$this->file->addRectorClassWithLine($rectorWithLineChange);
/** @var MutatingScope|null $currentScope */
$currentScope = $node->getAttribute(AttributeKey::SCOPE);
if (is_array($refactoredNode)) {
$firstNode = current($refactoredNode);
$this->mirrorComments($firstNode, $originalNode);
$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);
// search "infinite recursion" in https://github.com/nikic/PHP-Parser/blob/master/doc/component/Walking_the_AST.markdown
$originalNodeId = spl_object_id($originalNode);
// will be replaced in leaveNode() the original node must be passed
$this->nodesToReturn[$originalNodeId] = $refactoredNode;
return $originalNode;
}
$this->refreshScopeNodes($refactoredNode, $filePath, $currentScope);
return $refactoredNode;
}
/**
* @param Node[]|Node $node
*/
private function refreshScopeNodes(array | Node $node, string $filePath, ?MutatingScope $mutatingScope): void
{
$nodes = $node instanceof Node ? [$node] : $node;
foreach ($nodes as $node) {
$this->changedNodeScopeRefresher->refresh($node, $filePath, $mutatingScope);
}
}
private function isMatchingNodeType(Node $node): bool
{
$nodeClass = $node::class;
foreach ($this->getNodeTypes() as $nodeType) {
if (is_a($nodeClass, $nodeType, true)) {
return true;
}
}
return false;
}
}