Chris@13
|
1 <?php
|
Chris@13
|
2
|
Chris@13
|
3 /*
|
Chris@13
|
4 * This file is part of Psy Shell.
|
Chris@13
|
5 *
|
Chris@13
|
6 * (c) 2012-2018 Justin Hileman
|
Chris@13
|
7 *
|
Chris@13
|
8 * For the full copyright and license information, please view the LICENSE
|
Chris@13
|
9 * file that was distributed with this source code.
|
Chris@13
|
10 */
|
Chris@13
|
11
|
Chris@13
|
12 namespace Psy\Reflection;
|
Chris@13
|
13
|
Chris@13
|
14 /**
|
Chris@13
|
15 * Somehow the standard reflection library doesn't include constants.
|
Chris@13
|
16 *
|
Chris@13
|
17 * ReflectionConstant corrects that omission.
|
Chris@13
|
18 */
|
Chris@13
|
19 class ReflectionConstant implements \Reflector
|
Chris@13
|
20 {
|
Chris@13
|
21 private $class;
|
Chris@13
|
22 private $name;
|
Chris@13
|
23 private $value;
|
Chris@13
|
24
|
Chris@13
|
25 /**
|
Chris@13
|
26 * Construct a ReflectionConstant object.
|
Chris@13
|
27 *
|
Chris@13
|
28 * @param mixed $class
|
Chris@13
|
29 * @param string $name
|
Chris@13
|
30 */
|
Chris@13
|
31 public function __construct($class, $name)
|
Chris@13
|
32 {
|
Chris@13
|
33 if (!$class instanceof \ReflectionClass) {
|
Chris@13
|
34 $class = new \ReflectionClass($class);
|
Chris@13
|
35 }
|
Chris@13
|
36
|
Chris@13
|
37 $this->class = $class;
|
Chris@13
|
38 $this->name = $name;
|
Chris@13
|
39
|
Chris@13
|
40 $constants = $class->getConstants();
|
Chris@13
|
41 if (!array_key_exists($name, $constants)) {
|
Chris@13
|
42 throw new \InvalidArgumentException('Unknown constant: ' . $name);
|
Chris@13
|
43 }
|
Chris@13
|
44
|
Chris@13
|
45 $this->value = $constants[$name];
|
Chris@13
|
46 }
|
Chris@13
|
47
|
Chris@13
|
48 /**
|
Chris@13
|
49 * Gets the declaring class.
|
Chris@13
|
50 *
|
Chris@13
|
51 * @return string
|
Chris@13
|
52 */
|
Chris@13
|
53 public function getDeclaringClass()
|
Chris@13
|
54 {
|
Chris@13
|
55 $parent = $this->class;
|
Chris@13
|
56
|
Chris@13
|
57 // Since we don't have real reflection constants, we can't see where
|
Chris@13
|
58 // it's actually defined. Let's check for a constant that is also
|
Chris@13
|
59 // available on the parent class which has exactly the same value.
|
Chris@13
|
60 //
|
Chris@13
|
61 // While this isn't _technically_ correct, it's prolly close enough.
|
Chris@13
|
62 do {
|
Chris@13
|
63 $class = $parent;
|
Chris@13
|
64 $parent = $class->getParentClass();
|
Chris@13
|
65 } while ($parent && $parent->hasConstant($this->name) && $parent->getConstant($this->name) === $this->value);
|
Chris@13
|
66
|
Chris@13
|
67 return $class;
|
Chris@13
|
68 }
|
Chris@13
|
69
|
Chris@13
|
70 /**
|
Chris@13
|
71 * Gets the constant name.
|
Chris@13
|
72 *
|
Chris@13
|
73 * @return string
|
Chris@13
|
74 */
|
Chris@13
|
75 public function getName()
|
Chris@13
|
76 {
|
Chris@13
|
77 return $this->name;
|
Chris@13
|
78 }
|
Chris@13
|
79
|
Chris@13
|
80 /**
|
Chris@13
|
81 * Gets the value of the constant.
|
Chris@13
|
82 *
|
Chris@13
|
83 * @return mixed
|
Chris@13
|
84 */
|
Chris@13
|
85 public function getValue()
|
Chris@13
|
86 {
|
Chris@13
|
87 return $this->value;
|
Chris@13
|
88 }
|
Chris@13
|
89
|
Chris@13
|
90 /**
|
Chris@13
|
91 * Gets the constant's file name.
|
Chris@13
|
92 *
|
Chris@13
|
93 * Currently returns null, because if it returns a file name the signature
|
Chris@13
|
94 * formatter will barf.
|
Chris@13
|
95 */
|
Chris@13
|
96 public function getFileName()
|
Chris@13
|
97 {
|
Chris@13
|
98 return;
|
Chris@13
|
99 // return $this->class->getFileName();
|
Chris@13
|
100 }
|
Chris@13
|
101
|
Chris@13
|
102 /**
|
Chris@13
|
103 * Get the code start line.
|
Chris@13
|
104 *
|
Chris@13
|
105 * @throws \RuntimeException
|
Chris@13
|
106 */
|
Chris@13
|
107 public function getStartLine()
|
Chris@13
|
108 {
|
Chris@13
|
109 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
|
Chris@13
|
110 }
|
Chris@13
|
111
|
Chris@13
|
112 /**
|
Chris@13
|
113 * Get the code end line.
|
Chris@13
|
114 *
|
Chris@13
|
115 * @throws \RuntimeException
|
Chris@13
|
116 */
|
Chris@13
|
117 public function getEndLine()
|
Chris@13
|
118 {
|
Chris@13
|
119 return $this->getStartLine();
|
Chris@13
|
120 }
|
Chris@13
|
121
|
Chris@13
|
122 /**
|
Chris@13
|
123 * Get the constant's docblock.
|
Chris@13
|
124 *
|
Chris@13
|
125 * @return false
|
Chris@13
|
126 */
|
Chris@13
|
127 public function getDocComment()
|
Chris@13
|
128 {
|
Chris@13
|
129 return false;
|
Chris@13
|
130 }
|
Chris@13
|
131
|
Chris@13
|
132 /**
|
Chris@13
|
133 * Export the constant? I don't think this is possible.
|
Chris@13
|
134 *
|
Chris@13
|
135 * @throws \RuntimeException
|
Chris@13
|
136 */
|
Chris@13
|
137 public static function export()
|
Chris@13
|
138 {
|
Chris@13
|
139 throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
|
Chris@13
|
140 }
|
Chris@13
|
141
|
Chris@13
|
142 /**
|
Chris@13
|
143 * To string.
|
Chris@13
|
144 *
|
Chris@13
|
145 * @return string
|
Chris@13
|
146 */
|
Chris@13
|
147 public function __toString()
|
Chris@13
|
148 {
|
Chris@13
|
149 return $this->getName();
|
Chris@13
|
150 }
|
Chris@13
|
151 }
|