Skip to content

Commit 72224f8

Browse files
authored
Fix #43: Raise the minimum psr/simple-cache version to ^2.0|^3.0 and the minimum PHP version to ^8.0
1 parent d7879cb commit 72224f8

8 files changed

Lines changed: 26 additions & 86 deletions

File tree

.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: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
# Yii FileCache Change Log
22

3+
## 2.0.0 under development
34

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

97
## 1.0.1 March 23, 2021
108

README.md

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

2020
## Requirements
2121

22-
- PHP 7.4 or higher.
22+
- PHP 8.0 or higher.
2323

2424
## Installation
2525

composer.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
"source": "https://github.com/yiisoft/cache-file"
2121
},
2222
"require": {
23-
"php": "^7.4|^8.0",
24-
"psr/simple-cache": "^1.0.1"
23+
"php": "^8.0",
24+
"psr/simple-cache": "^2.0|^3.0"
2525
},
2626
"require-dev": {
2727
"php-mock/php-mock-phpunit": "^2.6",

src/FileCache.php

Lines changed: 15 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,8 @@
1919
use function fileowner;
2020
use function fopen;
2121
use function function_exists;
22-
use function gettype;
2322
use function is_dir;
2423
use function is_file;
25-
use function is_iterable;
26-
use function is_string;
2724
use function iterator_to_array;
2825
use function opendir;
2926
use function posix_geteuid;
@@ -111,7 +108,7 @@ public function __construct(string $cachePath)
111108
$this->cachePath = $cachePath;
112109
}
113110

114-
public function get($key, $default = null)
111+
public function get(string $key, mixed $default = null): mixed
115112
{
116113
$this->validateKey($key);
117114
$file = $this->getCacheFile($key);
@@ -128,7 +125,7 @@ public function get($key, $default = null)
128125
return unserialize($value);
129126
}
130127

131-
public function set($key, $value, $ttl = null): bool
128+
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
132129
{
133130
$this->validateKey($key);
134131
$this->gc();
@@ -163,7 +160,7 @@ public function set($key, $value, $ttl = null): bool
163160
return touch($file, $expiration);
164161
}
165162

166-
public function delete($key): bool
163+
public function delete(string $key): bool
167164
{
168165
$this->validateKey($key);
169166
$file = $this->getCacheFile($key);
@@ -181,7 +178,7 @@ public function clear(): bool
181178
return true;
182179
}
183180

184-
public function getMultiple($keys, $default = null): iterable
181+
public function getMultiple(iterable $keys, mixed $default = null): iterable
185182
{
186183
$keys = $this->iterableToArray($keys);
187184
$this->validateKeys($keys);
@@ -194,7 +191,7 @@ public function getMultiple($keys, $default = null): iterable
194191
return $results;
195192
}
196193

197-
public function setMultiple($values, $ttl = null): bool
194+
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
198195
{
199196
$values = $this->iterableToArray($values);
200197
$this->validateKeys(array_map('\strval', array_keys($values)));
@@ -206,7 +203,7 @@ public function setMultiple($values, $ttl = null): bool
206203
return true;
207204
}
208205

209-
public function deleteMultiple($keys): bool
206+
public function deleteMultiple(iterable $keys): bool
210207
{
211208
$keys = $this->iterableToArray($keys);
212209
$this->validateKeys($keys);
@@ -218,7 +215,7 @@ public function deleteMultiple($keys): bool
218215
return true;
219216
}
220217

221-
public function has($key): bool
218+
public function has(string $key): bool
222219
{
223220
$this->validateKey($key);
224221
return $this->existsAndNotExpired($this->getCacheFile($key));
@@ -296,11 +293,11 @@ public function withGcProbability(int $gcProbability): self
296293
/**
297294
* Converts TTL to expiration.
298295
*
299-
* @param DateInterval|int|null $ttl
296+
* @param DateInterval|int|string|null $ttl
300297
*
301298
* @return int
302299
*/
303-
private function ttlToExpiration($ttl): int
300+
private function ttlToExpiration(null|int|string|DateInterval $ttl = null): int
304301
{
305302
$ttl = $this->normalizeTtl($ttl);
306303

@@ -322,7 +319,7 @@ private function ttlToExpiration($ttl): int
322319
*
323320
* @return int|null TTL value as UNIX timestamp or null meaning infinity
324321
*/
325-
private function normalizeTtl($ttl): ?int
322+
private function normalizeTtl(null|int|string|DateInterval $ttl = null): ?int
326323
{
327324
if ($ttl === null) {
328325
return null;
@@ -365,7 +362,7 @@ private function getCacheFile(string $key): string
365362
$base = $this->cachePath;
366363

367364
for ($i = 0; $i < $this->directoryLevel; ++$i) {
368-
if (($prefix = substr($key, $i + $i, 2)) !== false) {
365+
if (($prefix = substr($key, $i + $i, 2)) !== '') {
369366
$base .= DIRECTORY_SEPARATOR . $prefix;
370367
}
371368
}
@@ -421,31 +418,20 @@ private function gc(): void
421418
}
422419
}
423420

424-
/**
425-
* @param mixed $key
426-
*/
427-
private function validateKey($key): void
421+
private function validateKey(string $key): void
428422
{
429-
if (!is_string($key) || $key === '' || strpbrk($key, '{}()/\@:')) {
423+
if ($key === '' || strpbrk($key, '{}()/\@:')) {
430424
throw new InvalidArgumentException('Invalid key value.');
431425
}
432426
}
433427

434-
/**
435-
* @param array $keys
436-
*/
437428
private function validateKeys(array $keys): void
438429
{
439430
foreach ($keys as $key) {
440431
$this->validateKey($key);
441432
}
442433
}
443434

444-
/**
445-
* @param string $file
446-
*
447-
* @return bool
448-
*/
449435
private function existsAndNotExpired(string $file): bool
450436
{
451437
return is_file($file) && @filemtime($file) > time();
@@ -454,16 +440,12 @@ private function existsAndNotExpired(string $file): bool
454440
/**
455441
* Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException.
456442
*
457-
* @param mixed $iterable
443+
* @param iterable $iterable
458444
*
459445
* @return array
460446
*/
461-
private function iterableToArray($iterable): array
447+
private function iterableToArray(iterable $iterable): array
462448
{
463-
if (!is_iterable($iterable)) {
464-
throw new InvalidArgumentException('Iterable is expected, got ' . gettype($iterable));
465-
}
466-
467449
/** @psalm-suppress RedundantCast */
468450
return $iterable instanceof Traversable ? iterator_to_array($iterable) : (array) $iterable;
469451
}

tests/FileCacheTest.php

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
use phpmock\phpunit\PHPMock;
1414
use Psr\SimpleCache\InvalidArgumentException;
1515
use ReflectionException;
16-
use stdClass;
1716
use Yiisoft\Cache\File\CacheException;
1817
use Yiisoft\Cache\File\FileCache;
1918
use Yiisoft\Cache\File\MockHelper;
@@ -522,12 +521,6 @@ public function testConstructorThrowExceptionForInvalidCacheDirectory(): void
522521
public function invalidKeyProvider(): array
523522
{
524523
return [
525-
'int' => [1],
526-
'float' => [1.1],
527-
'null' => [null],
528-
'bool' => [true],
529-
'object' => [new stdClass()],
530-
'callable' => [fn () => 'key'],
531524
'psr-reserved' => ['{}()/\@:'],
532525
'empty-string' => [''],
533526
];
@@ -577,28 +570,6 @@ public function testGetMultipleThrowExceptionForInvalidKeys($key): void
577570
$this->cache->getMultiple([$key]);
578571
}
579572

580-
/**
581-
* @dataProvider invalidKeyProvider
582-
*
583-
* @param mixed $key
584-
*/
585-
public function testGetMultipleThrowExceptionForInvalidKeysNotIterable($key): void
586-
{
587-
$this->expectException(InvalidArgumentException::class);
588-
$this->cache->getMultiple($key);
589-
}
590-
591-
/**
592-
* @dataProvider invalidKeyProvider
593-
*
594-
* @param mixed $key
595-
*/
596-
public function testSetMultipleThrowExceptionForInvalidKeysNotIterable($key): void
597-
{
598-
$this->expectException(InvalidArgumentException::class);
599-
$this->cache->setMultiple($key);
600-
}
601-
602573
/**
603574
* @dataProvider invalidKeyProvider
604575
*
@@ -610,17 +581,6 @@ public function testDeleteMultipleThrowExceptionForInvalidKeys($key): void
610581
$this->cache->deleteMultiple([$key]);
611582
}
612583

613-
/**
614-
* @dataProvider invalidKeyProvider
615-
*
616-
* @param mixed $key
617-
*/
618-
public function testDeleteMultipleThrowExceptionForInvalidKeysNotIterable($key): void
619-
{
620-
$this->expectException(InvalidArgumentException::class);
621-
$this->cache->deleteMultiple($key);
622-
}
623-
624584
/**
625585
* @dataProvider invalidKeyProvider
626586
*

tests/TestCase.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase
2828
*
2929
* @return mixed
3030
*/
31-
protected function invokeMethod(object $object, string $method, array $args = [], bool $revoke = true)
31+
protected function invokeMethod(object $object, string $method, array $args = [], bool $revoke = true): mixed
3232
{
3333
$reflection = new ReflectionObject($object);
3434
$method = $reflection->getMethod($method);
@@ -76,7 +76,7 @@ protected function setInaccessibleProperty(object $object, string $propertyName,
7676
*
7777
* @return mixed
7878
*/
79-
protected function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true)
79+
protected function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true): mixed
8080
{
8181
$class = new ReflectionClass($object);
8282

@@ -116,7 +116,7 @@ public function dataProvider(): array
116116
];
117117
}
118118

119-
public function getDataProviderData($keyPrefix = ''): array
119+
public function getDataProviderData(string $keyPrefix = ''): array
120120
{
121121
$data = [];
122122

@@ -149,7 +149,7 @@ public function prepare(CacheInterface $cache): CacheInterface
149149
return $cache;
150150
}
151151

152-
public function assertSameExceptObject($expected, $actual): void
152+
public function assertSameExceptObject(mixed $expected, mixed $actual): void
153153
{
154154
// assert for all types
155155
$this->assertEquals($expected, $actual);

0 commit comments

Comments
 (0)