Repository navigation
Expand file tree
/
Copy pathPathCloningVisitor.php
More file actions
141 lines (116 loc) · 3.12 KB
/
Copy pathPathCloningVisitor.php
File metadata and controls
141 lines (116 loc) · 3.12 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
<?php declare(strict_types = 1);
namespace PHPStan\Fixable;
use Override;
use PhpParser\Node;
use PhpParser\NodeVisitor;
use PhpParser\NodeVisitorAbstract;
use function array_pop;
/**
* Clones only the nodes that a fix can change: the fixed nodes with all their
* children, and the parents of the fixed nodes. Each other node stays the
* original object and gets itself as its origNode attribute, so that
* PhpPrinter can copy its original code without printing its children.
*
* Call restoreOriginalNodes() after printing, because the original nodes
* belong to the analysed AST.
*/
final class PathCloningVisitor extends NodeVisitorAbstract
{
/** @var list<array{int, int}> */
private array $targetRanges;
/** @var array<string, true> */
private array $targetKeys = [];
private int $insideTarget = 0;
/** @var list<bool> */
private array $enteredTargets = [];
/** @var list<Node> */
private array $sharedNodes = [];
/**
* @param non-empty-list<Node> $targets
*/
public function __construct(array $targets)
{
$ranges = [];
foreach ($targets as $target) {
$ranges[] = [$target->getStartFilePos(), $target->getEndFilePos()];
$this->targetKeys[self::getKey($target)] = true;
}
$this->targetRanges = $ranges;
}
/**
* @param non-empty-list<Node> $targets
*/
public static function canFind(array $targets): bool
{
foreach ($targets as $target) {
if ($target->getStartFilePos() < 0 || $target->getEndFilePos() < 0) {
return false;
}
}
return true;
}
#[Override]
public function enterNode(Node $node): Node|int
{
if ($this->insideTarget > 0) {
$this->enteredTargets[] = false;
return $this->cloneNode($node);
}
if (!$this->containsTarget($node)) {
$this->enteredTargets[] = false;
$node->setAttribute('origNode', $node);
$this->sharedNodes[] = $node;
return NodeVisitor::DONT_TRAVERSE_CHILDREN;
}
$isTarget = isset($this->targetKeys[self::getKey($node)]);
$this->enteredTargets[] = $isTarget;
if ($isTarget) {
$this->insideTarget++;
}
return $this->cloneNode($node);
}
#[Override]
public function leaveNode(Node $node): ?Node
{
if (array_pop($this->enteredTargets) === true) {
$this->insideTarget--;
}
return null;
}
/**
* Removes the origNode attribute from the original nodes again.
*/
public function restoreOriginalNodes(): void
{
foreach ($this->sharedNodes as $node) {
$attributes = $node->getAttributes();
unset($attributes['origNode']);
$node->setAttributes($attributes);
}
$this->sharedNodes = [];
}
private function containsTarget(Node $node): bool
{
$start = $node->getStartFilePos();
$end = $node->getEndFilePos();
if ($start < 0 || $end < 0) {
return true;
}
foreach ($this->targetRanges as [$targetStart, $targetEnd]) {
if ($start <= $targetStart && $targetEnd <= $end) {
return true;
}
}
return false;
}
private function cloneNode(Node $node): Node
{
$clone = clone $node;
$clone->setAttribute('origNode', $node);
return $clone;
}
private static function getKey(Node $node): string
{
return $node::class . ':' . $node->getStartFilePos() . ':' . $node->getEndFilePos();
}
}