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