diff --git a/src/Olivebbs/Map/GenericMap.php b/src/Olivebbs/Map/GenericMap.php index ae06c04..a438344 100644 --- a/src/Olivebbs/Map/GenericMap.php +++ b/src/Olivebbs/Map/GenericMap.php @@ -31,13 +31,13 @@ use ArrayAccess; use Countable; use Ds\Map; use function is_array; +use function is_callable; use function is_int; use function is_object; use function is_string; -use function mb_strlen; -use function strtolower; - use Olivebbs\Map\Exception\InvalidArgumentException; + +use function strtolower; use TypeError; use ValueError; @@ -56,18 +56,11 @@ class GenericMap implements ArrayAccess, Countable protected string $keyType; protected string $valueType; - public function __construct(?string $keyType = null, ?string $valueType = null) + public function __construct(string $keyType = self::ANY, string $valueType = self::ANY) { + $this->keyType = strtolower($keyType); - - - $this->keyType = strtolower($keyType ?? self::ANY); - - $this->valueType = $valueType ?? self::ANY; - - if(!class_exists($this->valueType)) { - $this->valueType = strtolower($this->valueType); - } + $this->valueType = class_exists($valueType) ? $valueType : strtolower($valueType); if (!$this->isValidKeyType($this->keyType)) { throw new InvalidArgumentException(sprintf('Invalid key type (%s)', $this->keyType)); @@ -96,7 +89,7 @@ class GenericMap implements ArrayAccess, Countable public function offsetExists($offset): bool { if (is_array($offset) || is_object($offset)) { - throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); + throw new InvalidArgumentException('Map keys cannot be objects or arrays'); } if (!$this->checkType($this->keyType, $offset)) { @@ -112,7 +105,7 @@ class GenericMap implements ArrayAccess, Countable public function offsetGet($offset) { if (is_array($offset) || is_object($offset)) { - throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); + throw new InvalidArgumentException('Map keys cannot be objects or arrays'); } if (!$this->checkType($this->keyType, $offset)) { @@ -130,7 +123,7 @@ class GenericMap implements ArrayAccess, Countable public function offsetSet($offset, $value): void { if (is_array($offset) || is_object($offset)) { - throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); + throw new InvalidArgumentException('Map keys cannot be objects or arrays'); } if (!$this->checkType($this->keyType, $offset)) { @@ -150,7 +143,7 @@ class GenericMap implements ArrayAccess, Countable public function offsetUnset($offset): void { if (is_array($offset) || is_object($offset)) { - throw new InvalidArgumentException('Map Keys cannot be objects or arrays'); + throw new InvalidArgumentException('Map keys cannot be objects or arrays'); } if (!$this->checkType($this->keyType, $offset)) { @@ -202,6 +195,8 @@ class GenericMap implements ArrayAccess, Countable switch ($type) { case self::OBJECT: return is_object($var); + case self::CALLABLE: + return is_callable($var); case self::ARRAY: return is_array($var); case self::INT: @@ -210,7 +205,7 @@ class GenericMap implements ArrayAccess, Countable case self::STRING: return is_string($var); case self::CHAR: - return is_string($var) && mb_strlen($var) === 1; + return is_string($var) && \mb_strlen($var) === 1; case self::ANY: return true; default: @@ -230,7 +225,7 @@ class GenericMap implements ArrayAccess, Countable private function isValidValueType(string $type): bool { - if(class_exists($type)) { + if (class_exists($type)) { return true; } diff --git a/tests/GenericMapTest.php b/tests/GenericMapTest.php index 1e045fb..77379e6 100644 --- a/tests/GenericMapTest.php +++ b/tests/GenericMapTest.php @@ -156,7 +156,15 @@ class GenericMapTest extends TestCase $genericMap[0] = $splObject; self::assertSame($splObject, $genericMap[0]); self::assertTrue($genericMap[0]->contains($stdClass)); + } + public function testObjectCantBeUsedAsKeyWithAny(): void + { + $genericMap = new GenericMap(GenericMap::ANY, GenericMap::ANY); + $object = new \SplObjectStorage(); + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Map keys cannot be objects or arrays'); + $genericMap[$object] = 1; } private function resetGenericMap(): void