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 class ReflectionConstant implements \Reflector
26 * Construct a ReflectionConstant object.
31 public function __construct($class, $name)
33 if (!$class instanceof \ReflectionClass) {
34 $class = new \ReflectionClass($class);
37 $this->class = $class;
40 $constants = $class->getConstants();
41 if (!array_key_exists($name, $constants)) {
42 throw new \InvalidArgumentException('Unknown constant: ' . $name);
45 $this->value = $constants[$name];
49 * Gets the declaring class.
53 public function getDeclaringClass()
55 $parent = $this->class;
57 // Since we don't have real reflection constants, we can't see where
58 // it's actually defined. Let's check for a constant that is also
59 // available on the parent class which has exactly the same value.
61 // While this isn't _technically_ correct, it's prolly close enough.
64 $parent = $class->getParentClass();
65 } while ($parent && $parent->hasConstant($this->name) && $parent->getConstant($this->name) === $this->value);
71 * Gets the constant name.
75 public function getName()
81 * Gets the value of the constant.
85 public function getValue()
91 * Gets the constant's file name.
93 * Currently returns null, because if it returns a file name the signature
94 * formatter will barf.
96 public function getFileName()
99 // return $this->class->getFileName();
103 * Get the code start line.
105 * @throws \RuntimeException
107 public function getStartLine()
109 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
113 * Get the code end line.
115 * @throws \RuntimeException
117 public function getEndLine()
119 return $this->getStartLine();
123 * Get the constant's docblock.
127 public function getDocComment()
133 * Export the constant? I don't think this is possible.
135 * @throws \RuntimeException
137 public static function export()
139 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
147 public function __toString()
149 return $this->getName();