/
CachingVisitor.php
212 lines (172 loc) · 6.96 KB
/
CachingVisitor.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
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
<?php
declare(strict_types=1);
namespace Roave\BetterReflection\SourceLocator\SourceStubber\PhpStormStubs;
use PhpParser\BuilderFactory;
use PhpParser\Node;
use PhpParser\NodeTraverser;
use PhpParser\NodeVisitorAbstract;
use Roave\BetterReflection\Reflection\Exception\InvalidConstantNode;
use Roave\BetterReflection\Util\ConstantNodeChecker;
use function array_key_exists;
use function assert;
use function constant;
use function defined;
use function in_array;
use function is_resource;
use function sprintf;
use function strtolower;
use function strtoupper;
/**
* @internal
*/
class CachingVisitor extends NodeVisitorAbstract
{
private const TRUE_FALSE_NULL = ['true', 'false', 'null'];
private ?Node\Stmt\Namespace_ $currentNamespace = null;
/** @var array<string, array{0: Node\Stmt\ClassLike, 1: Node\Stmt\Namespace_|null}> */
private array $classNodes = [];
/** @var array<string, list<array{0: Node\Stmt\Function_, 1: Node\Stmt\Namespace_|null}>> */
private array $functionNodes = [];
/** @var array<string, array{0: Node\Stmt\Const_|Node\Expr\FuncCall, 1: Node\Stmt\Namespace_|null}> */
private array $constantNodes = [];
public function __construct(private BuilderFactory $builderFactory)
{
}
public function enterNode(Node $node): ?int
{
if ($node instanceof Node\Stmt\Namespace_) {
$this->currentNamespace = $node;
return null;
}
if ($node instanceof Node\Stmt\ClassLike) {
$nodeName = $node->namespacedName->toString();
$this->classNodes[$nodeName] = [$node, $this->currentNamespace];
foreach ($node->getConstants() as $constantsNode) {
foreach ($constantsNode->consts as $constNode) {
$constClassName = sprintf('%s::%s', $nodeName, $constNode->name->toString());
$this->updateConstantValue($constNode, $constClassName);
}
}
// We need to traverse children to resolve attributes names for methods, properties etc.
return null;
}
if (
$node instanceof Node\Stmt\ClassMethod
|| $node instanceof Node\Stmt\Property
|| $node instanceof Node\Stmt\ClassConst
|| $node instanceof Node\Stmt\EnumCase
) {
return NodeTraverser::DONT_TRAVERSE_CURRENT_AND_CHILDREN;
}
if ($node instanceof Node\Stmt\Function_) {
$nodeName = $node->namespacedName->toString();
$this->functionNodes[$nodeName][] = [$node, $this->currentNamespace];
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
if ($node instanceof Node\Stmt\Const_) {
foreach ($node->consts as $constNode) {
$constNodeName = $constNode->namespacedName->toString();
$this->updateConstantValue($constNode, $constNodeName);
$this->constantNodes[$constNodeName] = [$node, $this->currentNamespace];
}
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
if ($node instanceof Node\Expr\FuncCall) {
$argumentNameNode = $node->args[0];
assert($argumentNameNode instanceof Node\Arg);
$nameNode = $argumentNameNode->value;
assert($nameNode instanceof Node\Scalar\String_);
$constantName = $nameNode->value;
// The definition is stubs looks like `define('STDIN', fopen('php://stdin', 'r'))`
// We will modify it to `define('STDIN', constant('STDIN'));
// The later definition can pass validation in `ConstantNodeChecker` and has support in `CompileNodeToValue`
if (
in_array($constantName, ['STDIN', 'STDOUT', 'STDERR'], true)
&& array_key_exists(1, $node->args)
&& $node->args[1] instanceof Node\Arg
) {
$node->args[1]->value = $this->builderFactory->funcCall('constant', [$constantName]);
}
try {
ConstantNodeChecker::assertValidDefineFunctionCall($node);
} catch (InvalidConstantNode) {
return null;
}
if (in_array($constantName, self::TRUE_FALSE_NULL, true)) {
$constantName = strtoupper($constantName);
$nameNode->value = $constantName;
}
$this->updateConstantValue($node, $constantName);
$this->constantNodes[$constantName] = [$node, $this->currentNamespace];
if (
array_key_exists(2, $node->args)
&& $node->args[2] instanceof Node\Arg
&& $node->args[2]->value instanceof Node\Expr\ConstFetch
&& $node->args[2]->value->name->toLowerString() === 'true'
) {
$this->constantNodes[strtolower($constantName)] = [$node, $this->currentNamespace];
}
return NodeTraverser::DONT_TRAVERSE_CHILDREN;
}
return null;
}
/**
* {@inheritDoc}
*/
public function leaveNode(Node $node)
{
if ($node instanceof Node\Stmt\Namespace_) {
$this->currentNamespace = null;
}
return null;
}
/**
* @return array<string, array{0: Node\Stmt\ClassLike, 1: Node\Stmt\Namespace_|null}>
*/
public function getClassNodes(): array
{
return $this->classNodes;
}
/**
* @return array<string, list<array{0: Node\Stmt\Function_, 1: Node\Stmt\Namespace_|null}>>
*/
public function getFunctionNodes(): array
{
return $this->functionNodes;
}
/**
* @return array<string, array{0: Node\Stmt\Const_|Node\Expr\FuncCall, 1: Node\Stmt\Namespace_|null}>
*/
public function getConstantNodes(): array
{
return $this->constantNodes;
}
public function clearNodes(): void
{
$this->classNodes = [];
$this->functionNodes = [];
$this->constantNodes = [];
}
/**
* Some constants has different values on different systems, some are not actual in stubs.
*/
private function updateConstantValue(Node\Expr\FuncCall|Node\Const_ $node, string $constantName): void
{
if (! defined($constantName)) {
return;
}
// @ because access to deprecated constant throws deprecated warning
/** @var scalar|resource|list<scalar>|null $constantValue */
$constantValue = @constant($constantName);
$normalizedConstantValue = is_resource($constantValue)
? $this->builderFactory->funcCall('constant', [$constantName])
: $this->builderFactory->val($constantValue);
if ($node instanceof Node\Expr\FuncCall) {
$argumentValueNode = $node->args[1];
assert($argumentValueNode instanceof Node\Arg);
$argumentValueNode->value = $normalizedConstantValue;
} else {
$node->value = $normalizedConstantValue;
}
}
}