Skip to content

Commit

Permalink
Raise psr/simple-cache: ^2.0|^3.0, set psalm to 1. (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
terabytesoftw authored Aug 12, 2022
1 parent 28d8559 commit 63636cc
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 99 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/mssql.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
name: PHP ${{ matrix.php }}-mssql-${{ matrix.mssql }}

env:
extensions: pdo, pdo_sqlsrv, pdo_sqlsrv-5.10.0-beta1
extensions: pdo, pdo_sqlsrv-5.10.1
key: cache

runs-on: ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/mutation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ jobs:
name: PHP ${{ matrix.php }}-${{ matrix.os }}

env:
extensions: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlsrv, pdo_sqlsrv-5.10.0-beta1, oci8
extensions: pdo, pdo_mysql, pdo_oci, pdo_pgsql, pdo_sqlsrv-5.10.1, oci8

runs-on: ${{ matrix.os }}

Expand Down
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"php": "^8.0",
"ext-pdo": "*",
"psr/log": "^2.0|^3.0",
"psr/simple-cache": "^1.0.1",
"psr/simple-cache": "^2.0|^3.0",
"yiisoft/db": "^3.0@dev",
"yiisoft/yii-db-migration": "^1.0@dev"
},
Expand Down
2 changes: 1 addition & 1 deletion psalm.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0"?>
<psalm
errorLevel="2"
errorLevel="1"
resolveFromConfigFile="true"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="https://getpsalm.org/schema/config"
Expand Down
57 changes: 28 additions & 29 deletions src/DbCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@
use Yiisoft\Db\Query\Query;

use function array_fill_keys;
use function gettype;
use function is_iterable;
use function is_string;
use function iterator_to_array;
use function random_int;
Expand Down Expand Up @@ -92,20 +90,22 @@ public function getTable(): string
return $this->table;
}

public function get($key, $default = null): mixed
public function get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);

/** @var bool|float|int|string|null */
$value = $this->getData($key, ['data'], 'scalar');

return $value === false ? $default : unserialize($value);
return $value === false ? $default : unserialize((string) $value);
}

/**
* @param string $key The cache data ID.
* @param mixed $value The cache data value.
* @param DateInterval|int|string|null $ttl The cache data TTL.
*/
public function set($key, $value, $ttl = null): bool
public function set(string $key, mixed $value, DateInterval|int|string|null $ttl = null): bool
{
$this->validateKey($key);
$ttl = $this->normalizeTtl($ttl);
Expand All @@ -131,7 +131,7 @@ public function set($key, $value, $ttl = null): bool
}
}

public function delete($key): bool
public function delete(string $key): bool
{
$this->validateKey($key);
$this->deleteData($key);
Expand All @@ -146,32 +146,38 @@ public function clear(): bool
return true;
}

public function getMultiple($keys, $default = null): iterable
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
/** @psalm-var array<array-key,array-key> */
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
$values = array_fill_keys($keys, $default);

/** @psalm-var array<string, string|resource> */
foreach ($this->getData($keys, ['id', 'data'], 'all') as $value) {
if (is_resource($value['data']) && get_resource_type($value['data']) === 'stream') {
$value['data'] = stream_get_contents($value['data']);
}
$values[$value['id']] = unserialize($value['data']);

/** @var string */
$values[$value['id']] = unserialize((string) $value['data']);
}

/** @psalm-var iterable<string,mixed> */
return $values;
}

/**
* @param iterable $values A list of key => value pairs for a multiple-set operation.
* @param DateInterval|int|string|null $ttl The cache data TTL.
*/
public function setMultiple($values, $ttl = null): bool
public function setMultiple(iterable $values, DateInterval|int|string|null $ttl = null): bool
{
$ttl = $this->normalizeTtl($ttl);
$values = $this->iterableToArray($values);
$rows = $keys = [];

/** @var mixed $value */
foreach ($values as $key => $value) {
$key = (string) $key;
$this->validateKey($key);
Expand Down Expand Up @@ -200,7 +206,7 @@ public function setMultiple($values, $ttl = null): bool
}
}

public function deleteMultiple($keys): bool
public function deleteMultiple(iterable $keys): bool
{
$keys = $this->iterableToArray($keys);
$this->validateKeys($keys);
Expand All @@ -209,11 +215,11 @@ public function deleteMultiple($keys): bool
return true;
}

public function has($key): bool
public function has(string $key): bool
{
$this->validateKey($key);

return $this->getData($key, ['id'], 'exists');
return (bool) $this->getData($key, ['id'], 'exists');
}

/**
Expand Down Expand Up @@ -245,7 +251,7 @@ public function setLoggerMessageUpdate(string $value): void
*
* @return mixed The cache data.
*/
private function getData($id, array $fields, string $method)
private function getData(array|string $id, array $fields, string $method): mixed
{
if (empty($id)) {
return is_string($id) ? false : [];
Expand All @@ -263,11 +269,11 @@ private function getData($id, array $fields, string $method)
/**
* Deletes a cache data from the database.
*
* @param array|string|true $id One or more IDs for deleting data.
* @param array|bool|string $id One or more IDs for deleting data.
*
* If `true`, the all cache data will be deleted from the database.
*/
private function deleteData($id): void
private function deleteData(array|string|bool $id): void
{
if (empty($id)) {
return;
Expand Down Expand Up @@ -295,9 +301,9 @@ private function deleteData($id): void
*
* @return array The row of cache data to insert into the database.
*/
private function buildDataRow(string $id, ?int $ttl, $value, bool $associative): array
private function buildDataRow(string $id, ?int $ttl, mixed $value, bool $associative): array
{
$expire = $this->isInfinityTtl($ttl) ? null : ($ttl + time());
$expire = $this->isInfinityTtl($ttl) ? null : ((int) $ttl + time());
$data = new Param(serialize($value), PDO::PARAM_LOB);

if ($associative) {
Expand Down Expand Up @@ -327,9 +333,9 @@ private function gc(): void
*
* @param DateInterval|int|string|null $ttl The raw TTL.
*
* @return int TTL value as UNIX timestamp.
* @return int|null TTL value as UNIX timestamp.
*/
private function normalizeTtl($ttl): ?int
private function normalizeTtl(DateInterval|int|string|null $ttl): ?int
{
if ($ttl === null) {
return null;
Expand All @@ -356,25 +362,17 @@ private function isInfinityTtl(?int $ttl): bool

/**
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException.
*
* @param mixed $iterable
*
* @return array
*/
private function iterableToArray($iterable): array
private function iterableToArray(iterable $iterable): array
{
if (!is_iterable($iterable)) {
throw new InvalidArgumentException('Iterable is expected, got ' . gettype($iterable));
}

/** @psalm-suppress RedundantCast */
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
}

/**
* @param mixed $key
*/
private function validateKey($key): void
private function validateKey(mixed $key): void
{
if (!is_string($key) || $key === '' || strpbrk($key, '{}()/\@:')) {
throw new InvalidArgumentException('Invalid key value.');
Expand All @@ -386,6 +384,7 @@ private function validateKey($key): void
*/
private function validateKeys(array $keys): void
{
/** @var mixed $key */
foreach ($keys as $key) {
$this->validateKey($key);
}
Expand Down
Loading

0 comments on commit 63636cc

Please # to comment.