Skip to content

Commit fc15efe

Browse files
committed
EnumMap::seek() throws TypeError in case of wrong argument type
1 parent 06d3c61 commit fc15efe

File tree

5 files changed

+32
-12
lines changed

5 files changed

+32
-12
lines changed

src/Enum.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,7 @@ private static function detectConstants(string $class): array
365365
{
366366
if (!isset(self::$constants[$class])) {
367367
$reflection = new ReflectionClass($class);
368-
$allConstants = [];
368+
$constants = [];
369369

370370
do {
371371
$scopeConstants = [];
@@ -376,16 +376,16 @@ private static function detectConstants(string $class): array
376376
}
377377
}
378378

379-
$allConstants = $scopeConstants + $allConstants;
379+
$constants = $scopeConstants + $constants;
380380
} while (($reflection = $reflection->getParentClass()) && $reflection->name !== __CLASS__);
381381

382382
assert(
383-
self::noAmbiguousValues($allConstants),
383+
self::noAmbiguousValues($constants),
384384
"Ambiguous enumerator values detected for {$class}"
385385
);
386386

387-
self::$constants[$class] = $allConstants;
388-
self::$names[$class] = \array_keys($allConstants);
387+
self::$constants[$class] = $constants;
388+
self::$names[$class] = \array_keys($constants);
389389
}
390390

391391
return self::$constants[$class];

src/EnumMap.php

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use InvalidArgumentException;
1010
use OutOfBoundsException;
1111
use SeekableIterator;
12+
use TypeError;
1213
use UnexpectedValueException;
1314

1415
/**
@@ -53,7 +54,8 @@ public function __construct(string $enumeration)
5354
{
5455
if (!\is_subclass_of($enumeration, Enum::class)) {
5556
throw new InvalidArgumentException(\sprintf(
56-
"This EnumMap can handle subclasses of '%s' only",
57+
'%s can handle subclasses of %s only',
58+
__CLASS__,
5759
Enum::class
5860
));
5961
}
@@ -199,7 +201,14 @@ public function offsetUnset($enumerator): void
199201
*/
200202
public function seek($pos): void
201203
{
202-
$pos = (int)$pos;
204+
if (!is_int($pos)) {
205+
throw new TypeError(\sprintf(
206+
'Argument 1 passed to %s() must be of the type int, %s given',
207+
__METHOD__,
208+
gettype($pos)
209+
));
210+
}
211+
203212
if (!isset($this->ordinals[$pos])) {
204213
throw new OutOfBoundsException("Position {$pos} not found");
205214
}

src/EnumSerializableTrait.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,6 @@ public function unserialize($serialized)
7070
self::$instances[$class][$name] = $enumerator;
7171
}
7272
};
73-
$closure = $closure->bindTo($this, Enum::class);
74-
$closure();
73+
$closure->bindTo($this, Enum::class)();
7574
}
7675
}

src/EnumSet.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ public function __construct(string $enumeration)
6969
{
7070
if (!\is_subclass_of($enumeration, Enum::class)) {
7171
throw new InvalidArgumentException(\sprintf(
72-
"%s can handle subclasses of '%s' only",
73-
static::class,
72+
'%s can handle subclasses of %s only',
73+
__METHOD__,
7474
Enum::class
7575
));
7676
}
@@ -301,7 +301,7 @@ private function doCountInt(): int
301301

302302
// PHP does not support right shift unsigned
303303
if ($bitset < 0) {
304-
$count = 1;
304+
$count = 1;
305305
$bitset = $bitset & \PHP_INT_MAX;
306306
}
307307

tests/MabeEnumTest/EnumMapTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -319,6 +319,18 @@ public function testSeek()
319319
$enumMap->seek(2);
320320
}
321321

322+
public function testSeekThrowsTypeError()
323+
{
324+
$enumMap = new EnumMap(EnumBasic::class);
325+
326+
$this->expectException(\TypeError::class);
327+
$this->expectExceptionMessage(
328+
'Argument 1 passed to ' . EnumMap::class . '::seek() must be of the type int, string given'
329+
);
330+
331+
$enumMap->seek('0');
332+
}
333+
322334
public function testSerializable()
323335
{
324336
$enumMap = new EnumMap(EnumBasic::class);

0 commit comments

Comments
 (0)