From 3d01196b0a41950cedd4a26bd5bb49301db49dc8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?M=C3=A8ir=20Noordermeer?= Date: Sat, 10 Apr 2021 12:45:25 +0200 Subject: [PATCH] Update to PHP 8 --- .gitignore | 3 ++- composer.json | 3 ++- src/Adapter/CacheAdapterInterface.php | 11 ++++---- src/Adapter/FilesystemAdapter.php | 28 ++++++++++----------- src/Adapter/MemoryAdapter.php | 10 ++++---- src/Adapter/NamespaceAdapter.php | 8 +++--- src/Adapter/NullAdapter.php | 6 ++--- src/Adapter/PredisAdapter.php | 20 +++++++-------- src/Serializer/JsonSerializer.php | 10 +++++++- src/Serializer/NoSerializer.php | 4 +-- src/Serializer/SerializerInterface.php | 12 +++++++-- test/Unit/Adapter/FilesystemAdapterTest.php | 2 +- test/Unit/Adapter/NamespaceAdapterTest.php | 2 +- test/Unit/Serializer/JsonSerializerTest.php | 2 -- 14 files changed, 69 insertions(+), 52 deletions(-) diff --git a/.gitignore b/.gitignore index efcaef4..200e23b 100644 --- a/.gitignore +++ b/.gitignore @@ -4,4 +4,5 @@ composer.lock vendor/ tmp/ .vs/ -.vscode/ \ No newline at end of file +.vscode/ +.phpunit* \ No newline at end of file diff --git a/composer.json b/composer.json index 785a058..fe6a46c 100644 --- a/composer.json +++ b/composer.json @@ -4,10 +4,11 @@ "description": "Simple cache library with Redis and Filesystem adapters.", "license": "MIT", "require": { + "php": "^8.0", "predis/predis": "^1.1" }, "require-dev": { - "phpunit/phpunit": "^6.3" + "phpunit/phpunit": "^9.5" }, "autoload": { "psr-4": { diff --git a/src/Adapter/CacheAdapterInterface.php b/src/Adapter/CacheAdapterInterface.php index 8de2431..14e38b0 100644 --- a/src/Adapter/CacheAdapterInterface.php +++ b/src/Adapter/CacheAdapterInterface.php @@ -5,11 +5,12 @@ namespace IQParts\Cache\Adapter; interface CacheAdapterInterface { public const NO_TTL = -1; + /** * @param string $key * @return mixed */ - public function get(string $key); + public function get(string $key): mixed; /** * @param string $key @@ -21,15 +22,15 @@ interface CacheAdapterInterface /** * @param string $key - * @return mixed + * @return void */ - public function delete(string $key); + public function delete(string $key): void; /** * @param string $key - * @return mixed + * @return array */ - public function keys($key = '*'); + public function keys($key = '*'): array; /** * @param $key diff --git a/src/Adapter/FilesystemAdapter.php b/src/Adapter/FilesystemAdapter.php index 507bea5..63695c8 100644 --- a/src/Adapter/FilesystemAdapter.php +++ b/src/Adapter/FilesystemAdapter.php @@ -9,39 +9,39 @@ class FilesystemAdapter implements CacheAdapterInterface /** * @var string */ - private $directory; + private string $directory; /** * @var null|int */ - private $chmod; + private ?int $chmod; /** * @var string */ - private $directorySeparator; + private ?string $directorySeparator; /** * @var int|null */ - private $ttl; + private ?int $ttl; /** * @var array */ - private $timeToLive; + private mixed $timeToLive; /** * @var SerializerInterface */ - private $serializer; + private SerializerInterface $serializer; /** * @var array */ - private $index; + private mixed $index; /** * @var string */ - private $indexLocation; + private string $indexLocation; /** * @var string */ - private $ttlLocation; + private string $ttlLocation; /** * FilesystemAdapter constructor. @@ -84,7 +84,7 @@ class FilesystemAdapter implements CacheAdapterInterface * @param string $key * @return mixed */ - public function get(string $key) + public function get(string $key): mixed { $file = $this->getFilename($key); if ($this->exists($file)) { @@ -126,9 +126,9 @@ class FilesystemAdapter implements CacheAdapterInterface /** * @param string $key */ - public function delete(string $key) + public function delete(string $key): void { - if (strpos($key, '*') !== false) { + if (str_contains($key, '*')) { $this->deleteGlob($key); $this->saveIndex(); } else { @@ -143,9 +143,9 @@ class FilesystemAdapter implements CacheAdapterInterface /** * @param string $key - * @return mixed + * @return array */ - public function keys($key = '*') + public function keys($key = '*'): array { $matches = []; foreach ($this->index as $name => $index) { diff --git a/src/Adapter/MemoryAdapter.php b/src/Adapter/MemoryAdapter.php index 6452a53..c2f569d 100644 --- a/src/Adapter/MemoryAdapter.php +++ b/src/Adapter/MemoryAdapter.php @@ -8,17 +8,17 @@ final class MemoryAdapter implements CacheAdapterInterface /** * @var array */ - private $data = []; + private array $data = []; /** * @var array */ - private $timeToLive = []; + private array $timeToLive = []; /** * @param string $key * @return mixed */ - public function get(string $key) + public function get(string $key): mixed { if (!isset($this->data[$key])) { return null; @@ -61,9 +61,9 @@ final class MemoryAdapter implements CacheAdapterInterface /** * @param string $key - * @return mixed + * @return array */ - public function keys($key = '*') + public function keys($key = '*'): array { $matches = []; foreach ($this->data as $name => $value) { diff --git a/src/Adapter/NamespaceAdapter.php b/src/Adapter/NamespaceAdapter.php index 2c2d3a4..6801e6a 100644 --- a/src/Adapter/NamespaceAdapter.php +++ b/src/Adapter/NamespaceAdapter.php @@ -7,11 +7,11 @@ final class NamespaceAdapter implements CacheAdapterInterface /** * @var string */ - private $namespace; + private string $namespace; /** * @var CacheAdapterInterface */ - private $cacheAdapter; + private CacheAdapterInterface $cacheAdapter; /** * NamespaceAdapter constructor. @@ -28,7 +28,7 @@ final class NamespaceAdapter implements CacheAdapterInterface * @param string $key * @return mixed */ - public function get(string $key) + public function get(string $key): mixed { return $this->cacheAdapter->get($this->namespace . $key); } @@ -69,7 +69,7 @@ final class NamespaceAdapter implements CacheAdapterInterface $len = \strlen($this->namespace); foreach ($keys as $cacheKey) { - if (0 === strpos($cacheKey, $this->namespace)) { + if (str_starts_with($cacheKey, $this->namespace)) { $allowed[] = substr($cacheKey, $len); } } diff --git a/src/Adapter/NullAdapter.php b/src/Adapter/NullAdapter.php index 5aaa653..4e1be91 100644 --- a/src/Adapter/NullAdapter.php +++ b/src/Adapter/NullAdapter.php @@ -9,7 +9,7 @@ final class NullAdapter implements CacheAdapterInterface * @param string $key * @return mixed */ - public function get(string $key) + public function get(string $key): mixed { return null; } @@ -34,9 +34,9 @@ final class NullAdapter implements CacheAdapterInterface /** * @param string $key - * @return mixed + * @return array */ - public function keys($key = '') + public function keys($key = ''): array { return []; } diff --git a/src/Adapter/PredisAdapter.php b/src/Adapter/PredisAdapter.php index dd92a80..95d1e1b 100644 --- a/src/Adapter/PredisAdapter.php +++ b/src/Adapter/PredisAdapter.php @@ -9,17 +9,17 @@ use Predis\ClientInterface; final class PredisAdapter implements CacheAdapterInterface { /** - * @var Client + * @var ClientInterface */ - private $client; + private ClientInterface $client; /** * @var SerializerInterface */ - private $serializer; + private SerializerInterface $serializer; /** * @var null */ - private $defaultTtl; + private ?int $defaultTtl; /** * PredisAdapter constructor. @@ -38,7 +38,7 @@ final class PredisAdapter implements CacheAdapterInterface * @param string $key * @return mixed */ - public function get(string $key) + public function get(string $key): mixed { return $this->serializer->deserialize($this->client->get($key)); } @@ -59,18 +59,18 @@ final class PredisAdapter implements CacheAdapterInterface /** * @param string $key - * @return integer + * @return void */ - public function delete(string $key): int + public function delete(string $key): void { - return $this->client->del([$key]); + $this->client->del([$key]); } /** * @param string $key - * @return mixed + * @return array */ - public function keys($key = '*') + public function keys($key = '*'): array { return $this->client->keys($key); } diff --git a/src/Serializer/JsonSerializer.php b/src/Serializer/JsonSerializer.php index a5eabbb..f526f48 100644 --- a/src/Serializer/JsonSerializer.php +++ b/src/Serializer/JsonSerializer.php @@ -4,6 +4,10 @@ namespace IQParts\Cache\Serializer; final class JsonSerializer implements SerializerInterface { + /** + * @param $input + * @return string + */ public function serialize($input): string { if (\is_array($input)) { @@ -13,7 +17,11 @@ final class JsonSerializer implements SerializerInterface return $input; } - public function deserialize($string) + /** + * @param $string + * @return mixed + */ + public function deserialize($string): mixed { return json_decode($string, true) ?? $string; } diff --git a/src/Serializer/NoSerializer.php b/src/Serializer/NoSerializer.php index 0d56db6..a0a1737 100644 --- a/src/Serializer/NoSerializer.php +++ b/src/Serializer/NoSerializer.php @@ -5,12 +5,12 @@ namespace IQParts\Cache\Serializer; final class NoSerializer implements SerializerInterface { - public function serialize($input) + public function serialize($input): string { return $input; } - public function deserialize($string) + public function deserialize($string): mixed { return $string; } diff --git a/src/Serializer/SerializerInterface.php b/src/Serializer/SerializerInterface.php index 3f4286f..0280a4c 100644 --- a/src/Serializer/SerializerInterface.php +++ b/src/Serializer/SerializerInterface.php @@ -4,7 +4,15 @@ namespace IQParts\Cache\Serializer; interface SerializerInterface { - public function serialize($input); + /** + * @param $input + * @return string + */ + public function serialize($input): string; - public function deserialize($string); + /** + * @param $string + * @return mixed + */ + public function deserialize($string): mixed; } \ No newline at end of file diff --git a/test/Unit/Adapter/FilesystemAdapterTest.php b/test/Unit/Adapter/FilesystemAdapterTest.php index b4295e5..0d559c7 100644 --- a/test/Unit/Adapter/FilesystemAdapterTest.php +++ b/test/Unit/Adapter/FilesystemAdapterTest.php @@ -121,7 +121,7 @@ final class FilesystemAdapterTest extends AbstractTestCase $adapter->delete('a/b'); } - protected function tearDown() + protected function tearDown(): void { parent::tearDown(); $files = new RecursiveIteratorIterator( diff --git a/test/Unit/Adapter/NamespaceAdapterTest.php b/test/Unit/Adapter/NamespaceAdapterTest.php index 69aa800..6d3da27 100644 --- a/test/Unit/Adapter/NamespaceAdapterTest.php +++ b/test/Unit/Adapter/NamespaceAdapterTest.php @@ -15,7 +15,7 @@ final class NamespaceAdapterTest extends AbstractTestCase */ private $adapter; - public function setUp() + public function setUp(): void { $this->adapter = new MemoryAdapter(); } diff --git a/test/Unit/Serializer/JsonSerializerTest.php b/test/Unit/Serializer/JsonSerializerTest.php index 01e3bde..0ffe434 100644 --- a/test/Unit/Serializer/JsonSerializerTest.php +++ b/test/Unit/Serializer/JsonSerializerTest.php @@ -13,9 +13,7 @@ final class JsonSerializerTest extends AbstractTestCase $string = 'myString'; $array = [0 => '1', 1 => '2', 2 => '3']; - $this->assertInternalType('string', $serializer->serialize($array)); $this->assertEquals($string, $serializer->serialize($string)); - $this->assertEquals($array, $serializer->deserialize($serializer->serialize($array))); }