Skip to content

Commit

Permalink
qa: enforce non-empty-string as array key for `MetadataCapableInter…
Browse files Browse the repository at this point in the history
…face#getMetadatas` return type

Signed-off-by: Maximilian Bösing <2189546+boesing@users.noreply.github.com>
  • Loading branch information
boesing committed Jun 15, 2024
1 parent 00b58ad commit 25e2f73
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 18 deletions.
30 changes: 14 additions & 16 deletions src/Storage/AbstractMetadataCapableAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Laminas\Cache\Storage\Adapter\AdapterOptions;
use Webmozart\Assert\Assert;

use function array_keys;
use function is_array;
use function is_object;

Expand Down Expand Up @@ -74,6 +75,7 @@ public function getMetadata(string $key): ?object
/**
* Internal method to get metadata of an item.
*
* @param non-empty-string $normalizedKey
* @return TMetadata|null Metadata on success, null on failure or in case metadata is not accessible.
* @throws ExceptionInterface
*/
Expand Down Expand Up @@ -103,28 +105,14 @@ public function getMetadatas(array $keys): array
}

$result = $this->triggerPost(__FUNCTION__, $args, $result);
Assert::isMap($result);
Assert::allObject($result);
$this->assertValidMetadataResult($result);

/**
* NOTE: We do trust the event handling here and assume that it will return a map of instances of Metadata
* and thus does not modify the type.
*
* @var array<string,TMetadata> $result
*/
return $result;
} catch (Exception $exception) {
$result = [];
$result = $this->triggerThrowable(__FUNCTION__, $args, $result, $exception);
Assert::isArray($result);
Assert::allObject($result);
$this->assertValidMetadataResult($result);

/**
* NOTE: We do trust the event handling here and assume that it will return a map of instances of Metadata
* and thus does not modify the type.
*
* @var array<string,TMetadata> $result
*/
return $result;
}
}
Expand All @@ -150,4 +138,14 @@ protected function internalGetMetadatas(array $normalizedKeys): array

return $result;
}

/**
* @psalm-assert array<non-empty-string,TMetadata> $result
*/
private function assertValidMetadataResult(mixed $result): void
{
Assert::isMap($result);
$this->assertValidKeys(array_keys($result));
Assert::allObject($result);
}
}
2 changes: 1 addition & 1 deletion src/Storage/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -1273,7 +1273,7 @@ protected function assertValidKeyValuePairs(mixed $keyValuePairs): void
/**
* @psalm-assert list<non-empty-string|int> $keys
*/
private function assertValidKeys(array $keys): void
protected function assertValidKeys(array $keys): void
{
foreach ($keys as $key) {
$this->assertValidKey($key);
Expand Down
2 changes: 1 addition & 1 deletion src/Storage/MetadataCapableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function getMetadata(string $key): ?object;
* Get multiple metadata
*
* @param non-empty-list<non-empty-string> $keys
* @return array<string,TMetadata> Associative array of keys and metadata
* @return array<non-empty-string|int,TMetadata> Associative array of keys and metadata
* @throws ExceptionInterface
*/
public function getMetadatas(array $keys): array;
Expand Down

0 comments on commit 25e2f73

Please sign in to comment.