Repository navigation
Expand file tree
/
Copy pathRectorRunner.php
More file actions
129 lines (104 loc) · 4.21 KB
/
Copy pathRectorRunner.php
File metadata and controls
129 lines (104 loc) · 4.21 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
<?php
declare(strict_types=1);
namespace Rector\Rector;
use PhpParser\Node;
use PhpParser\NodeVisitor;
use PHPStan\Analyser\MutatingScope;
use Rector\Application\ChangedNodeScopeRefresher;
use Rector\Application\Provider\CurrentFileProvider;
use Rector\ChangesReporting\ValueObject\RectorWithLineChange;
use Rector\Contract\Rector\RectorInterface;
use Rector\Exception\ShouldNotHappenException;
use Rector\NodeTypeResolver\Node\AttributeKey;
use Rector\ValueObject\Application\File;
/**
* Runs a single Rector rule against a node, with the skip/decorate/scope-refresh bookkeeping
* that used to live in AbstractRector::enterNode(). Keeps rules decoupled from PhpParser's NodeVisitor.
*
* @see \Rector\Tests\Rector\RectorRunner\RectorRunnerTest
*/
final readonly class RectorRunner
{
private const string 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;
public function __construct(
private ChangedNodeScopeRefresher $changedNodeScopeRefresher,
private CurrentFileProvider $currentFileProvider,
) {
}
/**
* @return NodeVisitor::REMOVE_NODE|Node|Node[]|null
*/
public function run(RectorInterface $rector, Node $node): int|Node|null|array
{
$file = $this->getFile();
$filePath = $file->getFilePath();
// ensure origNode pulled before refactor to avoid changed during refactor, ref https://3v4l.org/YMEGN
$originalNode = $node->getAttribute(AttributeKey::ORIGINAL_NODE) ?? $node;
$refactoredNodeOrState = $rector->refactor($node);
// nothing to change → continue
if ($refactoredNodeOrState === null) {
return null;
}
if ($refactoredNodeOrState === []) {
$errorMessage = sprintf(self::EMPTY_NODE_ARRAY_MESSAGE, $rector::class);
throw new ShouldNotHappenException($errorMessage);
}
if (is_int($refactoredNodeOrState)) {
// only remove node is supported
if ($refactoredNodeOrState !== NodeVisitor::REMOVE_NODE) {
throw new ShouldNotHappenException(sprintf('Unsupported state "%d" returned from "%s".', $refactoredNodeOrState, $rector::class));
}
// notify this rule changed code
$rectorWithLineChange = new RectorWithLineChange($rector::class, $originalNode->getStartLine());
$file->addRectorClassWithLine($rectorWithLineChange);
return $refactoredNodeOrState;
}
return $this->postRefactorProcess($rector, $file, $originalNode, $node, $refactoredNodeOrState, $filePath);
}
private function getFile(): File
{
$file = $this->currentFileProvider->getFile();
if (! $file instanceof File) {
throw new ShouldNotHappenException(
'File object is missing. Make sure you call $this->currentFileProvider->setFile(...) before traversing.'
);
}
return $file;
}
/**
* @param Node|Node[] $refactoredNode
* @return Node|Node[]
*/
private function postRefactorProcess(
RectorInterface $rector,
File $file,
Node $originalNode,
Node $node,
Node|array $refactoredNode,
string $filePath
): Node|array {
/** @var non-empty-array<Node>|Node $refactoredNode */
$rectorWithLineChange = new RectorWithLineChange($rector::class, $originalNode->getStartLine());
$file->addRectorClassWithLine($rectorWithLineChange);
/** @var MutatingScope|null $currentScope */
$currentScope = $node->getAttribute(AttributeKey::SCOPE);
$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);
}
}
}