Skip to content

Commit 81501d4

Browse files
authored
Merge pull request #316 from boesing/qa/narrow-key-types
Narrow some more string types to `non-empty-string`
2 parents bf83c2a + 25e2f73 commit 81501d4

6 files changed

+23
-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/ClearByNamespaceInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ interface ClearByNamespaceInterface
66
{
77
/**
88
* Remove items of given namespace
9+
*
10+
* @param non-empty-string $namespace
911
*/
1012
public function clearByNamespace(string $namespace): bool;
1113
}

src/Storage/ClearByPrefixInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ interface ClearByPrefixInterface
66
{
77
/**
88
* Remove items matching given prefix
9+
*
10+
* @param non-empty-string $prefix
911
*/
1012
public function clearByPrefix(string $prefix): bool;
1113
}

src/Storage/MetadataCapableInterface.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ interface MetadataCapableInterface
1414
/**
1515
* Get metadata of an item.
1616
*
17+
* @param non-empty-string $key
1718
* @return TMetadata|null Metadata on success, null on failure or in case metadata is not accessible.
1819
* @throws ExceptionInterface
1920
*/
@@ -23,7 +24,7 @@ public function getMetadata(string $key): ?object;
2324
* Get multiple metadata
2425
*
2526
* @param non-empty-list<non-empty-string> $keys
26-
* @return array<string,TMetadata> Associative array of keys and metadata
27+
* @return array<non-empty-string|int,TMetadata> Associative array of keys and metadata
2728
* @throws ExceptionInterface
2829
*/
2930
public function getMetadatas(array $keys): array;

src/Storage/TaggableInterface.php

+2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,15 @@ interface TaggableInterface
88
* Set tags to an item by given key.
99
* An empty array will remove all tags.
1010
*
11+
* @param non-empty-string $key
1112
* @param string[] $tags
1213
*/
1314
public function setTags(string $key, array $tags): bool;
1415

1516
/**
1617
* Get tags of an item by given key
1718
*
19+
* @param non-empty-string $key
1820
* @return string[]|false
1921
*/
2022
public function getTags(string $key): false|array;

0 commit comments

Comments
 (0)