@@ -22,7 +22,7 @@ abstract class Enum
22
22
/**
23
23
* The selected enumerator value
24
24
*
25
- * @var null|bool|int|float|string|array
25
+ * @var null|bool|int|float|string|array<mixed>
26
26
*/
27
27
private $ value ;
28
28
@@ -36,29 +36,29 @@ abstract class Enum
36
36
/**
37
37
* A map of enumerator names and values by enumeration class
38
38
*
39
- * @var array ["$ class" => ["$name" => $value, ...], ...]
39
+ * @var array< class-string<Enum>, array<string, null|bool|int|float|string|array<mixed>>>
40
40
*/
41
41
private static $ constants = [];
42
42
43
43
/**
44
44
* A List of available enumerator names by enumeration class
45
45
*
46
- * @var array ["$ class" => ["$name0", ...], ...]
46
+ * @var array< class-string<Enum>, string[]>
47
47
*/
48
48
private static $ names = [];
49
49
50
50
/**
51
- * Already instantiated enumerators
51
+ * A map of enumerator names and instances by enumeration class
52
52
*
53
- * @var array ["$ class" => ["$name" => $instance, ...], ...]
53
+ * @var array< class-string<Enum>, array<string, Enum>>
54
54
*/
55
55
private static $ instances = [];
56
56
57
57
/**
58
58
* Constructor
59
59
*
60
- * @param null|bool|int|float|string|array $value The value of the enumerator
61
- * @param int|null $ordinal The ordinal number of the enumerator
60
+ * @param null|bool|int|float|string|array<mixed> $value The value of the enumerator
61
+ * @param int|null $ordinal The ordinal number of the enumerator
62
62
*/
63
63
final private function __construct ($ value , $ ordinal = null )
64
64
{
@@ -111,7 +111,7 @@ final public function __wakeup()
111
111
/**
112
112
* Get the value of the enumerator
113
113
*
114
- * @return null|bool|int|float|string|array
114
+ * @return null|bool|int|float|string|array<mixed>
115
115
*/
116
116
final public function getValue ()
117
117
{
@@ -123,6 +123,7 @@ final public function getValue()
123
123
*
124
124
* @return string
125
125
*
126
+ * @phpstan-return string
126
127
* @psalm-return non-empty-string
127
128
*/
128
129
final public function getName ()
@@ -157,7 +158,7 @@ final public function getOrdinal()
157
158
/**
158
159
* Compare this enumerator against another and check if it's the same.
159
160
*
160
- * @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
161
+ * @param static|null|bool|int|float|string|array<mixed> $enumerator An enumerator object or value
161
162
* @return bool
162
163
*/
163
164
final public function is ($ enumerator )
@@ -174,7 +175,7 @@ final public function is($enumerator)
174
175
/**
175
176
* Get an enumerator instance of the given enumerator value or instance
176
177
*
177
- * @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
178
+ * @param static|null|bool|int|float|string|array<mixed> $enumerator An enumerator object or value
178
179
* @return static
179
180
* @throws InvalidArgumentException On an unknown or invalid value
180
181
* @throws LogicException On ambiguous constant values
@@ -183,7 +184,15 @@ final public function is($enumerator)
183
184
*/
184
185
final public static function get ($ enumerator )
185
186
{
186
- if ($ enumerator instanceof static && \get_class ($ enumerator ) === static ::class) {
187
+ if ($ enumerator instanceof static) {
188
+ if (\get_class ($ enumerator ) !== static ::class) {
189
+ throw new InvalidArgumentException (sprintf (
190
+ 'Invalid value of type %s for enumeration %s ' ,
191
+ \get_class ($ enumerator ),
192
+ static ::class
193
+ ));
194
+ }
195
+
187
196
return $ enumerator ;
188
197
}
189
198
@@ -193,7 +202,7 @@ final public static function get($enumerator)
193
202
/**
194
203
* Get an enumerator instance by the given value
195
204
*
196
- * @param null|bool|int|float|string|array $value Enumerator value
205
+ * @param null|bool|int|float|string|array<mixed> $value Enumerator value
197
206
* @return static
198
207
* @throws InvalidArgumentException On an unknown or invalid value
199
208
* @throws LogicException On ambiguous constant values
@@ -202,6 +211,8 @@ final public static function get($enumerator)
202
211
*/
203
212
final public static function byValue ($ value )
204
213
{
214
+ /** @var mixed $value */
215
+
205
216
$ constants = self ::$ constants [static ::class] ?? static ::getConstants ();
206
217
207
218
$ name = \array_search ($ value , $ constants , true );
@@ -215,8 +226,11 @@ final public static function byValue($value)
215
226
));
216
227
}
217
228
218
- return self ::$ instances [static ::class][$ name ]
229
+ /** @var static $instance */
230
+ $ instance = self ::$ instances [static ::class][$ name ]
219
231
?? self ::$ instances [static ::class][$ name ] = new static ($ constants [$ name ]);
232
+
233
+ return $ instance ;
220
234
}
221
235
222
236
/**
@@ -232,7 +246,9 @@ final public static function byValue($value)
232
246
final public static function byName (string $ name )
233
247
{
234
248
if (isset (self ::$ instances [static ::class][$ name ])) {
235
- return self ::$ instances [static ::class][$ name ];
249
+ /** @var static $instance */
250
+ $ instance = self ::$ instances [static ::class][$ name ];
251
+ return $ instance ;
236
252
}
237
253
238
254
$ const = static ::class . ":: {$ name }" ;
@@ -271,15 +287,20 @@ final public static function byOrdinal(int $ordinal)
271
287
}
272
288
273
289
$ name = self ::$ names [static ::class][$ ordinal ];
274
- return self ::$ instances [static ::class][$ name ]
290
+
291
+ /** @var static $instance */
292
+ $ instance = self ::$ instances [static ::class][$ name ]
275
293
?? self ::$ instances [static ::class][$ name ] = new static ($ constants [$ name ], $ ordinal );
294
+
295
+ return $ instance ;
276
296
}
277
297
278
298
/**
279
299
* Get a list of enumerator instances ordered by ordinal number
280
300
*
281
301
* @return static[]
282
302
*
303
+ * @phpstan-return array<int, static>
283
304
* @psalm-return list<static>
284
305
* @psalm-pure
285
306
*/
@@ -288,14 +309,18 @@ final public static function getEnumerators()
288
309
if (!isset (self ::$ names [static ::class])) {
289
310
static ::getConstants ();
290
311
}
291
- return \array_map ([static ::class, 'byName ' ], self ::$ names [static ::class]);
312
+
313
+ /** @var callable $byNameFn */
314
+ $ byNameFn = [static ::class, 'byName ' ];
315
+ return \array_map ($ byNameFn , self ::$ names [static ::class]);
292
316
}
293
317
294
318
/**
295
319
* Get a list of enumerator values ordered by ordinal number
296
320
*
297
- * @return mixed []
321
+ * @return (null|bool|int|float|string|array) []
298
322
*
323
+ * @phpstan-return array<int, null|bool|int|float|string|array>
299
324
* @psalm-return list<null|bool|int|float|string|array>
300
325
* @psalm-pure
301
326
*/
@@ -309,6 +334,7 @@ final public static function getValues()
309
334
*
310
335
* @return string[]
311
336
*
337
+ * @phpstan-return array<int, string>
312
338
* @psalm-return list<non-empty-string>
313
339
* @psalm-pure
314
340
*/
@@ -325,6 +351,7 @@ final public static function getNames()
325
351
*
326
352
* @return int[]
327
353
*
354
+ * @phpstan-return array<int, int>
328
355
* @psalm-return list<int>
329
356
* @psalm-pure
330
357
*/
@@ -337,9 +364,10 @@ final public static function getOrdinals()
337
364
/**
338
365
* Get all available constants of the called class
339
366
*
340
- * @return mixed []
367
+ * @return (null|bool|int|float|string|array) []
341
368
* @throws LogicException On ambiguous constant values
342
369
*
370
+ * @phpstan-return array<string, null|bool|int|float|string|array>
343
371
* @psalm-return array<non-empty-string, null|bool|int|float|string|array>
344
372
* @psalm-pure
345
373
*/
@@ -375,7 +403,7 @@ final public static function getConstants()
375
403
376
404
/**
377
405
* Test that the given constants does not contain ambiguous values
378
- * @param array $constants
406
+ * @param array<string, null|bool|int|float|string|array<mixed>> $constants
379
407
* @return bool
380
408
*/
381
409
private static function noAmbiguousValues ($ constants )
@@ -393,21 +421,24 @@ private static function noAmbiguousValues($constants)
393
421
/**
394
422
* Test if the given enumerator is part of this enumeration
395
423
*
396
- * @param static|null|bool|int|float|string|array $enumerator
424
+ * @param static|null|bool|int|float|string|array<mixed> $enumerator
397
425
* @return bool
398
426
*
399
427
* @psalm-pure
400
428
*/
401
429
final public static function has ($ enumerator )
402
430
{
403
- return ($ enumerator instanceof static && \get_class ($ enumerator ) === static ::class)
404
- || static ::hasValue ($ enumerator );
431
+ if ($ enumerator instanceof static) {
432
+ return \get_class ($ enumerator ) === static ::class;
433
+ }
434
+
435
+ return static ::hasValue ($ enumerator );
405
436
}
406
437
407
438
/**
408
439
* Test if the given enumerator value is part of this enumeration
409
440
*
410
- * @param null|bool|int|float|string|array $value
441
+ * @param null|bool|int|float|string|array<mixed> $value
411
442
* @return bool
412
443
*
413
444
* @psalm-pure
@@ -436,8 +467,8 @@ final public static function hasName(string $name)
436
467
* This will be called automatically on calling a method
437
468
* with the same name of a defined enumerator.
438
469
*
439
- * @param string $method The name of the enumerator (called as method)
440
- * @param array $args There should be no arguments
470
+ * @param string $method The name of the enumerator (called as method)
471
+ * @param array<mixed> $args There should be no arguments
441
472
* @return static
442
473
* @throws InvalidArgumentException On an invalid or unknown name
443
474
* @throws LogicException On ambiguous constant values
0 commit comments