From cf9b149eb3df707b79f30ef986822a6692907fcc Mon Sep 17 00:00:00 2001 From: dehbka Date: Sat, 9 Jul 2022 17:00:10 +0300 Subject: [PATCH] Raise psr/simple-cache to ^2.0|^3.0 and PHP to ^8.0 --- .github/workflows/build.yml | 2 +- .github/workflows/static.yml | 2 +- CHANGELOG.md | 6 ++--- README.md | 2 +- composer.json | 4 +-- src/FileCache.php | 48 +++++++++++------------------------- tests/FileCacheTest.php | 40 ------------------------------ tests/TestCase.php | 8 +++--- 8 files changed, 26 insertions(+), 86 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 0dde6b6..8506ea1 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -28,4 +28,4 @@ jobs: os: >- ['ubuntu-latest', 'windows-latest'] php: >- - ['7.4', '8.0', '8.1'] + ['8.0', '8.1'] diff --git a/.github/workflows/static.yml b/.github/workflows/static.yml index 40ac260..96b2679 100644 --- a/.github/workflows/static.yml +++ b/.github/workflows/static.yml @@ -28,4 +28,4 @@ jobs: os: >- ['ubuntu-latest'] php: >- - ['7.4', '8.0', '8.1'] + ['8.0', '8.1'] diff --git a/CHANGELOG.md b/CHANGELOG.md index 2c05a92..d3326e3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,10 +1,8 @@ # Yii FileCache Change Log +## 2.0.0 under development -## 1.0.2 under development - -- no changes in this release. - +- Chg #44: Raise the minimum `psr/simple-cache` version to `^2.0|^3.0` and the minimum PHP version to `^8.0` (@dehbka) ## 1.0.1 March 23, 2021 diff --git a/README.md b/README.md index af01a89..12d2dd5 100644 --- a/README.md +++ b/README.md @@ -19,7 +19,7 @@ This package implements file-based [PSR-16](https://www.php-fig.org/psr/psr-16/) ## Requirements -- PHP 7.4 or higher. +- PHP 8.0 or higher. ## Installation diff --git a/composer.json b/composer.json index d29962c..232e135 100644 --- a/composer.json +++ b/composer.json @@ -20,8 +20,8 @@ "source": "https://github.com/yiisoft/cache-file" }, "require": { - "php": "^7.4|^8.0", - "psr/simple-cache": "^1.0.1" + "php": "^8.0", + "psr/simple-cache": "^2.0|^3.0" }, "require-dev": { "php-mock/php-mock-phpunit": "^2.6", diff --git a/src/FileCache.php b/src/FileCache.php index edec58a..40e34a7 100644 --- a/src/FileCache.php +++ b/src/FileCache.php @@ -19,11 +19,8 @@ use function fileowner; use function fopen; use function function_exists; -use function gettype; use function is_dir; use function is_file; -use function is_iterable; -use function is_string; use function iterator_to_array; use function opendir; use function posix_geteuid; @@ -111,7 +108,7 @@ public function __construct(string $cachePath) $this->cachePath = $cachePath; } - public function get($key, $default = null) + public function get(string $key, mixed $default = null): mixed { $this->validateKey($key); $file = $this->getCacheFile($key); @@ -128,7 +125,7 @@ public function get($key, $default = null) return unserialize($value); } - public function set($key, $value, $ttl = null): bool + public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool { $this->validateKey($key); $this->gc(); @@ -163,7 +160,7 @@ public function set($key, $value, $ttl = null): bool return touch($file, $expiration); } - public function delete($key): bool + public function delete(string $key): bool { $this->validateKey($key); $file = $this->getCacheFile($key); @@ -181,7 +178,7 @@ public function clear(): bool return true; } - public function getMultiple($keys, $default = null): iterable + public function getMultiple(iterable $keys, mixed $default = null): iterable { $keys = $this->iterableToArray($keys); $this->validateKeys($keys); @@ -194,7 +191,7 @@ public function getMultiple($keys, $default = null): iterable return $results; } - public function setMultiple($values, $ttl = null): bool + public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool { $values = $this->iterableToArray($values); $this->validateKeys(array_map('\strval', array_keys($values))); @@ -206,7 +203,7 @@ public function setMultiple($values, $ttl = null): bool return true; } - public function deleteMultiple($keys): bool + public function deleteMultiple(iterable $keys): bool { $keys = $this->iterableToArray($keys); $this->validateKeys($keys); @@ -218,7 +215,7 @@ public function deleteMultiple($keys): bool return true; } - public function has($key): bool + public function has(string $key): bool { $this->validateKey($key); return $this->existsAndNotExpired($this->getCacheFile($key)); @@ -296,11 +293,11 @@ public function withGcProbability(int $gcProbability): self /** * Converts TTL to expiration. * - * @param DateInterval|int|null $ttl + * @param DateInterval|int|string|null $ttl * * @return int */ - private function ttlToExpiration($ttl): int + private function ttlToExpiration(null|int|string|DateInterval $ttl = null): int { $ttl = $this->normalizeTtl($ttl); @@ -322,7 +319,7 @@ private function ttlToExpiration($ttl): int * * @return int|null TTL value as UNIX timestamp or null meaning infinity */ - private function normalizeTtl($ttl): ?int + private function normalizeTtl(null|int|string|DateInterval $ttl = null): ?int { if ($ttl === null) { return null; @@ -365,7 +362,7 @@ private function getCacheFile(string $key): string $base = $this->cachePath; for ($i = 0; $i < $this->directoryLevel; ++$i) { - if (($prefix = substr($key, $i + $i, 2)) !== false) { + if (($prefix = substr($key, $i + $i, 2)) !== '') { $base .= DIRECTORY_SEPARATOR . $prefix; } } @@ -421,19 +418,13 @@ private function gc(): void } } - /** - * @param mixed $key - */ - private function validateKey($key): void + private function validateKey(string $key): void { - if (!is_string($key) || $key === '' || strpbrk($key, '{}()/\@:')) { + if ($key === '' || strpbrk($key, '{}()/\@:')) { throw new InvalidArgumentException('Invalid key value.'); } } - /** - * @param array $keys - */ private function validateKeys(array $keys): void { foreach ($keys as $key) { @@ -441,11 +432,6 @@ private function validateKeys(array $keys): void } } - /** - * @param string $file - * - * @return bool - */ private function existsAndNotExpired(string $file): bool { return is_file($file) && @filemtime($file) > time(); @@ -454,16 +440,12 @@ private function existsAndNotExpired(string $file): bool /** * Converts iterable to array. If provided value is not iterable it throws an InvalidArgumentException. * - * @param mixed $iterable + * @param iterable $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; } diff --git a/tests/FileCacheTest.php b/tests/FileCacheTest.php index c08020c..319b572 100644 --- a/tests/FileCacheTest.php +++ b/tests/FileCacheTest.php @@ -13,7 +13,6 @@ use phpmock\phpunit\PHPMock; use Psr\SimpleCache\InvalidArgumentException; use ReflectionException; -use stdClass; use Yiisoft\Cache\File\CacheException; use Yiisoft\Cache\File\FileCache; use Yiisoft\Cache\File\MockHelper; @@ -522,12 +521,6 @@ public function testConstructorThrowExceptionForInvalidCacheDirectory(): void public function invalidKeyProvider(): array { return [ - 'int' => [1], - 'float' => [1.1], - 'null' => [null], - 'bool' => [true], - 'object' => [new stdClass()], - 'callable' => [fn () => 'key'], 'psr-reserved' => ['{}()/\@:'], 'empty-string' => [''], ]; @@ -577,28 +570,6 @@ public function testGetMultipleThrowExceptionForInvalidKeys($key): void $this->cache->getMultiple([$key]); } - /** - * @dataProvider invalidKeyProvider - * - * @param mixed $key - */ - public function testGetMultipleThrowExceptionForInvalidKeysNotIterable($key): void - { - $this->expectException(InvalidArgumentException::class); - $this->cache->getMultiple($key); - } - - /** - * @dataProvider invalidKeyProvider - * - * @param mixed $key - */ - public function testSetMultipleThrowExceptionForInvalidKeysNotIterable($key): void - { - $this->expectException(InvalidArgumentException::class); - $this->cache->setMultiple($key); - } - /** * @dataProvider invalidKeyProvider * @@ -610,17 +581,6 @@ public function testDeleteMultipleThrowExceptionForInvalidKeys($key): void $this->cache->deleteMultiple([$key]); } - /** - * @dataProvider invalidKeyProvider - * - * @param mixed $key - */ - public function testDeleteMultipleThrowExceptionForInvalidKeysNotIterable($key): void - { - $this->expectException(InvalidArgumentException::class); - $this->cache->deleteMultiple($key); - } - /** * @dataProvider invalidKeyProvider * diff --git a/tests/TestCase.php b/tests/TestCase.php index 02f4f25..61ba87f 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -28,7 +28,7 @@ abstract class TestCase extends \PHPUnit\Framework\TestCase * * @return mixed */ - protected function invokeMethod(object $object, string $method, array $args = [], bool $revoke = true) + protected function invokeMethod(object $object, string $method, array $args = [], bool $revoke = true): mixed { $reflection = new ReflectionObject($object); $method = $reflection->getMethod($method); @@ -76,7 +76,7 @@ protected function setInaccessibleProperty(object $object, string $propertyName, * * @return mixed */ - protected function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true) + protected function getInaccessibleProperty(object $object, string $propertyName, bool $revoke = true): mixed { $class = new ReflectionClass($object); @@ -116,7 +116,7 @@ public function dataProvider(): array ]; } - public function getDataProviderData($keyPrefix = ''): array + public function getDataProviderData(string $keyPrefix = ''): array { $data = []; @@ -149,7 +149,7 @@ public function prepare(CacheInterface $cache): CacheInterface return $cache; } - public function assertSameExceptObject($expected, $actual): void + public function assertSameExceptObject(mixed $expected, mixed $actual): void { // assert for all types $this->assertEquals($expected, $actual);