1
1
<?php
2
2
3
+ declare (strict_types=1 );
4
+
3
5
namespace MabeEnum ;
4
6
5
7
use ReflectionClass ;
@@ -68,7 +70,7 @@ final private function __construct($value, $ordinal = null)
68
70
* @return string
69
71
* @see getName()
70
72
*/
71
- public function __toString ()
73
+ public function __toString (): string
72
74
{
73
75
return $ this ->getName ();
74
76
}
@@ -115,7 +117,7 @@ final public function getValue()
115
117
*
116
118
* @return string
117
119
*/
118
- final public function getName ()
120
+ final public function getName (): string
119
121
{
120
122
$ ordinal = $ this ->ordinal !== null ? $ this ->ordinal : $ this ->getOrdinal ();
121
123
return self ::$ names [static ::class][$ ordinal ];
@@ -126,7 +128,7 @@ final public function getName()
126
128
*
127
129
* @return int
128
130
*/
129
- final public function getOrdinal ()
131
+ final public function getOrdinal (): int
130
132
{
131
133
if ($ this ->ordinal === null ) {
132
134
$ ordinal = 0 ;
@@ -147,10 +149,10 @@ final public function getOrdinal()
147
149
/**
148
150
* Compare this enumerator against another and check if it's the same.
149
151
*
150
- * @param mixed $enumerator
152
+ * @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
151
153
* @return bool
152
154
*/
153
- final public function is ($ enumerator )
155
+ final public function is ($ enumerator ): bool
154
156
{
155
157
return $ this === $ enumerator || $ this ->value === $ enumerator
156
158
@@ -164,12 +166,12 @@ final public function is($enumerator)
164
166
/**
165
167
* Get an enumerator instance of the given enumerator value or instance
166
168
*
167
- * @param static|null|bool|int|float|string|array $enumerator
169
+ * @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
168
170
* @return static
169
171
* @throws InvalidArgumentException On an unknwon or invalid value
170
172
* @throws LogicException On ambiguous constant values
171
173
*/
172
- final public static function get ($ enumerator )
174
+ final public static function get ($ enumerator ): self
173
175
{
174
176
if ($ enumerator instanceof static && \get_class ($ enumerator ) === static ::class) {
175
177
return $ enumerator ;
@@ -181,12 +183,12 @@ final public static function get($enumerator)
181
183
/**
182
184
* Get an enumerator instance by the given value
183
185
*
184
- * @param null|bool|int|float|string|array $value
186
+ * @param null|bool|int|float|string|array $value Enumerator value
185
187
* @return static
186
188
* @throws InvalidArgumentException On an unknwon or invalid value
187
189
* @throws LogicException On ambiguous constant values
188
190
*/
189
- final public static function byValue ($ value )
191
+ final public static function byValue ($ value ): self
190
192
{
191
193
if (!isset (self ::$ constants [static ::class])) {
192
194
self ::detectConstants (static ::class);
@@ -218,9 +220,8 @@ final public static function byValue($value)
218
220
* @throws InvalidArgumentException On an invalid or unknown name
219
221
* @throws LogicException On ambiguous values
220
222
*/
221
- final public static function byName ($ name )
223
+ final public static function byName (string $ name ): self
222
224
{
223
- $ name = (string ) $ name ;
224
225
if (isset (self ::$ instances [static ::class][$ name ])) {
225
226
return self ::$ instances [static ::class][$ name ];
226
227
}
@@ -236,15 +237,13 @@ final public static function byName($name)
236
237
/**
237
238
* Get an enumeration instance by the given ordinal number
238
239
*
239
- * @param int $ordinal The ordinal number or the enumerator
240
+ * @param int $ordinal The ordinal number of the enumerator
240
241
* @return static
241
242
* @throws InvalidArgumentException On an invalid ordinal number
242
243
* @throws LogicException On ambiguous values
243
244
*/
244
- final public static function byOrdinal ($ ordinal )
245
+ final public static function byOrdinal (int $ ordinal ): self
245
246
{
246
- $ ordinal = (int ) $ ordinal ;
247
-
248
247
if (!isset (self ::$ names [static ::class])) {
249
248
self ::detectConstants (static ::class);
250
249
}
@@ -269,7 +268,7 @@ final public static function byOrdinal($ordinal)
269
268
*
270
269
* @return static[]
271
270
*/
272
- final public static function getEnumerators ()
271
+ final public static function getEnumerators (): array
273
272
{
274
273
if (!isset (self ::$ names [static ::class])) {
275
274
self ::detectConstants (static ::class);
@@ -282,7 +281,7 @@ final public static function getEnumerators()
282
281
*
283
282
* @return mixed[]
284
283
*/
285
- final public static function getValues ()
284
+ final public static function getValues (): array
286
285
{
287
286
return \array_values (self ::detectConstants (static ::class));
288
287
}
@@ -292,7 +291,7 @@ final public static function getValues()
292
291
*
293
292
* @return string[]
294
293
*/
295
- final public static function getNames ()
294
+ final public static function getNames (): array
296
295
{
297
296
if (!isset (self ::$ names [static ::class])) {
298
297
self ::detectConstants (static ::class);
@@ -305,7 +304,7 @@ final public static function getNames()
305
304
*
306
305
* @return int[]
307
306
*/
308
- final public static function getOrdinals ()
307
+ final public static function getOrdinals (): array
309
308
{
310
309
$ count = \count (self ::detectConstants (static ::class));
311
310
return $ count === 0 ? [] : \range (0 , $ count - 1 );
@@ -317,7 +316,7 @@ final public static function getOrdinals()
317
316
* @return array
318
317
* @throws LogicException On ambiguous constant values
319
318
*/
320
- final public static function getConstants ()
319
+ final public static function getConstants (): array
321
320
{
322
321
return self ::detectConstants (static ::class);
323
322
}
@@ -328,7 +327,7 @@ final public static function getConstants()
328
327
* @param static|null|bool|int|float|string|array $enumerator
329
328
* @return bool
330
329
*/
331
- final public static function has ($ enumerator )
330
+ final public static function has ($ enumerator ): bool
332
331
{
333
332
if ($ enumerator instanceof static && \get_class ($ enumerator ) === static ::class) {
334
333
return true ;
@@ -343,7 +342,7 @@ final public static function has($enumerator)
343
342
* @param null|bool|int|float|string|array $value
344
343
* @return bool
345
344
*/
346
- final public static function hasValue ($ value )
345
+ final public static function hasValue ($ value ): bool
347
346
{
348
347
$ constants = self ::detectConstants (static ::class);
349
348
return \in_array ($ value , $ constants , true );
@@ -355,9 +354,9 @@ final public static function hasValue($value)
355
354
* @param string $name
356
355
* @return bool
357
356
*/
358
- final public static function hasName ($ name )
357
+ final public static function hasName (string $ name ): bool
359
358
{
360
- return \is_string ( $ name ) && \ defined ("static:: $ name " );
359
+ return \defined ("static:: $ name " );
361
360
}
362
361
363
362
/**
@@ -366,7 +365,7 @@ final public static function hasName($name)
366
365
* @param string $class
367
366
* @return array
368
367
*/
369
- private static function detectConstants ($ class )
368
+ private static function detectConstants (string $ class ): array
370
369
{
371
370
if (!isset (self ::$ constants [$ class ])) {
372
371
$ reflection = new ReflectionClass ($ class );
@@ -409,7 +408,7 @@ private static function detectConstants($class)
409
408
* @param array $constants
410
409
* @return bool
411
410
*/
412
- private static function noAmbiguousValues (array $ constants )
411
+ private static function noAmbiguousValues (array $ constants ): bool
413
412
{
414
413
foreach ($ constants as $ value ) {
415
414
$ names = \array_keys ($ constants , $ value , true );
@@ -433,7 +432,7 @@ private static function noAmbiguousValues(array $constants)
433
432
* @throws InvalidArgumentException On an invalid or unknown name
434
433
* @throws LogicException On ambiguous constant values
435
434
*/
436
- final public static function __callStatic ($ method , array $ args )
435
+ final public static function __callStatic (string $ method , array $ args ): self
437
436
{
438
437
return self ::byName ($ method );
439
438
}
0 commit comments