-
Notifications
You must be signed in to change notification settings - Fork 507
/
Copy pathCleaningVisitor.php
174 lines (145 loc) · 4.18 KB
/
CleaningVisitor.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Parser;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use PHPStan\Reflection\ParametersAcceptor;
use PHPStan\ShouldNotHappenException;
use function array_filter;
use function array_map;
use function in_array;
use function is_array;
final class CleaningVisitor extends NodeVisitorAbstract
{
private const CONTEXT_DEFAULT = 0;
private const CONTEXT_FUNCTION_OR_METHOD = 1;
private const CONTEXT_PROPERTY_HOOK = 2;
/** @var self::CONTEXT_* */
private int $context = self::CONTEXT_DEFAULT;
private string|null $propertyName = null;
/**
* @return int|Node[]|null
*/
public function enterNode(Node $node): int|array|null
{
switch ($this->context) {
case self::CONTEXT_DEFAULT:
return $this->clean($node);
case self::CONTEXT_FUNCTION_OR_METHOD:
return $this->cleanFunctionOrMethod($node);
case self::CONTEXT_PROPERTY_HOOK:
return $this->cleanPropertyHook($node);
}
}
private function clean(Node $node): int|null
{
if (($node instanceof Node\Stmt\Function_ || $node instanceof Node\Stmt\ClassMethod) && $node->stmts !== null) {
$params = [];
foreach ($this->traverse($node->params, self::CONTEXT_DEFAULT) as $param) {
if (!($param instanceof Node\Param)) {
continue;
}
$params[] = $param;
}
$node->params = $params;
$stmts = [];
foreach ($this->traverse($node->stmts, self::CONTEXT_FUNCTION_OR_METHOD) as $stmt) {
if (!($stmt instanceof Node\Stmt)) {
continue;
}
$stmts[] = $stmt;
}
$node->stmts = $stmts;
return self::DONT_TRAVERSE_CHILDREN;
}
if ($node instanceof Node\PropertyHook && is_array($node->body)) {
$propertyName = $node->getAttribute('propertyName');
if ($propertyName !== null) {
$body = [];
foreach ($this->traverse($node->body, self::CONTEXT_PROPERTY_HOOK, $propertyName) as $stmt) {
if (!($stmt instanceof Node\Stmt)) {
continue;
}
$body[] = $stmt;
}
$node->body = $body;
return self::DONT_TRAVERSE_CHILDREN;
}
}
return null;
}
/**
* @return int|Node[]
*/
private function cleanFunctionOrMethod(Node $node): int|array
{
if ($node instanceof Node\Expr\YieldFrom || $node instanceof Node\Expr\Yield_) {
return self::DONT_TRAVERSE_CHILDREN;
}
if ($node instanceof Node\Expr\FuncCall && $node->name instanceof Node\Name
&& in_array($node->name->toLowerString(), ParametersAcceptor::VARIADIC_FUNCTIONS, true)
) {
$node->name = new Node\Name\FullyQualified('func_get_args');
return self::DONT_TRAVERSE_CHILDREN;
}
if ($node instanceof Node\Expr\Closure || $node instanceof Node\Expr\ArrowFunction) {
return self::REMOVE_NODE;
}
return $this->cleanSubnodes($node);
}
/**
* @param Node[] $nodes
* @param self::CONTEXT_* $context
* @return Node[]
*/
private function traverse(
array $nodes,
int $context = self::CONTEXT_DEFAULT,
string|null $propertyName = null,
): array
{
$visitor = new self();
$visitor->context = $context;
$visitor->propertyName = $propertyName;
return (new NodeTraverser($visitor))->traverse($nodes);
}
/**
* @return int|Node[]
*/
private function cleanPropertyHook(Node $node): int|array
{
if (
$node instanceof Node\Expr\PropertyFetch
&& $node->var instanceof Node\Expr\Variable
&& $node->var->name === 'this'
&& $node->name instanceof Node\Identifier
&& $node->name->toString() === $this->propertyName
) {
return self::DONT_TRAVERSE_CHILDREN;
}
return $this->cleanSubnodes($node);
}
/**
* @return Node[]
*/
private function cleanSubnodes(Node $node): array
{
$subnodes = [];
foreach ($node->getSubNodeNames() as $subnodeName) {
$subnodes = [...$subnodes, ...array_filter(
is_array($node->$subnodeName) ? $node->$subnodeName : [$node->$subnodeName],
static fn ($subnode) => $subnode instanceof Node,
)];
}
return array_map(static function ($node) {
switch (true) {
case $node instanceof Node\Stmt:
return $node;
case $node instanceof Node\Expr:
return new Node\Stmt\Expression($node);
default:
throw new ShouldNotHappenException();
}
}, $this->traverse($subnodes, $this->context, $this->propertyName));
}
}