Skip to content

Commit 1058fca

Browse files
committed
strict types + argument and return type-hints
1 parent 8d82781 commit 1058fca

File tree

6 files changed

+92
-115
lines changed

6 files changed

+92
-115
lines changed

src/Enum.php

Lines changed: 26 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MabeEnum;
46

57
use ReflectionClass;
@@ -68,7 +70,7 @@ final private function __construct($value, $ordinal = null)
6870
* @return string
6971
* @see getName()
7072
*/
71-
public function __toString()
73+
public function __toString(): string
7274
{
7375
return $this->getName();
7476
}
@@ -115,7 +117,7 @@ final public function getValue()
115117
*
116118
* @return string
117119
*/
118-
final public function getName()
120+
final public function getName(): string
119121
{
120122
$ordinal = $this->ordinal !== null ? $this->ordinal : $this->getOrdinal();
121123
return self::$names[static::class][$ordinal];
@@ -126,7 +128,7 @@ final public function getName()
126128
*
127129
* @return int
128130
*/
129-
final public function getOrdinal()
131+
final public function getOrdinal(): int
130132
{
131133
if ($this->ordinal === null) {
132134
$ordinal = 0;
@@ -147,10 +149,10 @@ final public function getOrdinal()
147149
/**
148150
* Compare this enumerator against another and check if it's the same.
149151
*
150-
* @param mixed $enumerator
152+
* @param static|null|bool|int|float|string|array $enumerator An enumerator object or value
151153
* @return bool
152154
*/
153-
final public function is($enumerator)
155+
final public function is($enumerator): bool
154156
{
155157
return $this === $enumerator || $this->value === $enumerator
156158

@@ -164,12 +166,12 @@ final public function is($enumerator)
164166
/**
165167
* Get an enumerator instance of the given enumerator value or instance
166168
*
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
168170
* @return static
169171
* @throws InvalidArgumentException On an unknwon or invalid value
170172
* @throws LogicException On ambiguous constant values
171173
*/
172-
final public static function get($enumerator)
174+
final public static function get($enumerator): self
173175
{
174176
if ($enumerator instanceof static && \get_class($enumerator) === static::class) {
175177
return $enumerator;
@@ -181,12 +183,12 @@ final public static function get($enumerator)
181183
/**
182184
* Get an enumerator instance by the given value
183185
*
184-
* @param null|bool|int|float|string|array $value
186+
* @param null|bool|int|float|string|array $value Enumerator value
185187
* @return static
186188
* @throws InvalidArgumentException On an unknwon or invalid value
187189
* @throws LogicException On ambiguous constant values
188190
*/
189-
final public static function byValue($value)
191+
final public static function byValue($value): self
190192
{
191193
if (!isset(self::$constants[static::class])) {
192194
self::detectConstants(static::class);
@@ -218,9 +220,8 @@ final public static function byValue($value)
218220
* @throws InvalidArgumentException On an invalid or unknown name
219221
* @throws LogicException On ambiguous values
220222
*/
221-
final public static function byName($name)
223+
final public static function byName(string $name): self
222224
{
223-
$name = (string) $name;
224225
if (isset(self::$instances[static::class][$name])) {
225226
return self::$instances[static::class][$name];
226227
}
@@ -236,15 +237,13 @@ final public static function byName($name)
236237
/**
237238
* Get an enumeration instance by the given ordinal number
238239
*
239-
* @param int $ordinal The ordinal number or the enumerator
240+
* @param int $ordinal The ordinal number of the enumerator
240241
* @return static
241242
* @throws InvalidArgumentException On an invalid ordinal number
242243
* @throws LogicException On ambiguous values
243244
*/
244-
final public static function byOrdinal($ordinal)
245+
final public static function byOrdinal(int $ordinal): self
245246
{
246-
$ordinal = (int) $ordinal;
247-
248247
if (!isset(self::$names[static::class])) {
249248
self::detectConstants(static::class);
250249
}
@@ -269,7 +268,7 @@ final public static function byOrdinal($ordinal)
269268
*
270269
* @return static[]
271270
*/
272-
final public static function getEnumerators()
271+
final public static function getEnumerators(): array
273272
{
274273
if (!isset(self::$names[static::class])) {
275274
self::detectConstants(static::class);
@@ -282,7 +281,7 @@ final public static function getEnumerators()
282281
*
283282
* @return mixed[]
284283
*/
285-
final public static function getValues()
284+
final public static function getValues(): array
286285
{
287286
return \array_values(self::detectConstants(static::class));
288287
}
@@ -292,7 +291,7 @@ final public static function getValues()
292291
*
293292
* @return string[]
294293
*/
295-
final public static function getNames()
294+
final public static function getNames(): array
296295
{
297296
if (!isset(self::$names[static::class])) {
298297
self::detectConstants(static::class);
@@ -305,7 +304,7 @@ final public static function getNames()
305304
*
306305
* @return int[]
307306
*/
308-
final public static function getOrdinals()
307+
final public static function getOrdinals(): array
309308
{
310309
$count = \count(self::detectConstants(static::class));
311310
return $count === 0 ? [] : \range(0, $count - 1);
@@ -317,7 +316,7 @@ final public static function getOrdinals()
317316
* @return array
318317
* @throws LogicException On ambiguous constant values
319318
*/
320-
final public static function getConstants()
319+
final public static function getConstants(): array
321320
{
322321
return self::detectConstants(static::class);
323322
}
@@ -328,7 +327,7 @@ final public static function getConstants()
328327
* @param static|null|bool|int|float|string|array $enumerator
329328
* @return bool
330329
*/
331-
final public static function has($enumerator)
330+
final public static function has($enumerator): bool
332331
{
333332
if ($enumerator instanceof static && \get_class($enumerator) === static::class) {
334333
return true;
@@ -343,7 +342,7 @@ final public static function has($enumerator)
343342
* @param null|bool|int|float|string|array $value
344343
* @return bool
345344
*/
346-
final public static function hasValue($value)
345+
final public static function hasValue($value): bool
347346
{
348347
$constants = self::detectConstants(static::class);
349348
return \in_array($value, $constants, true);
@@ -355,9 +354,9 @@ final public static function hasValue($value)
355354
* @param string $name
356355
* @return bool
357356
*/
358-
final public static function hasName($name)
357+
final public static function hasName(string $name): bool
359358
{
360-
return \is_string($name) && \defined("static::$name");
359+
return \defined("static::$name");
361360
}
362361

363362
/**
@@ -366,7 +365,7 @@ final public static function hasName($name)
366365
* @param string $class
367366
* @return array
368367
*/
369-
private static function detectConstants($class)
368+
private static function detectConstants(string $class): array
370369
{
371370
if (!isset(self::$constants[$class])) {
372371
$reflection = new ReflectionClass($class);
@@ -409,7 +408,7 @@ private static function detectConstants($class)
409408
* @param array $constants
410409
* @return bool
411410
*/
412-
private static function noAmbiguousValues(array $constants)
411+
private static function noAmbiguousValues(array $constants): bool
413412
{
414413
foreach ($constants as $value) {
415414
$names = \array_keys($constants, $value, true);
@@ -433,7 +432,7 @@ private static function noAmbiguousValues(array $constants)
433432
* @throws InvalidArgumentException On an invalid or unknown name
434433
* @throws LogicException On ambiguous constant values
435434
*/
436-
final public static function __callStatic($method, array $args)
435+
final public static function __callStatic(string $method, array $args): self
437436
{
438437
return self::byName($method);
439438
}

src/EnumMap.php

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MabeEnum;
46

57
use ArrayAccess;
@@ -47,7 +49,7 @@ class EnumMap implements ArrayAccess, Countable, SeekableIterator
4749
* @param string $enumeration The classname of the enumeration type
4850
* @throws InvalidArgumentException
4951
*/
50-
public function __construct($enumeration)
52+
public function __construct(string $enumeration)
5153
{
5254
if (!\is_subclass_of($enumeration, Enum::class)) {
5355
throw new InvalidArgumentException(\sprintf(
@@ -62,7 +64,7 @@ public function __construct($enumeration)
6264
* Get the classname of the enumeration
6365
* @return string
6466
*/
65-
public function getEnumeration()
67+
public function getEnumeration(): string
6668
{
6769
return $this->enumeration;
6870
}
@@ -71,7 +73,7 @@ public function getEnumeration()
7173
* Get a list of map keys
7274
* @return Enum[]
7375
*/
74-
public function getKeys()
76+
public function getKeys(): array
7577
{
7678
return \array_map([$this->enumeration, 'byOrdinal'], $this->ordinals);
7779
}
@@ -80,7 +82,7 @@ public function getKeys()
8082
* Get a list of map values
8183
* @return mixed[]
8284
*/
83-
public function getValues()
85+
public function getValues(): array
8486
{
8587
return \array_values($this->map);
8688
}
@@ -91,7 +93,7 @@ public function getValues()
9193
* @param bool $strict Use strict type comparison
9294
* @return Enum|null The found key or NULL
9395
*/
94-
public function search($value, $strict = false)
96+
public function search($value, bool $strict = false): ?Enum
9597
{
9698
$ord = \array_search($value, $this->map, $strict);
9799
if ($ord !== false) {
@@ -108,7 +110,7 @@ public function search($value, $strict = false)
108110
* @return bool
109111
* @see offsetExists
110112
*/
111-
public function contains($enumerator)
113+
public function contains($enumerator): bool
112114
{
113115
try {
114116
$enumeration = $this->enumeration;
@@ -126,7 +128,7 @@ public function contains($enumerator)
126128
* @return bool
127129
* @see contains
128130
*/
129-
public function offsetExists($enumerator)
131+
public function offsetExists($enumerator): bool
130132
{
131133
try {
132134
$enumeration = $this->enumeration;
@@ -168,7 +170,7 @@ public function offsetGet($enumerator)
168170
* @throws InvalidArgumentException On an invalid given enumerator
169171
* @see attach()
170172
*/
171-
public function offsetSet($enumerator, $value = null)
173+
public function offsetSet($enumerator, $value = null): void
172174
{
173175
$enumeration = $this->enumeration;
174176
$ord = $enumeration::get($enumerator)->getOrdinal();
@@ -186,7 +188,7 @@ public function offsetSet($enumerator, $value = null)
186188
* @throws InvalidArgumentException On an invalid given enumerator
187189
* @see detach()
188190
*/
189-
public function offsetUnset($enumerator)
191+
public function offsetUnset($enumerator): void
190192
{
191193
$enumeration = $this->enumeration;
192194
$ord = $enumeration::get($enumerator)->getOrdinal();
@@ -202,7 +204,7 @@ public function offsetUnset($enumerator)
202204
* @param int $pos
203205
* @throws OutOfBoundsException On an invalid position
204206
*/
205-
public function seek($pos)
207+
public function seek($pos): void
206208
{
207209
$pos = (int)$pos;
208210
if (!isset($this->ordinals[$pos])) {
@@ -229,7 +231,7 @@ public function current()
229231
* Get the current key
230232
* @return Enum|null
231233
*/
232-
public function key()
234+
public function key(): ?Enum
233235
{
234236
if (!isset($this->ordinals[$this->pos])) {
235237
return null;
@@ -243,7 +245,7 @@ public function key()
243245
* Reset the iterator position to zero.
244246
* @return void
245247
*/
246-
public function rewind()
248+
public function rewind(): void
247249
{
248250
$this->pos = 0;
249251
}
@@ -252,7 +254,7 @@ public function rewind()
252254
* Increment the iterator position by one.
253255
* @return void
254256
*/
255-
public function next()
257+
public function next(): void
256258
{
257259
++$this->pos;
258260
}
@@ -261,7 +263,7 @@ public function next()
261263
* Test if the iterator is in a valid state
262264
* @return bool
263265
*/
264-
public function valid()
266+
public function valid(): bool
265267
{
266268
return isset($this->ordinals[$this->pos]);
267269
}
@@ -271,7 +273,7 @@ public function valid()
271273
*
272274
* @return int
273275
*/
274-
public function count()
276+
public function count(): int
275277
{
276278
return \count($this->ordinals);
277279
}

src/EnumSerializableTrait.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
<?php
22

3+
declare(strict_types=1);
4+
35
namespace MabeEnum;
46

57
use RuntimeException;
@@ -31,7 +33,7 @@ abstract public function getValue();
3133
* This will be called automatically on `serialize()` if the enumeration implements the `Serializable` interface
3234
* @return string
3335
*/
34-
public function serialize()
36+
public function serialize(): string
3537
{
3638
return \serialize($this->getValue());
3739
}

0 commit comments

Comments
 (0)