Skip to content

Commit 9387e9d

Browse files
authored
Raise psr/simple-cache to ^2.0|^3.0 and PHP to ^8.0 + PHP 8 improvements (#103)
1 parent c7b645f commit 9387e9d

22 files changed

Lines changed: 119 additions & 235 deletions

.github/workflows/build.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
os: >-
2929
['ubuntu-latest', 'windows-latest']
3030
php: >-
31-
['7.4', '8.0', '8.1']
31+
['8.0', '8.1']

.github/workflows/static.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@ jobs:
2828
os: >-
2929
['ubuntu-latest']
3030
php: >-
31-
['7.4', '8.0', '8.1']
31+
['8.0', '8.1']

CHANGELOG.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
1-
# Cache Change Log
1+
# Yii Cache Change Log
22

3-
## 1.0.2 under development
3+
## 2.0.0 under development
44

5-
- no changes in this release.
5+
- Chg #103: Raise the minimum `psr/simple-cache` version to `^2.0|^3.0` and the minimum PHP version to `^8.0` (@vjik)
66

77
## 1.0.1 March 23, 2021
88

9-
- Chg: Adjust config for new config plugin (samdark)
9+
- Chg: Adjust config for new config plugin (@samdark)
1010

1111
## 1.0.0 February 02, 2021
1212

composer.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,10 @@
2828
}
2929
],
3030
"require": {
31-
"php": "^7.4|^8.0",
31+
"php": "^8.0",
3232
"ext-json": "*",
3333
"ext-mbstring": "*",
34-
"psr/simple-cache": "^1.0.1"
34+
"psr/simple-cache": "^2.0|^3.0"
3535
},
3636
"require-dev": {
3737
"phpunit/phpunit": "^9.5",
@@ -40,7 +40,7 @@
4040
"vimeo/psalm": "^4.18"
4141
},
4242
"provide": {
43-
"psr/simple-cache-implementation": "1.0.0"
43+
"psr/simple-cache-implementation": "2.0|3.0"
4444
},
4545
"suggest": {
4646
"yiisoft/cache-apcu": "Allows to store cache using APCu PECL extension",

src/ArrayCache.php

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,6 @@
1111

1212
use function array_keys;
1313
use function array_map;
14-
use function gettype;
15-
use function is_iterable;
1614
use function is_object;
1715
use function is_string;
1816
use function iterator_to_array;
@@ -32,7 +30,7 @@ final class ArrayCache implements \Psr\SimpleCache\CacheInterface
3230
/** @psalm-var array<string, array{0: mixed, 1: int}> */
3331
private array $cache = [];
3432

35-
public function get($key, $default = null)
33+
public function get(string $key, mixed $default = null): mixed
3634
{
3735
if ($this->has($key)) {
3836
/** @var mixed */
@@ -48,7 +46,7 @@ public function get($key, $default = null)
4846
return $default;
4947
}
5048

51-
public function set($key, $value, $ttl = null): bool
49+
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
5250
{
5351
$this->validateKey($key);
5452
$expiration = $this->ttlToExpiration($ttl);
@@ -65,7 +63,7 @@ public function set($key, $value, $ttl = null): bool
6563
return true;
6664
}
6765

68-
public function delete($key): bool
66+
public function delete(string $key): bool
6967
{
7068
$this->validateKey($key);
7169
unset($this->cache[$key]);
@@ -78,9 +76,10 @@ public function clear(): bool
7876
return true;
7977
}
8078

81-
public function getMultiple($keys, $default = null): iterable
79+
public function getMultiple(iterable $keys, mixed $default = null): iterable
8280
{
8381
$keys = $this->iterableToArray($keys);
82+
/** @psalm-suppress RedundantCondition */
8483
$this->validateKeys($keys);
8584
$results = [];
8685

@@ -94,7 +93,7 @@ public function getMultiple($keys, $default = null): iterable
9493
return $results;
9594
}
9695

97-
public function setMultiple($values, $ttl = null): bool
96+
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
9897
{
9998
$values = $this->iterableToArray($values);
10099
$this->validateKeysOfValues($values);
@@ -107,9 +106,10 @@ public function setMultiple($values, $ttl = null): bool
107106
return true;
108107
}
109108

110-
public function deleteMultiple($keys): bool
109+
public function deleteMultiple(iterable $keys): bool
111110
{
112111
$keys = $this->iterableToArray($keys);
112+
/** @psalm-suppress RedundantCondition */
113113
$this->validateKeys($keys);
114114

115115
foreach ($keys as $key) {
@@ -119,7 +119,7 @@ public function deleteMultiple($keys): bool
119119
return true;
120120
}
121121

122-
public function has($key): bool
122+
public function has(string $key): bool
123123
{
124124
$this->validateKey($key);
125125
return !$this->isExpired($key);
@@ -138,13 +138,9 @@ private function isExpired(string $key): bool
138138
}
139139

140140
/**
141-
* Converts TTL to expiration
142-
*
143-
* @param DateInterval|int|null $ttl
144-
*
145-
* @return int
141+
* Converts TTL to expiration.
146142
*/
147-
private function ttlToExpiration($ttl): int
143+
private function ttlToExpiration(DateInterval|int|null $ttl): int
148144
{
149145
$ttl = $this->normalizeTtl($ttl);
150146

@@ -166,7 +162,7 @@ private function ttlToExpiration($ttl): int
166162
*
167163
* @return int|null TTL value as UNIX timestamp or null meaning infinity
168164
*/
169-
private function normalizeTtl($ttl): ?int
165+
private function normalizeTtl(DateInterval|int|string|null $ttl): ?int
170166
{
171167
if ($ttl instanceof DateInterval) {
172168
return (new DateTime('@0'))
@@ -182,34 +178,25 @@ private function normalizeTtl($ttl): ?int
182178
}
183179

184180
/**
185-
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException
181+
* Converts iterable to array.
186182
*
187-
* @param mixed $iterable
188-
*
189-
* @return array
183+
* @psalm-template T
184+
* @psalm-param iterable<T> $iterable
185+
* @psalm-return array<array-key,T>
190186
*/
191-
private function iterableToArray($iterable): array
187+
private function iterableToArray(iterable $iterable): array
192188
{
193-
if (!is_iterable($iterable)) {
194-
throw new InvalidArgumentException('Iterable is expected, got ' . gettype($iterable));
195-
}
196-
197-
/** @psalm-suppress RedundantCast */
198-
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
189+
return $iterable instanceof Traversable ? iterator_to_array($iterable) : $iterable;
199190
}
200191

201-
/**
202-
* @param mixed $key
203-
*/
204-
private function validateKey($key): void
192+
private function validateKey(mixed $key): void
205193
{
206194
if (!is_string($key) || $key === '' || strpbrk($key, '{}()/\@:')) {
207195
throw new InvalidArgumentException('Invalid key value.');
208196
}
209197
}
210198

211199
/**
212-
* @param array $keys
213200
* @psalm-assert string[] $keys
214201
*/
215202
private function validateKeys(array $keys): void

src/Cache.php

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@
1313
use Yiisoft\Cache\Metadata\CacheItem;
1414
use Yiisoft\Cache\Metadata\CacheItems;
1515

16-
use function gettype;
1716
use function is_array;
18-
use function is_int;
1917

2018
/**
2119
* Cache provides support for the data caching, including cache key composition and dependencies, and uses
@@ -54,7 +52,7 @@ final class Cache implements CacheInterface
5452
* null meaning infinity, negative or zero results in the cache key deletion.
5553
* This value is used by {@see getOrSet()}, if the duration is not explicitly given.
5654
*/
57-
public function __construct(\Psr\SimpleCache\CacheInterface $handler, $defaultTtl = null)
55+
public function __construct(\Psr\SimpleCache\CacheInterface $handler, DateInterval|int|null $defaultTtl = null)
5856
{
5957
$this->psr = new DependencyAwareCache($this, $handler);
6058
$this->items = new CacheItems();
@@ -67,16 +65,21 @@ public function psr(): \Psr\SimpleCache\CacheInterface
6765
return $this->psr;
6866
}
6967

70-
public function getOrSet($key, callable $callable, $ttl = null, Dependency $dependency = null, float $beta = 1.0)
71-
{
68+
public function getOrSet(
69+
mixed $key,
70+
callable $callable,
71+
DateInterval|int|null $ttl = null,
72+
Dependency $dependency = null,
73+
float $beta = 1.0
74+
) {
7275
$key = $this->keyNormalizer->normalize($key);
7376
/** @var mixed */
7477
$value = $this->getValue($key, $beta);
7578

7679
return $value ?? $this->setAndGet($key, $callable, $ttl, $dependency);
7780
}
7881

79-
public function remove($key): void
82+
public function remove(mixed $key): void
8083
{
8184
$key = $this->keyNormalizer->normalize($key);
8285

@@ -95,7 +98,7 @@ public function remove($key): void
9598
*
9699
* @return mixed|null The cache value or `null` if the cache is outdated or a dependency has been changed.
97100
*/
98-
private function getValue(string $key, float $beta)
101+
private function getValue(string $key, float $beta): mixed
99102
{
100103
if ($this->items->expired($key, $beta, $this)) {
101104
return null;
@@ -132,8 +135,12 @@ private function getValue(string $key, float $beta)
132135
*
133136
* @return mixed|null The cache value.
134137
*/
135-
private function setAndGet(string $key, callable $callable, $ttl, ?Dependency $dependency)
136-
{
138+
private function setAndGet(
139+
string $key,
140+
callable $callable,
141+
DateInterval|int|null $ttl,
142+
?Dependency $dependency
143+
): mixed {
137144
$ttl = $this->normalizeTtl($ttl);
138145
$ttl ??= $this->defaultTtl;
139146
/** @var mixed */
@@ -156,13 +163,13 @@ private function setAndGet(string $key, callable $callable, $ttl, ?Dependency $d
156163
/**
157164
* Normalizes cache TTL handling `null` value and {@see DateInterval} objects.
158165
*
159-
* @param mixed $ttl raw TTL.
166+
* @param DateInterval|int|null $ttl raw TTL.
160167
*
161168
* @throws InvalidArgumentException For invalid TTL.
162169
*
163170
* @return int|null TTL value as UNIX timestamp or null meaning infinity.
164171
*/
165-
private function normalizeTtl($ttl): ?int
172+
private function normalizeTtl(DateInterval|int|null $ttl): ?int
166173
{
167174
if ($ttl === null) {
168175
return null;
@@ -174,13 +181,6 @@ private function normalizeTtl($ttl): ?int
174181
->getTimestamp();
175182
}
176183

177-
if (is_int($ttl)) {
178-
return $ttl;
179-
}
180-
181-
throw new InvalidArgumentException(sprintf(
182-
'Invalid TTL "%s" specified. It must be a \DateInterval instance, an integer, or null.',
183-
gettype($ttl),
184-
));
184+
return $ttl;
185185
}
186186
}

src/CacheInterface.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,13 @@ public function psr(): \Psr\SimpleCache\CacheInterface;
5555
*
5656
* @return mixed Result of `$callable` execution.
5757
*/
58-
public function getOrSet($key, callable $callable, $ttl = null, Dependency $dependency = null, float $beta = 1.0);
58+
public function getOrSet(
59+
mixed $key,
60+
callable $callable,
61+
DateInterval|int|null $ttl = null,
62+
Dependency $dependency = null,
63+
float $beta = 1.0
64+
);
5965

6066
/**
6167
* Removes a value with the specified key from cache.
@@ -65,5 +71,5 @@ public function getOrSet($key, callable $callable, $ttl = null, Dependency $depe
6571
* @throws InvalidArgumentException MUST be thrown if the `$key` is not a legal value.
6672
* @throws RemoveCacheException Must be thrown if the data could not be removed from the cache.
6773
*/
68-
public function remove($key): void;
74+
public function remove(mixed $key): void;
6975
}

src/CacheKeyNormalizer.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ final class CacheKeyNormalizer
3434
*
3535
* @return string The normalized cache key.
3636
*/
37-
public function normalize($key): string
37+
public function normalize(mixed $key): string
3838
{
3939
if (is_string($key) || is_int($key)) {
4040
$key = (string) $key;

src/Dependency/AllDependencies.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
use Yiisoft\Cache\CacheInterface;
88
use Yiisoft\Cache\Exception\InvalidArgumentException;
99

10-
use function gettype;
11-
use function get_class;
12-
use function is_object;
1310
use function sprintf;
1411

1512
/**
@@ -37,7 +34,7 @@ public function __construct(array $dependencies = [])
3734
throw new InvalidArgumentException(sprintf(
3835
'The dependency must be a "%s" instance, "%s" received',
3936
Dependency::class,
40-
is_object($dependency) ? get_class($dependency) : gettype($dependency),
37+
get_debug_type($dependency),
4138
));
4239
}
4340
}
@@ -54,12 +51,8 @@ public function evaluateDependency(CacheInterface $cache): void
5451

5552
/**
5653
* @codeCoverageIgnore Method is not used.
57-
*
58-
* @param CacheInterface $cache
59-
*
60-
* @return mixed
6154
*/
62-
protected function generateDependencyData(CacheInterface $cache)
55+
protected function generateDependencyData(CacheInterface $cache): mixed
6356
{
6457
return null;
6558
}

src/Dependency/AnyDependency.php

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77
use Yiisoft\Cache\CacheInterface;
88
use Yiisoft\Cache\Exception\InvalidArgumentException;
99

10-
use function gettype;
11-
use function get_class;
12-
use function is_object;
1310
use function sprintf;
1411

1512
/**
@@ -37,7 +34,7 @@ public function __construct(array $dependencies = [])
3734
throw new InvalidArgumentException(sprintf(
3835
'The dependency must be a "%s" instance, "%s" received',
3936
Dependency::class,
40-
is_object($dependency) ? get_class($dependency) : gettype($dependency),
37+
get_debug_type($dependency),
4138
));
4239
}
4340
}
@@ -54,12 +51,8 @@ public function evaluateDependency(CacheInterface $cache): void
5451

5552
/**
5653
* @codeCoverageIgnore Method is not used.
57-
*
58-
* @param CacheInterface $cache
59-
*
60-
* @return mixed
6154
*/
62-
protected function generateDependencyData(CacheInterface $cache)
55+
protected function generateDependencyData(CacheInterface $cache): mixed
6356
{
6457
return null;
6558
}

0 commit comments

Comments
 (0)