1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Part of SplTypes package. |
5
|
|
|
* |
6
|
|
|
* (c) Adrien Loyant <[email protected]> |
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
|
|
|
declare(strict_types=1); |
13
|
|
|
|
14
|
|
|
namespace Ducks\Component\SplTypes\Reflection; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* The ReflectionEnum class reports information about an Enum. |
18
|
|
|
* |
19
|
|
|
* @link https://php.net/manual/en/class.reflectionenum.php |
20
|
|
|
*/ |
21
|
|
|
final class SplReflectionEnumHelper |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Array of \ReflectionNamedType for types |
25
|
|
|
* |
26
|
|
|
* @var \ReflectionNamedType[] |
27
|
|
|
* |
28
|
|
|
* @phpstan-var array<string,\ReflectionNamedType> |
29
|
|
|
*/ |
30
|
|
|
private static array $rnt = []; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @psalm-suppress UnusedConstructor |
34
|
|
|
*/ |
35
|
|
|
private function __construct() |
36
|
|
|
{ |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get \ReflectionNamedType from internal ReflectionFunction closure. |
41
|
|
|
* |
42
|
|
|
* @param \ReflectionFunction $func |
43
|
|
|
* |
44
|
|
|
* @return \ReflectionNamedType |
45
|
|
|
*/ |
46
|
|
|
private static function getReflectionNamedType(\ReflectionFunction $func): \ReflectionNamedType |
47
|
|
|
{ |
48
|
|
|
/** @var \ReflectionNamedType $type */ |
49
|
|
|
$type = ($func->getParameters()[0])->getType(); |
50
|
|
|
|
51
|
|
|
return $type; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get SplReflectionNamedType from a type. |
56
|
|
|
* |
57
|
|
|
* @param string $type |
58
|
|
|
* @return \ReflectionNamedType |
59
|
|
|
* |
60
|
|
|
* @phpstan-param non-empty-string $type |
61
|
|
|
* @phpstan-return \ReflectionNamedType |
62
|
|
|
*/ |
63
|
|
|
private static function getTypedReflectionNamedType(string $type): \ReflectionNamedType |
64
|
|
|
{ |
65
|
|
|
if (!isset(static::$rnt[$type])) { |
|
|
|
|
66
|
|
|
static::$rnt[$type] = new SplReflectionNamedType($type); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
return static::$rnt[$type]; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Return ReflectionNamedType from a $type (Internal use). |
74
|
|
|
* |
75
|
|
|
* @param string $type |
76
|
|
|
* @return \ReflectionNamedType |
77
|
|
|
*/ |
78
|
|
|
public static function getReflectionNamedTypeFromType(string $type): \ReflectionNamedType |
79
|
|
|
{ |
80
|
|
|
// Create a factory |
81
|
|
|
switch ($type) { |
82
|
|
|
case 'string': |
83
|
|
|
$result = self::getStringReflectionNamedType(); |
84
|
|
|
break; |
85
|
|
|
|
86
|
|
|
case 'integer': |
87
|
|
|
case 'int': |
88
|
|
|
$result = self::getIntReflectionNamedType(); |
89
|
|
|
break; |
90
|
|
|
|
91
|
|
|
case 'double': |
92
|
|
|
case 'float': |
93
|
|
|
$result = self::getFloatReflectionNamedType(); |
94
|
|
|
break; |
95
|
|
|
|
96
|
|
|
case 'boolean': |
97
|
|
|
case 'bool': |
98
|
|
|
$result = self::getBoolReflectionNamedType(); |
99
|
|
|
break; |
100
|
|
|
|
101
|
|
|
case 'array': |
102
|
|
|
$result = self::getArrayReflectionNamedType(); |
103
|
|
|
break; |
104
|
|
|
|
105
|
|
|
case 'object': |
106
|
|
|
$result = self::getObjectReflectionNamedType(); |
107
|
|
|
break; |
108
|
|
|
|
109
|
|
|
case 'mixed': |
110
|
|
|
case 'unknown type': |
111
|
|
|
$result = self::getTypedReflectionNamedType('mixed'); |
112
|
|
|
break; |
113
|
|
|
|
114
|
|
|
default: |
115
|
|
|
/** @phpstan-var non-empty-string $type */ |
116
|
|
|
$result = self::getTypedReflectionNamedType($type); |
117
|
|
|
break; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return $result; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Only way to generate a string ReflectionNamedType (Internal use). |
125
|
|
|
* |
126
|
|
|
* @return \ReflectionNamedType |
127
|
|
|
*/ |
128
|
|
|
public static function getStringReflectionNamedType(): \ReflectionNamedType |
129
|
|
|
{ |
130
|
|
|
if (!isset(static::$rnt['string'])) { |
|
|
|
|
131
|
|
|
static::$rnt['string'] = static::getReflectionNamedType( |
132
|
|
|
new \ReflectionFunction(static fn (string $param): string => $param) |
133
|
|
|
); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
return static::$rnt['string']; |
137
|
|
|
} |
138
|
|
|
|
139
|
|
|
/** |
140
|
|
|
* Only way to generate an int ReflectionNamedType (Internal use). |
141
|
|
|
* |
142
|
|
|
* @return \ReflectionNamedType |
143
|
|
|
*/ |
144
|
|
|
public static function getIntReflectionNamedType(): \ReflectionNamedType |
145
|
|
|
{ |
146
|
|
|
if (!isset(static::$rnt['int'])) { |
|
|
|
|
147
|
|
|
static::$rnt['int'] = static::getReflectionNamedType( |
148
|
|
|
new \ReflectionFunction(static fn (int $param): int => $param) |
149
|
|
|
); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
return static::$rnt['int']; |
153
|
|
|
} |
154
|
|
|
|
155
|
|
|
/** |
156
|
|
|
* Only way to generate a float ReflectionNamedType (Internal use). |
157
|
|
|
* |
158
|
|
|
* @return \ReflectionNamedType |
159
|
|
|
*/ |
160
|
|
|
public static function getFloatReflectionNamedType(): \ReflectionNamedType |
161
|
|
|
{ |
162
|
|
|
if (!isset(static::$rnt['float'])) { |
|
|
|
|
163
|
|
|
static::$rnt['float'] = static::getReflectionNamedType( |
164
|
|
|
new \ReflectionFunction(static fn (float $param): float => $param) |
165
|
|
|
); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return static::$rnt['float']; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** |
172
|
|
|
* Only way to generate a bool ReflectionNamedType (Internal use). |
173
|
|
|
* |
174
|
|
|
* @return \ReflectionNamedType |
175
|
|
|
*/ |
176
|
|
|
public static function getBoolReflectionNamedType(): \ReflectionNamedType |
177
|
|
|
{ |
178
|
|
|
if (!isset(static::$rnt['bool'])) { |
|
|
|
|
179
|
|
|
static::$rnt['bool'] = static::getReflectionNamedType( |
180
|
|
|
new \ReflectionFunction(static fn (bool $param): bool => $param) |
181
|
|
|
); |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
return static::$rnt['bool']; |
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
/** |
188
|
|
|
* Only way to generate an array ReflectionNamedType (Internal use). |
189
|
|
|
* |
190
|
|
|
* @return \ReflectionNamedType |
191
|
|
|
*/ |
192
|
|
|
public static function getArrayReflectionNamedType(): \ReflectionNamedType |
193
|
|
|
{ |
194
|
|
|
if (!isset(static::$rnt['array'])) { |
|
|
|
|
195
|
|
|
static::$rnt['array'] = static::getReflectionNamedType( |
196
|
|
|
new \ReflectionFunction(static fn (array $param): array => $param) |
197
|
|
|
); |
198
|
|
|
} |
199
|
|
|
|
200
|
|
|
return static::$rnt['array']; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Only way to generate an object ReflectionNamedType (Internal use). |
205
|
|
|
* |
206
|
|
|
* @return \ReflectionNamedType |
207
|
|
|
*/ |
208
|
|
|
public static function getObjectReflectionNamedType(): \ReflectionNamedType |
209
|
|
|
{ |
210
|
|
|
if (!isset(static::$rnt['object'])) { |
|
|
|
|
211
|
|
|
static::$rnt['object'] = static::getReflectionNamedType( |
212
|
|
|
new \ReflectionFunction(static fn (object $param): object => $param) |
213
|
|
|
); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
return static::$rnt['object']; |
217
|
|
|
} |
218
|
|
|
} |
219
|
|
|
|