b355e350912e9e2547936a8308fdfef5b5ca8b80
[yaffs-website] / vendor / psy / psysh / src / Reflection / ReflectionConstant_.php
1 <?php
2
3 /*
4  * This file is part of Psy Shell.
5  *
6  * (c) 2012-2018 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  * Note: For backwards compatibility reasons, this class is named
20  * ReflectionConstant_ rather than ReflectionConstant. It will be renamed in
21  * v0.10.0.
22  */
23 class ReflectionConstant_ implements \Reflector
24 {
25     public $name;
26     private $value;
27
28     private static $magicConstants = [
29         '__LINE__',
30         '__FILE__',
31         '__DIR__',
32         '__FUNCTION__',
33         '__CLASS__',
34         '__TRAIT__',
35         '__METHOD__',
36         '__NAMESPACE__',
37         '__COMPILER_HALT_OFFSET__',
38     ];
39
40     /**
41      * Construct a ReflectionConstant_ object.
42      *
43      * @param string $name
44      */
45     public function __construct($name)
46     {
47         $this->name = $name;
48
49         if (!\defined($name) && !self::isMagicConstant($name)) {
50             throw new \InvalidArgumentException('Unknown constant: ' . $name);
51         }
52
53         if (!self::isMagicConstant($name)) {
54             $this->value = @\constant($name);
55         }
56     }
57
58     /**
59      * Exports a reflection.
60      *
61      * @param string $name
62      * @param bool   $return pass true to return the export, as opposed to emitting it
63      *
64      * @return null|string
65      */
66     public static function export($name, $return = false)
67     {
68         $refl = new self($name);
69         $value = $refl->getValue();
70
71         $str = \sprintf('Constant [ %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
72
73         if ($return) {
74             return $str;
75         }
76
77         echo $str . "\n";
78     }
79
80     public static function isMagicConstant($name)
81     {
82         return \in_array($name, self::$magicConstants);
83     }
84
85     /**
86      * Get the constant's docblock.
87      *
88      * @return false
89      */
90     public function getDocComment()
91     {
92         return false;
93     }
94
95     /**
96      * Gets the constant name.
97      *
98      * @return string
99      */
100     public function getName()
101     {
102         return $this->name;
103     }
104
105     /**
106      * Gets the namespace name.
107      *
108      * Returns '' when the constant is not namespaced.
109      *
110      * @return string
111      */
112     public function getNamespaceName()
113     {
114         if (!$this->inNamespace()) {
115             return '';
116         }
117
118         return \preg_replace('/\\\\[^\\\\]+$/', '', $this->name);
119     }
120
121     /**
122      * Gets the value of the constant.
123      *
124      * @return mixed
125      */
126     public function getValue()
127     {
128         return $this->value;
129     }
130
131     /**
132      * Checks if this constant is defined in a namespace.
133      *
134      * @return bool
135      */
136     public function inNamespace()
137     {
138         return \strpos($this->name, '\\') !== false;
139     }
140
141     /**
142      * To string.
143      *
144      * @return string
145      */
146     public function __toString()
147     {
148         return $this->getName();
149     }
150
151     /**
152      * Gets the constant's file name.
153      *
154      * Currently returns null, because if it returns a file name the signature
155      * formatter will barf.
156      */
157     public function getFileName()
158     {
159         return;
160         // return $this->class->getFileName();
161     }
162
163     /**
164      * Get the code start line.
165      *
166      * @throws \RuntimeException
167      */
168     public function getStartLine()
169     {
170         throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
171     }
172
173     /**
174      * Get the code end line.
175      *
176      * @throws \RuntimeException
177      */
178     public function getEndLine()
179     {
180         return $this->getStartLine();
181     }
182 }