Skip to content

Commit 3ae429a

Browse files
authored
Raise psr/simple-cache to ^2.0|^3.0 and PHP to ^8.0 (#10)
1 parent f6c8b45 commit 3ae429a

7 files changed

Lines changed: 30 additions & 87 deletions

File tree

.github/workflows/build.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ jobs:
3636
- ubuntu-latest
3737

3838
php:
39-
- 7.4
4039
- 8.0
4140
- 8.1
4241

.github/workflows/static.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ jobs:
3333
- ubuntu-latest
3434

3535
php:
36-
- 7.4
3736
- 8.0
3837
- 8.1
3938

CHANGELOG.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
# Yii Redis Cache Change Log
22

3+
## 2.0.0 under development
34

4-
## 1.0.1 under development
5-
6-
- no changes in this release.
5+
- Chg #5: Raise the minimum `psr/simple-cache` version to `^2.0|^3.0` and the minimum PHP version to `^8.0` (@dehbka)
76

87
## 1.0.0 November 07, 2021
98

10-
- Initial release.
9+
- Initial release.

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ implements [PSR-16](https://www.php-fig.org/psr/psr-16/) cache.
2020

2121
## Requirements
2222

23-
- PHP 7.4 or higher.
23+
- PHP 8.0 or higher.
2424

2525
## Installation
2626

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,9 @@
3030
}
3131
],
3232
"require": {
33-
"php": "^7.4|^8.0",
33+
"php": "^8.0",
3434
"predis/predis": "^1.1",
35-
"psr/simple-cache": "^1.0.1"
35+
"psr/simple-cache": "^2.0|^3.0"
3636
},
3737
"require-dev": {
3838
"phpunit/phpunit": "^9.5",

src/RedisCache.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,6 @@
1515
use function array_keys;
1616
use function array_map;
1717
use function count;
18-
use function gettype;
19-
use function is_iterable;
20-
use function is_string;
2118
use function iterator_to_array;
2219
use function serialize;
2320
use function strpbrk;
@@ -43,14 +40,14 @@ public function __construct(ClientInterface $client)
4340
$this->client = $client;
4441
}
4542

46-
public function get($key, $default = null)
43+
public function get(string $key, mixed $default = null): mixed
4744
{
4845
$this->validateKey($key);
4946
$value = $this->client->get($key);
5047
return $value === null ? $default : unserialize($value);
5148
}
5249

53-
public function set($key, $value, $ttl = null): bool
50+
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
5451
{
5552
$ttl = $this->normalizeTtl($ttl);
5653

@@ -69,7 +66,7 @@ public function set($key, $value, $ttl = null): bool
6966
return $result !== null;
7067
}
7168

72-
public function delete($key): bool
69+
public function delete(string $key): bool
7370
{
7471
return !$this->has($key) || $this->client->del($key) === 1;
7572
}
@@ -79,27 +76,27 @@ public function clear(): bool
7976
return $this->client->flushdb() !== null;
8077
}
8178

82-
public function getMultiple($keys, $default = null): iterable
79+
public function getMultiple(iterable $keys, mixed $default = null): iterable
8380
{
81+
/** @var string[] $keys */
8482
$keys = $this->iterableToArray($keys);
8583
$this->validateKeys($keys);
86-
/** @var string[] $keys */
8784
$values = array_fill_keys($keys, $default);
8885
/** @var null[]|string[] $valuesFromCache */
8986
$valuesFromCache = $this->client->mget($keys);
9087

9188
$i = 0;
92-
/** @var mixed $default */
93-
foreach ($values as $key => $default) {
94-
/** @psalm-suppress MixedAssignment */
95-
$values[$key] = isset($valuesFromCache[$i]) ? unserialize($valuesFromCache[$i]) : $default;
89+
90+
/** @psalm-suppress MixedAssignment */
91+
foreach ($values as $key => $value) {
92+
$values[$key] = isset($valuesFromCache[$i]) ? unserialize($valuesFromCache[$i]) : $value;
9693
$i++;
9794
}
9895

9996
return $values;
10097
}
10198

102-
public function setMultiple($values, $ttl = null): bool
99+
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
103100
{
104101
$values = $this->iterableToArray($values);
105102
$keys = array_map('\strval', array_keys($values));
@@ -140,7 +137,7 @@ public function setMultiple($values, $ttl = null): bool
140137
return true;
141138
}
142139

143-
public function deleteMultiple($keys): bool
140+
public function deleteMultiple(iterable $keys): bool
144141
{
145142
$keys = $this->iterableToArray($keys);
146143

@@ -154,7 +151,7 @@ public function deleteMultiple($keys): bool
154151
return empty($keys) || $this->client->del($keys) === count($keys);
155152
}
156153

157-
public function has($key): bool
154+
public function has(string $key): bool
158155
{
159156
$this->validateKey($key);
160157
$ttl = $this->client->ttl($key);
@@ -167,9 +164,9 @@ public function has($key): bool
167164
*
168165
* @param DateInterval|int|string|null $ttl The raw TTL.
169166
*
170-
* @return int TTL value as UNIX timestamp.
167+
* @return int|null TTL value as UNIX timestamp.
171168
*/
172-
private function normalizeTtl($ttl): ?int
169+
private function normalizeTtl(null|int|string|DateInterval $ttl): ?int
173170
{
174171
if ($ttl === null) {
175172
return null;
@@ -185,42 +182,34 @@ private function normalizeTtl($ttl): ?int
185182
}
186183

187184
/**
188-
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException.
185+
* Converts iterable to array.
189186
*
190-
* @param mixed $iterable
187+
* @param iterable $iterable
191188
*
192189
* @return array
193190
*/
194-
private function iterableToArray($iterable): array
191+
private function iterableToArray(iterable $iterable): array
195192
{
196-
if (!is_iterable($iterable)) {
197-
throw new InvalidArgumentException('Iterable is expected, got ' . gettype($iterable));
198-
}
199-
200193
/** @psalm-suppress RedundantCast */
201194
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
202195
}
203196

204-
/**
205-
* @param mixed $key
206-
*/
207-
private function validateKey($key): void
197+
private function validateKey(string $key): void
208198
{
209-
if (!is_string($key) || $key === '' || strpbrk($key, '{}()/\@:')) {
199+
if ($key === '' || strpbrk($key, '{}()/\@:')) {
210200
throw new InvalidArgumentException('Invalid key value.');
211201
}
212202
}
213203

214204
/**
215-
* @param array $keys
205+
* @param string[] $keys
216206
*/
217207
private function validateKeys(array $keys): void
218208
{
219-
if (empty($keys)) {
209+
if ([] === $keys) {
220210
throw new InvalidArgumentException('Invalid key values.');
221211
}
222212

223-
/** @var mixed $key */
224213
foreach ($keys as $key) {
225214
$this->validateKey($key);
226215
}

tests/RedisCacheTest.php

Lines changed: 3 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
use function array_keys;
2121
use function array_map;
22-
use function gettype;
2322
use function is_array;
2423
use function is_object;
2524
use function time;
@@ -75,7 +74,7 @@ public function dataProvider(): array
7574
*
7675
* @throws InvalidArgumentException
7776
*/
78-
public function testSet($key, $value): void
77+
public function testSet(string $key, mixed $value): void
7978
{
8079
for ($i = 0; $i < 2; $i++) {
8180
$this->assertTrue($this->cache->set($key, $value));
@@ -90,7 +89,7 @@ public function testSet($key, $value): void
9089
*
9190
* @throws InvalidArgumentException
9291
*/
93-
public function testSetWithTtl($key, $value): void
92+
public function testSetWithTtl(string $key, mixed $value): void
9493
{
9594
for ($i = 0; $i < 2; $i++) {
9695
$this->assertTrue($this->cache->set($key, $value, 3600));
@@ -407,12 +406,6 @@ public function testValuesAsIterable(array $array, iterable $iterable): void
407406
public function invalidKeyProvider(): array
408407
{
409408
return [
410-
'int' => [1],
411-
'float' => [1.1],
412-
'null' => [null],
413-
'bool' => [true],
414-
'object' => [new stdClass()],
415-
'callable' => [fn () => 'key'],
416409
'psr-reserved' => ['{}()/\@:'],
417410
'empty-string' => [''],
418411
];
@@ -462,30 +455,6 @@ public function testGetMultipleThrowExceptionForInvalidKeys($key): void
462455
$this->cache->getMultiple([$key]);
463456
}
464457

465-
/**
466-
* @dataProvider invalidKeyProvider
467-
*
468-
* @param mixed $key
469-
*/
470-
public function testGetMultipleThrowExceptionForInvalidKeysNotIterable($key): void
471-
{
472-
$this->expectException(InvalidArgumentException::class);
473-
$this->expectExceptionMessage('Iterable is expected, got ' . gettype($key));
474-
$this->cache->getMultiple($key);
475-
}
476-
477-
/**
478-
* @dataProvider invalidKeyProvider
479-
*
480-
* @param mixed $key
481-
*/
482-
public function testSetMultipleThrowExceptionForInvalidKeysNotIterable($key): void
483-
{
484-
$this->expectException(InvalidArgumentException::class);
485-
$this->expectExceptionMessage('Iterable is expected, got ' . gettype($key));
486-
$this->cache->setMultiple($key);
487-
}
488-
489458
/**
490459
* @dataProvider invalidKeyProvider
491460
*
@@ -497,18 +466,6 @@ public function testDeleteMultipleThrowExceptionForInvalidKeys($key): void
497466
$this->cache->deleteMultiple([$key]);
498467
}
499468

500-
/**
501-
* @dataProvider invalidKeyProvider
502-
*
503-
* @param mixed $key
504-
*/
505-
public function testDeleteMultipleThrowExceptionForInvalidKeysNotIterable($key): void
506-
{
507-
$this->expectException(InvalidArgumentException::class);
508-
$this->expectExceptionMessage('Iterable is expected, got ' . gettype($key));
509-
$this->cache->deleteMultiple($key);
510-
}
511-
512469
/**
513470
* @dataProvider invalidKeyProvider
514471
*
@@ -549,7 +506,7 @@ private function getDataProviderData(): array
549506
return $data;
550507
}
551508

552-
private function assertSameExceptObject($expected, $actual): void
509+
private function assertSameExceptObject(mixed $expected, mixed $actual): void
553510
{
554511
// Assert for all types.
555512
$this->assertEquals($expected, $actual);

0 commit comments

Comments
 (0)