Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Narrow some more string types to non-empty-string #316

Merged
merged 2 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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: 2 additions & 0 deletions src/Storage/ClearByNamespaceInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface ClearByNamespaceInterface
{
/**
* Remove items of given namespace
*
* @param non-empty-string $namespace
*/
public function clearByNamespace(string $namespace): bool;
}
2 changes: 2 additions & 0 deletions src/Storage/ClearByPrefixInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ interface ClearByPrefixInterface
{
/**
* Remove items matching given prefix
*
* @param non-empty-string $prefix
*/
public function clearByPrefix(string $prefix): bool;
}
3 changes: 2 additions & 1 deletion src/Storage/MetadataCapableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ interface MetadataCapableInterface
/**
* Get metadata of an item.
*
* @param non-empty-string $key
* @return TMetadata|null Metadata on success, null on failure or in case metadata is not accessible.
* @throws ExceptionInterface
*/
Expand All @@ -23,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
2 changes: 2 additions & 0 deletions src/Storage/TaggableInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ interface TaggableInterface
* Set tags to an item by given key.
* An empty array will remove all tags.
*
* @param non-empty-string $key
* @param string[] $tags
*/
public function setTags(string $key, array $tags): bool;

/**
* Get tags of an item by given key
*
* @param non-empty-string $key
* @return string[]|false
*/
public function getTags(string $key): false|array;
Expand Down