Skip to content

Commit 25e2f73

Browse files
committed
qa: enforce non-empty-string as array key for MetadataCapableInterface#getMetadatas return type
Signed-off-by: Maximilian Bösing <[email protected]>
1 parent 00b58ad commit 25e2f73

File tree

3 files changed

+16
-18
lines changed

3 files changed

+16
-18
lines changed

src/Storage/AbstractMetadataCapableAdapter.php

+14-16
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Laminas\Cache\Storage\Adapter\AdapterOptions;
1515
use Webmozart\Assert\Assert;
1616

17+
use function array_keys;
1718
use function is_array;
1819
use function is_object;
1920

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

105107
$result = $this->triggerPost(__FUNCTION__, $args, $result);
106-
Assert::isMap($result);
107-
Assert::allObject($result);
108+
$this->assertValidMetadataResult($result);
108109

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

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

151139
return $result;
152140
}
141+
142+
/**
143+
* @psalm-assert array<non-empty-string,TMetadata> $result
144+
*/
145+
private function assertValidMetadataResult(mixed $result): void
146+
{
147+
Assert::isMap($result);
148+
$this->assertValidKeys(array_keys($result));
149+
Assert::allObject($result);
150+
}
153151
}

src/Storage/Adapter/AbstractAdapter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1273,7 +1273,7 @@ protected function assertValidKeyValuePairs(mixed $keyValuePairs): void
12731273
/**
12741274
* @psalm-assert list<non-empty-string|int> $keys
12751275
*/
1276-
private function assertValidKeys(array $keys): void
1276+
protected function assertValidKeys(array $keys): void
12771277
{
12781278
foreach ($keys as $key) {
12791279
$this->assertValidKey($key);

src/Storage/MetadataCapableInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ public function getMetadata(string $key): ?object;
2424
* Get multiple metadata
2525
*
2626
* @param non-empty-list<non-empty-string> $keys
27-
* @return array<string,TMetadata> Associative array of keys and metadata
27+
* @return array<non-empty-string|int,TMetadata> Associative array of keys and metadata
2828
* @throws ExceptionInterface
2929
*/
3030
public function getMetadatas(array $keys): array;

0 commit comments

Comments
 (0)