@@ -34,21 +34,21 @@ abstract class Enum
34
34
*
35
35
* @var array ["$class" => ["$name" => $value, ...], ...]
36
36
*/
37
- private static $ constants = array () ;
37
+ private static $ constants = [] ;
38
38
39
39
/**
40
40
* A List of available enumerator names by enumeration class
41
41
*
42
42
* @var array ["$class" => ["$name0", ...], ...]
43
43
*/
44
- private static $ names = array () ;
44
+ private static $ names = [] ;
45
45
46
46
/**
47
47
* Already instantiated enumerators
48
48
*
49
49
* @var array ["$class" => ["$name" => $instance, ...], ...]
50
50
*/
51
- private static $ instances = array () ;
51
+ private static $ instances = [] ;
52
52
53
53
/**
54
54
* Constructor
@@ -162,20 +162,20 @@ final public function is($enumerator)
162
162
}
163
163
164
164
/**
165
- * Get an enumerator instance of the given value or instance
165
+ * Get an enumerator instance of the given enumerator value or instance
166
166
*
167
- * @param static|null|bool|int|float|string $value
167
+ * @param static|null|bool|int|float|string $enumerator
168
168
* @return static
169
169
* @throws InvalidArgumentException On an unknwon or invalid value
170
170
* @throws LogicException On ambiguous constant values
171
171
*/
172
- final public static function get ($ value )
172
+ final public static function get ($ enumerator )
173
173
{
174
- if ($ value instanceof static && \get_class ($ value ) === static ::class) {
175
- return $ value ;
174
+ if ($ enumerator instanceof static && \get_class ($ enumerator ) === static ::class) {
175
+ return $ enumerator ;
176
176
}
177
177
178
- return static ::byValue ($ value );
178
+ return static ::byValue ($ enumerator );
179
179
}
180
180
181
181
/**
@@ -188,8 +188,7 @@ final public static function get($value)
188
188
*/
189
189
final public static function byValue ($ value )
190
190
{
191
- $ class = static ::class;
192
- $ constants = self ::detectConstants ($ class );
191
+ $ constants = self ::detectConstants (static ::class);
193
192
$ name = \array_search ($ value , $ constants , true );
194
193
if ($ name === false ) {
195
194
$ message = \is_scalar ($ value )
@@ -198,11 +197,11 @@ final public static function byValue($value)
198
197
throw new InvalidArgumentException ($ message );
199
198
}
200
199
201
- if (!isset (self ::$ instances [$ class ][$ name ])) {
202
- self ::$ instances [$ class ][$ name ] = new $ class ($ constants [$ name ]);
200
+ if (!isset (self ::$ instances [static :: class][$ name ])) {
201
+ self ::$ instances [static :: class][$ name ] = new static ($ constants [$ name ]);
203
202
}
204
203
205
- return self ::$ instances [$ class ][$ name ];
204
+ return self ::$ instances [static :: class][$ name ];
206
205
}
207
206
208
207
/**
@@ -215,18 +214,17 @@ final public static function byValue($value)
215
214
*/
216
215
final public static function byName ($ name )
217
216
{
218
- $ name = (string ) $ name ;
219
- $ class = static ::class;
220
- if (isset (self ::$ instances [$ class ][$ name ])) {
221
- return self ::$ instances [$ class ][$ name ];
217
+ $ name = (string ) $ name ;
218
+ if (isset (self ::$ instances [static ::class][$ name ])) {
219
+ return self ::$ instances [static ::class][$ name ];
222
220
}
223
221
224
- $ const = $ class . ':: ' . $ name ;
222
+ $ const = static :: class . ':: ' . $ name ;
225
223
if (!\defined ($ const )) {
226
224
throw new InvalidArgumentException ($ const . ' not defined ' );
227
225
}
228
226
229
- return self ::$ instances [$ class ][$ name ] = new $ class (\constant ($ const ));
227
+ return self ::$ instances [static :: class][$ name ] = new static (\constant ($ const ));
230
228
}
231
229
232
230
/**
@@ -239,27 +237,26 @@ final public static function byName($name)
239
237
*/
240
238
final public static function byOrdinal ($ ordinal )
241
239
{
242
- $ ordinal = (int ) $ ordinal ;
243
- $ class = static ::class;
240
+ $ ordinal = (int ) $ ordinal ;
244
241
245
- if (!isset (self ::$ names [$ class ])) {
246
- self ::detectConstants ($ class );
242
+ if (!isset (self ::$ names [static :: class])) {
243
+ self ::detectConstants (static :: class);
247
244
}
248
245
249
- if (!isset (self ::$ names [$ class ][$ ordinal ])) {
246
+ if (!isset (self ::$ names [static :: class][$ ordinal ])) {
250
247
throw new InvalidArgumentException (\sprintf (
251
248
'Invalid ordinal number, must between 0 and %s ' ,
252
- \count (self ::$ names [$ class ]) - 1
249
+ \count (self ::$ names [static :: class]) - 1
253
250
));
254
251
}
255
252
256
- $ name = self ::$ names [$ class ][$ ordinal ];
257
- if (isset (self ::$ instances [$ class ][$ name ])) {
258
- return self ::$ instances [$ class ][$ name ];
253
+ $ name = self ::$ names [static :: class][$ ordinal ];
254
+ if (isset (self ::$ instances [static :: class][$ name ])) {
255
+ return self ::$ instances [static :: class][$ name ];
259
256
}
260
257
261
- $ const = $ class . ':: ' . $ name ;
262
- return self ::$ instances [$ class ][$ name ] = new $ class (\constant ($ const ), $ ordinal );
258
+ $ const = static :: class . ':: ' . $ name ;
259
+ return self ::$ instances [static :: class][$ name ] = new static (\constant ($ const ), $ ordinal );
263
260
}
264
261
265
262
/**
@@ -305,7 +302,7 @@ final public static function getNames()
305
302
final public static function getOrdinals ()
306
303
{
307
304
$ count = \count (self ::detectConstants (static ::class));
308
- return $ count === 0 ? array () : \range (0 , $ count - 1 );
305
+ return $ count === 0 ? [] : \range (0 , $ count - 1 );
309
306
}
310
307
311
308
/**
@@ -346,10 +343,10 @@ private static function detectConstants($class)
346
343
{
347
344
if (!isset (self ::$ constants [$ class ])) {
348
345
$ reflection = new ReflectionClass ($ class );
349
- $ constants = array () ;
346
+ $ constants = [] ;
350
347
351
348
do {
352
- $ scopeConstants = array () ;
349
+ $ scopeConstants = [] ;
353
350
if (PHP_VERSION_ID >= 70100 ) {
354
351
// Since PHP-7.1 visibility modifiers are allowed for class constants
355
352
// for enumerations we are only interested in public once.
@@ -367,7 +364,7 @@ private static function detectConstants($class)
367
364
} while (($ reflection = $ reflection ->getParentClass ()) && $ reflection ->name !== __CLASS__ );
368
365
369
366
// Detect ambiguous values and report names
370
- $ ambiguous = array () ;
367
+ $ ambiguous = [] ;
371
368
foreach ($ constants as $ value ) {
372
369
$ names = \array_keys ($ constants , $ value , true );
373
370
if (\count ($ names ) > 1 ) {
0 commit comments