Subversion-Projekte lars-tiefland.laravel_shop

Revision

Zur aktuellen Revision | Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
/*
4
 * This file is part of Psy Shell.
5
 *
6
 * (c) 2012-2022 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 didn't include class constants until 7.1.
16
 *
17
 * ReflectionClassConstant corrects that omission.
18
 */
19
class ReflectionClassConstant implements \Reflector
20
{
21
    public $class;
22
    public $name;
23
    private $value;
24
 
25
    /**
26
     * Construct a ReflectionClassConstant object.
27
     *
28
     * @param string|object $class
29
     * @param string        $name
30
     */
31
    public function __construct($class, string $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
     * Exports a reflection.
50
     *
51
     * @param string|object $class
52
     * @param string        $name
53
     * @param bool          $return pass true to return the export, as opposed to emitting it
54
     *
55
     * @return string|null
56
     */
57
    public static function export($class, string $name, bool $return = false)
58
    {
59
        $refl = new self($class, $name);
60
        $value = $refl->getValue();
61
 
62
        $str = \sprintf('Constant [ public %s %s ] { %s }', \gettype($value), $refl->getName(), $value);
63
 
64
        if ($return) {
65
            return $str;
66
        }
67
 
68
        echo $str."\n";
69
    }
70
 
71
    /**
72
     * Gets the declaring class.
73
     *
74
     * @return \ReflectionClass
75
     */
76
    public function getDeclaringClass(): \ReflectionClass
77
    {
78
        $parent = $this->class;
79
 
80
        // Since we don't have real reflection constants, we can't see where
81
        // it's actually defined. Let's check for a constant that is also
82
        // available on the parent class which has exactly the same value.
83
        //
84
        // While this isn't _technically_ correct, it's prolly close enough.
85
        do {
86
            $class = $parent;
87
            $parent = $class->getParentClass();
88
        } while ($parent && $parent->hasConstant($this->name) && $parent->getConstant($this->name) === $this->value);
89
 
90
        return $class;
91
    }
92
 
93
    /**
94
     * Get the constant's docblock.
95
     *
96
     * @return false
97
     */
98
    public function getDocComment(): bool
99
    {
100
        return false;
101
    }
102
 
103
    /**
104
     * Gets the class constant modifiers.
105
     *
106
     * Since this is only used for PHP < 7.1, we can just return "public". All
107
     * the fancier modifiers are only available on PHP versions which have their
108
     * own ReflectionClassConstant class :)
109
     *
110
     * @return int
111
     */
112
    public function getModifiers(): int
113
    {
114
        return \ReflectionMethod::IS_PUBLIC;
115
    }
116
 
117
    /**
118
     * Gets the constant name.
119
     *
120
     * @return string
121
     */
122
    public function getName(): string
123
    {
124
        return $this->name;
125
    }
126
 
127
    /**
128
     * Gets the value of the constant.
129
     *
130
     * @return mixed
131
     */
132
    public function getValue()
133
    {
134
        return $this->value;
135
    }
136
 
137
    /**
138
     * Checks if class constant is private.
139
     *
140
     * @return bool false
141
     */
142
    public function isPrivate(): bool
143
    {
144
        return false;
145
    }
146
 
147
    /**
148
     * Checks if class constant is protected.
149
     *
150
     * @return bool false
151
     */
152
    public function isProtected(): bool
153
    {
154
        return false;
155
    }
156
 
157
    /**
158
     * Checks if class constant is public.
159
     *
160
     * @return bool true
161
     */
162
    public function isPublic(): bool
163
    {
164
        return true;
165
    }
166
 
167
    /**
168
     * To string.
169
     *
170
     * @return string
171
     */
172
    public function __toString(): string
173
    {
174
        return $this->getName();
175
    }
176
 
177
    /**
178
     * Gets the constant's file name.
179
     *
180
     * Currently returns null, because if it returns a file name the signature
181
     * formatter will barf.
182
     */
183
    public function getFileName()
184
    {
185
        return;
186
        // return $this->class->getFileName();
187
    }
188
 
189
    /**
190
     * Get the code start line.
191
     *
192
     * @throws \RuntimeException
193
     */
194
    public function getStartLine()
195
    {
196
        throw new \RuntimeException('Not yet implemented because it\'s unclear what I should do here :)');
197
    }
198
 
199
    /**
200
     * Get the code end line.
201
     *
202
     * @throws \RuntimeException
203
     */
204
    public function getEndLine()
205
    {
206
        return $this->getStartLine();
207
    }
208
 
209
    /**
210
     * Get a ReflectionClassConstant instance.
211
     *
212
     * In PHP >= 7.1, this will return a \ReflectionClassConstant from the
213
     * standard reflection library. For older PHP, it will return this polyfill.
214
     *
215
     * @param string|object $class
216
     * @param string        $name
217
     *
218
     * @return ReflectionClassConstant|\ReflectionClassConstant
219
     */
220
    public static function create($class, string $name)
221
    {
222
        if (\class_exists(\ReflectionClassConstant::class)) {
223
            return new \ReflectionClassConstant($class, $name);
224
        }
225
 
226
        return new self($class, $name);
227
    }
228
}