forked from Roave/BetterReflection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompileNodeToValue.php
161 lines (135 loc) · 5.11 KB
/
CompileNodeToValue.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
<?php
declare(strict_types=1);
namespace Roave\BetterReflection\NodeCompiler;
use PhpParser\ConstExprEvaluator;
use PhpParser\Node;
use Roave\BetterReflection\Reflection\ReflectionClass;
use Roave\BetterReflection\Reflection\ReflectionClassConstant;
use Roave\BetterReflection\Reflector\Exception\IdentifierNotFound;
use Roave\BetterReflection\Util\FileHelper;
use function constant;
use function defined;
use function dirname;
use function realpath;
use function reset;
class CompileNodeToValue
{
/**
* Compile an expression from a node into a value.
*
* @param Node\Stmt\Expression|Node\Expr $node Node has to be processed by the PhpParser\NodeVisitor\NameResolver
*
* @return mixed
*
* @throws Exception\UnableToCompileNode
*/
public function __invoke(Node $node, CompilerContext $context)
{
if ($node instanceof Node\Stmt\Expression) {
return $this($node->expr, $context);
}
$constExprEvaluator = new ConstExprEvaluator(function (Node\Expr $node) use ($context) {
if ($node instanceof Node\Expr\ConstFetch) {
return $this->compileConstFetch($node, $context);
}
if ($node instanceof Node\Expr\ClassConstFetch) {
return $this->compileClassConstFetch($node, $context);
}
if ($node instanceof Node\Scalar\MagicConst\Dir) {
return $this->compileDirConstant($context);
}
if ($node instanceof Node\Scalar\MagicConst\Class_) {
return $this->compileClassConstant($context);
}
throw Exception\UnableToCompileNode::forUnRecognizedExpressionInContext($node, $context);
});
return $constExprEvaluator->evaluateDirectly($node);
}
/**
* Compile constant expressions
*
* @return bool|mixed|null
*
* @throws Exception\UnableToCompileNode
*/
private function compileConstFetch(Node\Expr\ConstFetch $constNode, CompilerContext $context)
{
$firstName = reset($constNode->name->parts);
switch ($firstName) {
case 'null':
return null;
case 'false':
return false;
case 'true':
return true;
default:
if (! defined($firstName)) {
throw Exception\UnableToCompileNode::becauseOfNotFoundConstantReference($context, $constNode);
}
return constant($firstName);
}
}
/**
* Compile class constants
*
* @return string|int|float|bool|mixed[]|null
*
* @throws IdentifierNotFound
* @throws Exception\UnableToCompileNode If a referenced constant could not be located on the expected referenced class.
*/
private function compileClassConstFetch(Node\Expr\ClassConstFetch $node, CompilerContext $context)
{
/** @var Node\Identifier $node->name */
$nodeName = $node->name->name;
/** @var Node\Name $node->class */
$className = $node->class->toString();
if ($nodeName === 'class') {
return $this->resolveClassNameForClassNameConstant($className, $context);
}
/** @var ReflectionClass|null $classInfo */
$classInfo = null;
if ($className === 'self' || $className === 'static') {
$classInfo = $context->getSelf()->hasConstant($nodeName) ? $context->getSelf() : null;
} elseif ($className === 'parent') {
$classInfo = $context->getSelf()->getParentClass();
}
if ($classInfo === null) {
/** @var ReflectionClass $classInfo */
$classInfo = $context->getReflector()->reflect($className);
}
$reflectionConstant = $classInfo->getReflectionConstant($nodeName);
if (! $reflectionConstant instanceof ReflectionClassConstant) {
throw Exception\UnableToCompileNode::becauseOfNotFoundClassConstantReference($context, $classInfo, $node);
}
return $this->__invoke(
$reflectionConstant->getAst()->consts[$reflectionConstant->getPositionInAst()]->value,
new CompilerContext($context->getReflector(), $classInfo)
);
}
/**
* Compile a __DIR__ node
*/
private function compileDirConstant(CompilerContext $context) : string
{
return FileHelper::normalizeWindowsPath(dirname(realpath($context->getFileName())));
}
/**
* Compiles magic constant __CLASS__
*/
private function compileClassConstant(CompilerContext $context) : string
{
return $context->hasSelf() ? $context->getSelf()->getName() : '';
}
private function resolveClassNameForClassNameConstant(string $className, CompilerContext $context) : string
{
if ($className === 'self' || $className === 'static') {
return $context->getSelf()->getName();
}
if ($className === 'parent') {
/** @var ReflectionClass $parentClass */
$parentClass = $context->getSelf()->getParentClass();
return $parentClass->getName();
}
return $className;
}
}