Skip to content

Commit

Permalink
Check key type when multiple keys passed
Browse files Browse the repository at this point in the history
  • Loading branch information
vjik committed Feb 17, 2024
1 parent 1dc0075 commit cfe435b
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 11 additions & 0 deletions src/Memcached.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
9 changes: 9 additions & 0 deletions tests/MemcachedTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit cfe435b

Please sign in to comment.