Skip to content

Commit

Permalink
Merge pull request #317 from boesing/qa/narrow-key-types
Browse files Browse the repository at this point in the history
Allow integer keys for some more methods
  • Loading branch information
boesing authored Jun 15, 2024
2 parents 81501d4 + 09fee32 commit 512dc92
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 101 deletions.
2 changes: 0 additions & 2 deletions psalm-baseline.xml
Original file line number Diff line number Diff line change
Expand Up @@ -213,8 +213,6 @@
</PossiblyInvalidPropertyAssignmentValue>
<PossiblyUnusedMethod>
<code><![CDATA[getCaching]]></code>
<code><![CDATA[normalizeKey]]></code>
<code><![CDATA[normalizeKeyValuePairs]]></code>
<code><![CDATA[setCaching]]></code>
</PossiblyUnusedMethod>
<ReferenceConstraintViolation>
Expand Down
114 changes: 15 additions & 99 deletions src/Storage/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -593,10 +593,6 @@ abstract protected function internalSetItem(string $normalizedKey, mixed $value)

/**
* {@inheritDoc}
*
* @triggers setItems.pre(PreEvent)
* @triggers setItems.post(PostEvent)
* @triggers setItems.exception(ExceptionEvent)
*/
public function setItems(array $keyValuePairs): array
{
Expand Down Expand Up @@ -630,15 +626,15 @@ public function setItems(array $keyValuePairs): array
/**
* Internal method to store multiple items.
*
* @param non-empty-array<non-empty-string,mixed> $normalizedKeyValuePairs
* @return list<non-empty-string> Array of not stored keys
* @param non-empty-array<non-empty-string|int,mixed> $normalizedKeyValuePairs
* @return list<non-empty-string|int> Array of not stored keys
* @throws Exception\ExceptionInterface
*/
protected function internalSetItems(array $normalizedKeyValuePairs): array
{
$failedKeys = [];
foreach ($normalizedKeyValuePairs as $normalizedKey => $value) {
if (! $this->internalSetItem($normalizedKey, $value)) {
if (! $this->internalSetItem((string) $normalizedKey, $value)) {
$failedKeys[] = $normalizedKey;
}
}
Expand All @@ -647,12 +643,7 @@ protected function internalSetItems(array $normalizedKeyValuePairs): array
}

/**
* Add an item.
*
* @throws Exception\ExceptionInterface
* @triggers addItem.pre(PreEvent)
* @triggers addItem.post(PostEvent)
* @triggers addItem.exception(ExceptionEvent)
* {@inheritDoc}
*/
public function addItem(string $key, mixed $value): bool
{
Expand Down Expand Up @@ -702,10 +693,6 @@ protected function internalAddItem(string $normalizedKey, mixed $value): bool

/**
* {@inheritDoc}
*
* @triggers addItems.pre(PreEvent)
* @triggers addItems.post(PostEvent)
* @triggers addItems.exception(ExceptionEvent)
*/
public function addItems(array $keyValuePairs): array
{
Expand Down Expand Up @@ -740,15 +727,15 @@ public function addItems(array $keyValuePairs): array
/**
* Internal method to add multiple items.
*
* @param non-empty-array<non-empty-string,mixed> $normalizedKeyValuePairs
* @return list<non-empty-string> Array of not stored keys
* @param non-empty-array<non-empty-string|int,mixed> $normalizedKeyValuePairs
* @return list<non-empty-string|int> Array of not stored keys
* @throws Exception\ExceptionInterface
*/
protected function internalAddItems(array $normalizedKeyValuePairs): array
{
$result = [];
foreach ($normalizedKeyValuePairs as $normalizedKey => $value) {
if (! $this->internalAddItem($normalizedKey, $value)) {
if (! $this->internalAddItem((string) $normalizedKey, $value)) {
$result[] = $normalizedKey;
}
}
Expand All @@ -757,12 +744,7 @@ protected function internalAddItems(array $normalizedKeyValuePairs): array
}

/**
* Replace an existing item.
*
* @throws Exception\ExceptionInterface
* @triggers replaceItem.pre(PreEvent)
* @triggers replaceItem.post(PostEvent)
* @triggers replaceItem.exception(ExceptionEvent)
* {@inheritDoc}
*/
public function replaceItem(string $key, mixed $value): bool
{
Expand Down Expand Up @@ -812,10 +794,6 @@ protected function internalReplaceItem(string $normalizedKey, mixed $value): boo

/**
* {@inheritDoc}
*
* @triggers replaceItems.pre(PreEvent)
* @triggers replaceItems.post(PostEvent)
* @triggers replaceItems.exception(ExceptionEvent)
*/
public function replaceItems(array $keyValuePairs): array
{
Expand Down Expand Up @@ -847,15 +825,15 @@ public function replaceItems(array $keyValuePairs): array
/**
* Internal method to replace multiple existing items.
*
* @param non-empty-array<non-empty-string,mixed> $normalizedKeyValuePairs
* @return list<non-empty-string> Array of not stored keys
* @param non-empty-array<non-empty-string|int,mixed> $normalizedKeyValuePairs
* @return list<non-empty-string|int> Array of not stored keys
* @throws Exception\ExceptionInterface
*/
protected function internalReplaceItems(array $normalizedKeyValuePairs): array
{
$result = [];
foreach ($normalizedKeyValuePairs as $normalizedKey => $value) {
if (! $this->internalReplaceItem($normalizedKey, $value)) {
if (! $this->internalReplaceItem((string) $normalizedKey, $value)) {
$result[] = $normalizedKey;
}
}
Expand All @@ -864,15 +842,7 @@ protected function internalReplaceItems(array $normalizedKeyValuePairs): array
}

/**
* Set an item only if token matches
*
* It uses the token received from getItem() to check if the item has
* changed before overwriting it.
*
* @see setItem()
* @see getItem()
*
* @throws Exception\ExceptionInterface
* {@inheritDoc}
*/
public function checkAndSetItem(mixed $token, string $key, mixed $value): bool
{
Expand Down Expand Up @@ -906,9 +876,6 @@ public function checkAndSetItem(mixed $token, string $key, mixed $value): bool
/**
* Internal method to set an item only if token matches
*
* @see getItem()
* @see setItem()
*
* @param non-empty-string $normalizedKey
* @throws Exception\ExceptionInterface
*/
Expand All @@ -923,12 +890,7 @@ protected function internalCheckAndSetItem(mixed $token, string $normalizedKey,
}

/**
* Reset lifetime of an item
*
* @throws Exception\ExceptionInterface
* @triggers touchItem.pre(PreEvent)
* @triggers touchItem.post(PostEvent)
* @triggers touchItem.exception(ExceptionEvent)
* {@inheritDoc}
*/
public function touchItem(string $key): bool
{
Expand Down Expand Up @@ -976,10 +938,6 @@ protected function internalTouchItem(string $normalizedKey): bool

/**
* {@inheritDoc}
*
* @triggers touchItems.pre(PreEvent)
* @triggers touchItems.post(PostEvent)
* @triggers touchItems.exception(ExceptionEvent)
*/
public function touchItems(array $keys): array
{
Expand Down Expand Up @@ -1029,12 +987,7 @@ protected function internalTouchItems(array $normalizedKeys): array
}

/**
* Remove an item.
*
* @throws Exception\ExceptionInterface
* @triggers removeItem.pre(PreEvent)
* @triggers removeItem.post(PostEvent)
* @triggers removeItem.exception(ExceptionEvent)
* {@inheritDoc}
*/
public function removeItem(string $key): bool
{
Expand Down Expand Up @@ -1076,10 +1029,6 @@ abstract protected function internalRemoveItem(string $normalizedKey): bool;

/**
* {@inheritDoc}
*
* @triggers removeItems.pre(PreEvent)
* @triggers removeItems.post(PostEvent)
* @triggers removeItems.exception(ExceptionEvent)
*/
public function removeItems(array $keys): array
{
Expand Down Expand Up @@ -1135,11 +1084,7 @@ protected function internalRemoveItems(array $normalizedKeys): array
/* status */

/**
* Get capabilities of this adapter
*
* @triggers getCapabilities.pre(PreEvent)
* @triggers getCapabilities.post(PostEvent)
* @triggers getCapabilities.exception(ExceptionEvent)
* {@inheritDoc}
*/
public function getCapabilities(): Capabilities
{
Expand Down Expand Up @@ -1172,21 +1117,6 @@ protected function internalGetCapabilities(): Capabilities
return $this->capabilities ??= new Capabilities();
}

/* internal */

/**
* Validates and normalizes a key
*
* @deprecated Use {@see AbstractAdapter::assertValidKey()} instead.
*
* @throws Exception\InvalidArgumentException On an invalid key.
* @psalm-assert non-empty-string $key
*/
protected function normalizeKey(string $key): void
{
$this->assertValidKey($key);
}

/**
* Validates and normalizes multiple keys
*
Expand All @@ -1203,20 +1133,6 @@ protected function normalizeKeys(array $keys): array
return array_values(array_unique($keys));
}

/**
* Validates and normalizes an array of key-value pairs
*
* @deprecated Please use {@see AbstractAdapter::assertValidKeyValuePairs()} instead.
*
* @param array<string,mixed> $keyValuePairs
* @psalm-assert array<non-empty-string,mixed> $keyValuePairs
* @throws Exception\InvalidArgumentException On an invalid key.
*/
protected function normalizeKeyValuePairs(array $keyValuePairs): void
{
$this->assertValidKeyValuePairs($keyValuePairs);
}

/**
* @template TKey
* @param TKey $key
Expand Down

0 comments on commit 512dc92

Please sign in to comment.