1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Parser Reflection API |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright 2015, Lisachenko Alexander <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
|
11
|
|
|
namespace Go\ParserReflection\ValueResolver; |
12
|
|
|
|
13
|
|
|
use Go\ParserReflection\ReflectionClass; |
14
|
|
|
use Go\ParserReflection\ReflectionException; |
15
|
|
|
use Go\ParserReflection\ReflectionFileNamespace; |
16
|
|
|
use PhpParser\Node; |
17
|
|
|
use PhpParser\Node\Expr; |
18
|
|
|
use PhpParser\Node\Scalar; |
19
|
|
|
use PhpParser\Node\Scalar\MagicConst; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Tries to resolve expression into value |
23
|
|
|
*/ |
24
|
|
|
class NodeExpressionResolver |
25
|
|
|
{ |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* List of exception for constant fetch |
29
|
|
|
* |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
private static $notConstants = [ |
33
|
|
|
'true' => true, |
34
|
|
|
'false' => true, |
35
|
|
|
'null' => true, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Name of the constant (if present) |
40
|
|
|
* |
41
|
|
|
* @var null|string |
42
|
|
|
*/ |
43
|
|
|
private $constantName = null; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Current reflection context for parsing |
47
|
|
|
* |
48
|
|
|
* @var mixed |
49
|
|
|
*/ |
50
|
|
|
private $context; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Flag if expression is constant |
54
|
|
|
* |
55
|
|
|
* @var bool |
56
|
|
|
*/ |
57
|
|
|
private $isConstant = false; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Node resolving level, 1 = top-level |
61
|
|
|
* |
62
|
|
|
* @var int |
63
|
|
|
*/ |
64
|
|
|
private $nodeLevel = 0; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @var mixed Value of expression/constant |
68
|
|
|
*/ |
69
|
|
|
private $value; |
70
|
|
|
|
71
|
36 |
|
public function __construct($context) |
72
|
|
|
{ |
73
|
36 |
|
$this->context = $context; |
74
|
36 |
|
} |
75
|
|
|
|
76
|
10 |
|
public function getConstantName() |
77
|
|
|
{ |
78
|
10 |
|
return $this->constantName; |
79
|
|
|
} |
80
|
|
|
|
81
|
22 |
|
public function getValue() |
82
|
|
|
{ |
83
|
22 |
|
return $this->value; |
84
|
|
|
} |
85
|
|
|
|
86
|
10 |
|
public function isConstant() |
87
|
|
|
{ |
88
|
10 |
|
return $this->isConstant; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* {@inheritDoc} |
93
|
|
|
*/ |
94
|
22 |
|
public function process(Node $node) |
95
|
|
|
{ |
96
|
22 |
|
$this->nodeLevel = 0; |
97
|
22 |
|
$this->isConstant = false; |
98
|
22 |
|
$this->constantName = null; |
99
|
22 |
|
$this->value = $this->resolve($node); |
100
|
22 |
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Resolves node into valid value |
104
|
|
|
* |
105
|
|
|
* @param Node $node |
106
|
|
|
* |
107
|
|
|
* @return mixed |
108
|
|
|
*/ |
109
|
22 |
|
protected function resolve(Node $node) |
110
|
|
|
{ |
111
|
22 |
|
$value = null; |
112
|
|
|
try { |
113
|
22 |
|
++$this->nodeLevel; |
114
|
|
|
|
115
|
22 |
|
$nodeType = $node->getType(); |
116
|
22 |
|
$methodName = 'resolve' . str_replace('_', '', $nodeType); |
117
|
22 |
|
if (method_exists($this, $methodName)) { |
118
|
22 |
|
$value = $this->$methodName($node); |
119
|
|
|
} |
120
|
22 |
|
} finally { |
121
|
22 |
|
--$this->nodeLevel; |
122
|
|
|
} |
123
|
|
|
|
124
|
22 |
|
return $value; |
125
|
|
|
} |
126
|
|
|
|
127
|
4 |
|
protected function resolveScalarDNumber(Scalar\DNumber $node) |
128
|
|
|
{ |
129
|
4 |
|
return $node->value; |
130
|
|
|
} |
131
|
|
|
|
132
|
13 |
|
protected function resolveScalarLNumber(Scalar\LNumber $node) |
133
|
|
|
{ |
134
|
13 |
|
return $node->value; |
135
|
|
|
} |
136
|
|
|
|
137
|
9 |
|
protected function resolveScalarString(Scalar\String_ $node) |
138
|
|
|
{ |
139
|
9 |
|
return $node->value; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
protected function resolveScalarMagicConstMethod() |
143
|
|
|
{ |
144
|
|
|
if ($this->context instanceof \ReflectionMethod) { |
145
|
|
|
$fullName = $this->context->getDeclaringClass()->getName() . '::' . $this->context->getShortName(); |
|
|
|
|
146
|
|
|
|
147
|
|
|
return $fullName; |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
return ''; |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
protected function resolveScalarMagicConstFunction() |
154
|
|
|
{ |
155
|
|
|
if ($this->context instanceof \ReflectionFunctionAbstract) { |
156
|
|
|
return $this->context->getName(); |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
return ''; |
160
|
|
|
} |
161
|
|
|
|
162
|
10 |
|
protected function resolveScalarMagicConstNamespace() |
163
|
|
|
{ |
164
|
10 |
|
if (method_exists($this->context, 'getNamespaceName')) { |
165
|
8 |
|
return $this->context->getNamespaceName(); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
if ($this->context instanceof ReflectionFileNamespace) { |
169
|
2 |
|
return $this->context->getName(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
return ''; |
173
|
|
|
} |
174
|
|
|
|
175
|
8 |
|
protected function resolveScalarMagicConstClass() |
176
|
|
|
{ |
177
|
8 |
|
if ($this->context instanceof \ReflectionClass) { |
178
|
1 |
|
return $this->context->getName(); |
|
|
|
|
179
|
|
|
} |
180
|
7 |
|
if (method_exists($this->context, 'getDeclaringClass')) { |
181
|
|
|
$declaringClass = $this->context->getDeclaringClass(); |
182
|
|
|
if ($declaringClass instanceof \ReflectionClass) { |
183
|
|
|
return $declaringClass->getName(); |
|
|
|
|
184
|
|
|
} |
185
|
|
|
} |
186
|
|
|
|
187
|
7 |
|
return ''; |
188
|
|
|
} |
189
|
|
|
|
190
|
1 |
|
protected function resolveScalarMagicConstDir() |
191
|
|
|
{ |
192
|
1 |
|
if (method_exists($this->context, 'getFileName')) { |
193
|
1 |
|
return dirname($this->context->getFileName()); |
194
|
|
|
} |
195
|
|
|
|
196
|
|
|
return ''; |
197
|
|
|
} |
198
|
|
|
|
199
|
3 |
|
protected function resolveScalarMagicConstFile() |
200
|
|
|
{ |
201
|
3 |
|
if (method_exists($this->context, 'getFileName')) { |
202
|
3 |
|
return $this->context->getFileName(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
return ''; |
206
|
|
|
} |
207
|
|
|
|
208
|
3 |
|
protected function resolveScalarMagicConstLine(MagicConst\Line $node) |
209
|
|
|
{ |
210
|
3 |
|
return $node->hasAttribute('startLine') ? $node->getAttribute('startLine') : 0; |
211
|
|
|
} |
212
|
|
|
|
213
|
1 |
|
protected function resolveScalarMagicConstTrait() |
214
|
|
|
{ |
215
|
1 |
|
if ($this->context instanceof \ReflectionClass && $this->context->isTrait()) { |
216
|
1 |
|
return $this->context->getName(); |
|
|
|
|
217
|
|
|
} |
218
|
|
|
|
219
|
|
|
return ''; |
220
|
|
|
} |
221
|
|
|
|
222
|
13 |
|
protected function resolveExprConstFetch(Expr\ConstFetch $node) |
223
|
|
|
{ |
224
|
13 |
|
$constantValue = null; |
225
|
13 |
|
$isResolved = false; |
226
|
|
|
|
227
|
|
|
/** @var ReflectionFileNamespace|null $fileNamespace */ |
228
|
13 |
|
$fileNamespace = null; |
|
|
|
|
229
|
13 |
|
$isFQNConstant = $node->name instanceof Node\Name\FullyQualified; |
230
|
13 |
|
$constantName = $node->name->toString(); |
231
|
|
|
|
232
|
13 |
|
if (!$isFQNConstant) { |
233
|
13 |
|
if (method_exists($this->context, 'getFileName')) { |
234
|
13 |
|
$fileName = $this->context->getFileName(); |
235
|
13 |
|
$namespaceName = $this->context->getNamespaceName(); |
236
|
13 |
|
$fileNamespace = new ReflectionFileNamespace($fileName, $namespaceName); |
237
|
13 |
|
if ($fileNamespace->hasConstant($constantName)) { |
238
|
9 |
|
$constantValue = $fileNamespace->getConstant($constantName); |
239
|
9 |
|
$constantName = $fileNamespace->getName() . '\\' . $constantName; |
240
|
9 |
|
$isResolved = true; |
241
|
|
|
} |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
|
245
|
13 |
|
if (!$isResolved && defined($constantName)) { |
246
|
12 |
|
$constantValue = constant($constantName); |
247
|
|
|
} |
248
|
|
|
|
249
|
13 |
|
if ($this->nodeLevel === 1 && !isset(self::$notConstants[$constantName])) { |
250
|
9 |
|
$this->isConstant = true; |
251
|
9 |
|
$this->constantName = $constantName; |
252
|
|
|
} |
253
|
|
|
|
254
|
13 |
|
return $constantValue; |
255
|
|
|
} |
256
|
|
|
|
257
|
4 |
|
protected function resolveExprClassConstFetch(Expr\ClassConstFetch $node) |
258
|
|
|
{ |
259
|
4 |
|
$refClass = $this->fetchReflectionClass($node->class); |
260
|
4 |
|
$constantName = $node->name; |
261
|
|
|
|
262
|
|
|
// special handling of ::class constants |
263
|
4 |
|
if ('class' === $constantName) { |
264
|
1 |
|
return $refClass->getName(); |
265
|
|
|
} |
266
|
|
|
|
267
|
4 |
|
return $refClass->getConstant($constantName); |
268
|
|
|
} |
269
|
|
|
|
270
|
9 |
|
protected function resolveExprArray(Expr\Array_ $node) |
271
|
|
|
{ |
272
|
9 |
|
$result = []; |
273
|
9 |
|
foreach ($node->items as $itemIndex => $arrayItem) { |
274
|
8 |
|
$itemValue = $this->resolve($arrayItem->value); |
275
|
8 |
|
$itemKey = isset($arrayItem->key) ? $this->resolve($arrayItem->key) : $itemIndex; |
276
|
8 |
|
$result[$itemKey] = $itemValue; |
277
|
|
|
} |
278
|
|
|
|
279
|
9 |
|
return $result; |
280
|
|
|
} |
281
|
|
|
|
282
|
|
|
/** |
283
|
|
|
* Utility method to fetch reflection class instance by name |
284
|
|
|
* |
285
|
|
|
* Supports: |
286
|
|
|
* 'self' keyword |
287
|
|
|
* 'parent' keyword |
288
|
|
|
* not-FQN class names |
289
|
|
|
* |
290
|
|
|
* @param Node\Name $node Class name node |
291
|
|
|
* |
292
|
|
|
* @return bool|\ReflectionClass |
293
|
|
|
* |
294
|
|
|
* @throws ReflectionException |
295
|
|
|
*/ |
296
|
4 |
|
private function fetchReflectionClass(Node\Name $node) |
297
|
|
|
{ |
298
|
4 |
|
$className = $node->toString(); |
299
|
4 |
|
$isFQNClass = $node instanceof Node\Name\FullyQualified; |
300
|
4 |
|
if ($isFQNClass) { |
301
|
1 |
|
return new ReflectionClass($className); |
302
|
|
|
} |
303
|
|
|
|
304
|
4 |
|
if ('self' === $className) { |
305
|
4 |
|
if ($this->context instanceof \ReflectionClass) { |
306
|
4 |
|
return $this->context; |
307
|
|
|
} elseif (method_exists($this->context, 'getDeclaringClass')) { |
308
|
|
|
return $this->context->getDeclaringClass(); |
309
|
|
|
} |
310
|
|
|
} |
311
|
|
|
|
312
|
1 |
|
if ('parent' === $className) { |
313
|
1 |
|
if ($this->context instanceof \ReflectionClass) { |
314
|
1 |
|
return $this->context->getParentClass(); |
315
|
|
|
} elseif (method_exists($this->context, 'getDeclaringClass')) { |
316
|
|
|
return $this->context->getDeclaringClass()->getParentClass(); |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|
320
|
|
|
if (method_exists($this->context, 'getFileName')) { |
321
|
|
|
/** @var ReflectionFileNamespace|null $fileNamespace */ |
322
|
|
|
$fileName = $this->context->getFileName(); |
323
|
|
|
$namespaceName = $this->context->getNamespaceName(); |
324
|
|
|
|
325
|
|
|
$fileNamespace = new ReflectionFileNamespace($fileName, $namespaceName); |
326
|
|
|
return $fileNamespace->getClass($className); |
327
|
|
|
} |
328
|
|
|
|
329
|
|
|
throw new ReflectionException("Can not resolve class $className"); |
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|