4 * This file is part of Psy Shell.
6 * (c) 2012-2018 Justin Hileman
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Psy\Reflection;
15 * Somehow the standard reflection library doesn't include constants.
17 * ReflectionConstant_ corrects that omission.
19 * Note: For backwards compatibility reasons, this class is named
20 * ReflectionConstant_ rather than ReflectionConstant. It will be renamed in
23 class ReflectionConstant_ implements \Reflector
28 private static $magicConstants = [
37 '__COMPILER_HALT_OFFSET__',
41 * Construct a ReflectionConstant_ object.
45 public function __construct($name)
49 if (!\defined($name) && !self::isMagicConstant($name)) {
50 throw new \InvalidArgumentException('Unknown constant: ' . $name);
53 if (!self::isMagicConstant($name)) {
54 $this->value = @\constant($name);
59 * Exports a reflection.
62 * @param bool $return pass true to return the export, as opposed to emitting it
66 public static function export($name, $return = false)
68 $refl = new self($name);
69 $value = $refl->getValue();
71 $str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
80 public static function isMagicConstant($name)
82 return \in_array($name, self::$magicConstants);
86 * Get the constant's docblock.
90 public function getDocComment()
96 * Gets the constant name.
100 public function getName()
106 * Gets the namespace name.
108 * Returns '' when the constant is not namespaced.
112 public function getNamespaceName()
114 if (!$this->inNamespace()) {
118 return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name);
122 * Gets the value of the constant.
126 public function getValue()
132 * Checks if this constant is defined in a namespace.
136 public function inNamespace()
138 return \strpos($this->name, '\\') !== false;
146 public function __toString()
148 return $this->getName();
152 * Gets the constant's file name.
154 * Currently returns null, because if it returns a file name the signature
155 * formatter will barf.
157 public function getFileName()
160 // return $this->class->getFileName();
164 * Get the code start line.
166 * @throws \RuntimeException
168 public function getStartLine()
170 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
174 * Get the code end line.
176 * @throws \RuntimeException
178 public function getEndLine()
180 return $this->getStartLine();