diff --git a/CHANGELOG.md b/CHANGELOG.md index 552b6cf..318c905 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ - Enh #62: Remove not need casting to array in private method `Memcached::iterableToArray()` (@vjik) - Enh #62: Improve list of memcached server validation (@vjik) +- Enh #62: Check key type when multiple keys passed (@vjik) ## 2.0.0 February 15, 2023 diff --git a/src/Memcached.php b/src/Memcached.php index 8e0351d..3dbdc74 100644 --- a/src/Memcached.php +++ b/src/Memcached.php @@ -269,9 +269,20 @@ private function validateKey(string $key): void } } + /** + * @psalm-assert string[] $keys + */ private function validateKeys(array $keys): void { foreach ($keys as $key) { + if (!is_string($key)) { + throw new InvalidArgumentException( + sprintf( + 'Invalid key type. Expected string, got %s.', + get_debug_type($key) + ) + ); + } $this->validateKey($key); } } diff --git a/tests/MemcachedTest.php b/tests/MemcachedTest.php index b836659..601f0fd 100644 --- a/tests/MemcachedTest.php +++ b/tests/MemcachedTest.php @@ -219,6 +219,15 @@ public function testGetMultiple(): void $this->assertSameExceptObject($data, $cache->getMultiple(array_map('\strval', array_keys($data)))); } + public function testGetMultipleWithNotStringKey() + { + $cache = $this->createCacheInstance(); + + $this->expectException(InvalidArgumentException::class); + $this->expectExceptionMessage('Invalid key type. Expected string, got int.'); + $cache->getMultiple(['test', 1]); + } + public function testDeleteMultiple(): void { $cache = $this->createCacheInstance();