ReflectionConstant.php
3.5 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
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
<?php
/*
* This file is part of Psy Shell.
*
* (c) 2012-2023 Justin Hileman
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Psy\Reflection;
/**
* Somehow the standard reflection library doesn't include constants.
*
* ReflectionConstant corrects that omission.
*/
class ReflectionConstant implements \Reflector
{
public $name;
private $value;
private static $magicConstants = [
'__LINE__',
'__FILE__',
'__DIR__',
'__FUNCTION__',
'__CLASS__',
'__TRAIT__',
'__METHOD__',
'__NAMESPACE__',
'__COMPILER_HALT_OFFSET__',
];
/**
* Construct a ReflectionConstant object.
*
* @param string $name
*/
public function __construct(string $name)
{
$this->name = $name;
if (!\defined($name) && !self::isMagicConstant($name)) {
throw new \InvalidArgumentException('Unknown constant: '.$name);
}
if (!self::isMagicConstant($name)) {
$this->value = @\constant($name);
}
}
/**
* Exports a reflection.
*
* @param string $name
* @param bool $return pass true to return the export, as opposed to emitting it
*
* @return string|null
*/
public static function export(string $name, bool $return = false)
{
$refl = new self($name);
$value = $refl->getValue();
$str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
if ($return) {
return $str;
}
echo $str."\n";
}
public static function isMagicConstant($name)
{
return \in_array($name, self::$magicConstants);
}
/**
* Get the constant's docblock.
*
* @return false
*/
public function getDocComment(): bool
{
return false;
}
/**
* Gets the constant name.
*/
public function getName(): string
{
return $this->name;
}
/**
* Gets the namespace name.
*
* Returns '' when the constant is not namespaced.
*/
public function getNamespaceName(): string
{
if (!$this->inNamespace()) {
return '';
}
return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name);
}
/**
* Gets the value of the constant.
*
* @return mixed
*/
public function getValue()
{
return $this->value;
}
/**
* Checks if this constant is defined in a namespace.
*/
public function inNamespace(): bool
{
return \strpos($this->name, '\\') !== false;
}
/**
* To string.
*/
public function __toString(): string
{
return $this->getName();
}
/**
* Gets the constant's file name.
*
* Currently returns null, because if it returns a file name the signature
* formatter will barf.
*/
public function getFileName()
{
return;
// return $this->class->getFileName();
}
/**
* Get the code start line.
*
* @throws \RuntimeException
*/
public function getStartLine()
{
throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
}
/**
* Get the code end line.
*
* @throws \RuntimeException
*/
public function getEndLine()
{
return $this->getStartLine();
}
}