@@ -20,7 +20,7 @@ abstract class Enum
20
20
/**
21
21
* The selected enumerator value
22
22
*
23
- * @var null|bool|int|float|string|array
23
+ * @var null|bool|int|float|string|mixed[]
24
24
*/
25
25
private $ value ;
26
26
@@ -34,29 +34,29 @@ abstract class Enum
34
34
/**
35
35
* A map of enumerator names and values by enumeration class
36
36
*
37
- * @var array ["$ class" => ["$name" => $value, ...], ...]
37
+ * @var array< class-string<Enum>, array<string, null|bool|int|float|string|mixed[]>>
38
38
*/
39
39
private static $ constants = [];
40
40
41
41
/**
42
42
* A List of available enumerator names by enumeration class
43
43
*
44
- * @var array ["$ class" => ["$name0", ...], ...]
44
+ * @var array< class-string<Enum>, string[]>
45
45
*/
46
46
private static $ names = [];
47
47
48
48
/**
49
- * Already instantiated enumerators
49
+ * A map of enumerator names and instances by enumeration class
50
50
*
51
- * @var array ["$ class" => ["$name" => $instance, ...], ...]
51
+ * @var array< class-string<Enum>, array<string, Enum>>
52
52
*/
53
53
private static $ instances = [];
54
54
55
55
/**
56
56
* Constructor
57
57
*
58
- * @param null|bool|int|float|string|array $value The value of the enumerator
59
- * @param int|null $ordinal The ordinal number of the enumerator
58
+ * @param null|bool|int|float|string|mixed[] $value The value of the enumerator
59
+ * @param int|null $ordinal The ordinal number of the enumerator
60
60
*/
61
61
final private function __construct ($ value , $ ordinal = null )
62
62
{
@@ -105,7 +105,7 @@ final public function __wakeup()
105
105
/**
106
106
* Get the value of the enumerator
107
107
*
108
- * @return null|bool|int|float|string|array
108
+ * @return null|bool|int|float|string|mixed[]
109
109
*/
110
110
final public function getValue ()
111
111
{
@@ -149,7 +149,7 @@ final public function getOrdinal()
149
149
/**
150
150
* Compare this enumerator against another and check if it's the same.
151
151
*
152
- * @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
152
+ * @param static|null|bool|int|float|string|mixed[] $enumerator An enumerator object or value
153
153
* @return bool
154
154
*/
155
155
final public function is ($ enumerator )
@@ -166,14 +166,22 @@ final public function is($enumerator)
166
166
/**
167
167
* Get an enumerator instance of the given enumerator value or instance
168
168
*
169
- * @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
169
+ * @param static|null|bool|int|float|string|mixed[] $enumerator An enumerator object or value
170
170
* @return static
171
171
* @throws InvalidArgumentException On an unknown or invalid value
172
172
* @throws LogicException On ambiguous constant values
173
173
*/
174
174
final public static function get ($ enumerator )
175
175
{
176
- if ($ enumerator instanceof static && \get_class ($ enumerator ) === static ::class) {
176
+ if ($ enumerator instanceof static) {
177
+ if (\get_class ($ enumerator ) !== static ::class) {
178
+ throw new InvalidArgumentException (sprintf (
179
+ 'Invalid value of type %s for enumeration %s ' ,
180
+ \get_class ($ enumerator ),
181
+ static ::class
182
+ ));
183
+ }
184
+
177
185
return $ enumerator ;
178
186
}
179
187
@@ -183,13 +191,15 @@ final public static function get($enumerator)
183
191
/**
184
192
* Get an enumerator instance by the given value
185
193
*
186
- * @param null|bool|int|float|string|array $value Enumerator value
194
+ * @param null|bool|int|float|string|mixed[] $value Enumerator value
187
195
* @return static
188
196
* @throws InvalidArgumentException On an unknown or invalid value
189
197
* @throws LogicException On ambiguous constant values
190
198
*/
191
199
final public static function byValue ($ value )
192
200
{
201
+ /** @var mixed $value */
202
+
193
203
$ constants = self ::$ constants [static ::class] ?? static ::getConstants ();
194
204
195
205
$ name = \array_search ($ value , $ constants , true );
@@ -203,8 +213,11 @@ final public static function byValue($value)
203
213
));
204
214
}
205
215
206
- return self ::$ instances [static ::class][$ name ]
216
+ /** @var static $instance */
217
+ $ instance = self ::$ instances [static ::class][$ name ]
207
218
?? self ::$ instances [static ::class][$ name ] = new static ($ constants [$ name ]);
219
+
220
+ return $ instance ;
208
221
}
209
222
210
223
/**
@@ -218,7 +231,9 @@ final public static function byValue($value)
218
231
final public static function byName (string $ name )
219
232
{
220
233
if (isset (self ::$ instances [static ::class][$ name ])) {
221
- return self ::$ instances [static ::class][$ name ];
234
+ /** @var static $instance */
235
+ $ instance = self ::$ instances [static ::class][$ name ];
236
+ return $ instance ;
222
237
}
223
238
224
239
$ const = static ::class . ":: {$ name }" ;
@@ -255,8 +270,12 @@ final public static function byOrdinal(int $ordinal)
255
270
}
256
271
257
272
$ name = self ::$ names [static ::class][$ ordinal ];
258
- return self ::$ instances [static ::class][$ name ]
273
+
274
+ /** @var static $instance */
275
+ $ instance = self ::$ instances [static ::class][$ name ]
259
276
?? self ::$ instances [static ::class][$ name ] = new static ($ constants [$ name ], $ ordinal );
277
+
278
+ return $ instance ;
260
279
}
261
280
262
281
/**
@@ -269,13 +288,16 @@ final public static function getEnumerators()
269
288
if (!isset (self ::$ names [static ::class])) {
270
289
static ::getConstants ();
271
290
}
272
- return \array_map ([static ::class, 'byName ' ], self ::$ names [static ::class]);
291
+
292
+ /** @var callable $byNameFn */
293
+ $ byNameFn = [static ::class, 'byName ' ];
294
+ return \array_map ($ byNameFn , self ::$ names [static ::class]);
273
295
}
274
296
275
297
/**
276
298
* Get a list of enumerator values ordered by ordinal number
277
299
*
278
- * @return mixed[]
300
+ * @return (null|bool|int|float|string| mixed[]) []
279
301
*/
280
302
final public static function getValues ()
281
303
{
@@ -309,7 +331,7 @@ final public static function getOrdinals()
309
331
/**
310
332
* Get all available constants of the called class
311
333
*
312
- * @return array
334
+ * @return array<string, null|bool|int|float|string|mixed[]>
313
335
* @throws LogicException On ambiguous constant values
314
336
*/
315
337
final public static function getConstants ()
@@ -344,7 +366,7 @@ final public static function getConstants()
344
366
345
367
/**
346
368
* Test that the given constants does not contain ambiguous values
347
- * @param array $constants
369
+ * @param array<string, null|bool|int|float|string|mixed[]> $constants
348
370
* @return bool
349
371
*/
350
372
private static function noAmbiguousValues ($ constants )
@@ -362,19 +384,22 @@ private static function noAmbiguousValues($constants)
362
384
/**
363
385
* Test if the given enumerator is part of this enumeration
364
386
*
365
- * @param static|null|bool|int|float|string|array $enumerator
387
+ * @param static|null|bool|int|float|string|mixed[] $enumerator
366
388
* @return bool
367
389
*/
368
390
final public static function has ($ enumerator )
369
391
{
370
- return ($ enumerator instanceof static && \get_class ($ enumerator ) === static ::class)
371
- || static ::hasValue ($ enumerator );
392
+ if ($ enumerator instanceof static) {
393
+ return \get_class ($ enumerator ) === static ::class;
394
+ }
395
+
396
+ return static ::hasValue ($ enumerator );
372
397
}
373
398
374
399
/**
375
400
* Test if the given enumerator value is part of this enumeration
376
401
*
377
- * @param null|bool|int|float|string|array $value
402
+ * @param null|bool|int|float|string|mixed[] $value
378
403
* @return bool
379
404
*/
380
405
final public static function hasValue ($ value )
@@ -399,8 +424,8 @@ final public static function hasName(string $name)
399
424
* This will be called automatically on calling a method
400
425
* with the same name of a defined enumerator.
401
426
*
402
- * @param string $method The name of the enumerator (called as method)
403
- * @param array $args There should be no arguments
427
+ * @param string $method The name of the enumerator (called as method)
428
+ * @param array<mixed> $args There should be no arguments
404
429
* @return static
405
430
* @throws InvalidArgumentException On an invalid or unknown name
406
431
* @throws LogicException On ambiguous constant values
0 commit comments