Update to PHP 8

This commit is contained in:
2021-04-10 12:45:25 +02:00
parent d992c767e8
commit 3d01196b0a
14 changed files with 69 additions and 52 deletions

3
.gitignore vendored
View File

@@ -4,4 +4,5 @@ composer.lock
vendor/ vendor/
tmp/ tmp/
.vs/ .vs/
.vscode/ .vscode/
.phpunit*

View File

@@ -4,10 +4,11 @@
"description": "Simple cache library with Redis and Filesystem adapters.", "description": "Simple cache library with Redis and Filesystem adapters.",
"license": "MIT", "license": "MIT",
"require": { "require": {
"php": "^8.0",
"predis/predis": "^1.1" "predis/predis": "^1.1"
}, },
"require-dev": { "require-dev": {
"phpunit/phpunit": "^6.3" "phpunit/phpunit": "^9.5"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View File

@@ -5,11 +5,12 @@ namespace IQParts\Cache\Adapter;
interface CacheAdapterInterface interface CacheAdapterInterface
{ {
public const NO_TTL = -1; public const NO_TTL = -1;
/** /**
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function get(string $key); public function get(string $key): mixed;
/** /**
* @param string $key * @param string $key
@@ -21,15 +22,15 @@ interface CacheAdapterInterface
/** /**
* @param string $key * @param string $key
* @return mixed * @return void
*/ */
public function delete(string $key); public function delete(string $key): void;
/** /**
* @param string $key * @param string $key
* @return mixed * @return array
*/ */
public function keys($key = '*'); public function keys($key = '*'): array;
/** /**
* @param $key * @param $key

View File

@@ -9,39 +9,39 @@ class FilesystemAdapter implements CacheAdapterInterface
/** /**
* @var string * @var string
*/ */
private $directory; private string $directory;
/** /**
* @var null|int * @var null|int
*/ */
private $chmod; private ?int $chmod;
/** /**
* @var string * @var string
*/ */
private $directorySeparator; private ?string $directorySeparator;
/** /**
* @var int|null * @var int|null
*/ */
private $ttl; private ?int $ttl;
/** /**
* @var array * @var array
*/ */
private $timeToLive; private mixed $timeToLive;
/** /**
* @var SerializerInterface * @var SerializerInterface
*/ */
private $serializer; private SerializerInterface $serializer;
/** /**
* @var array * @var array
*/ */
private $index; private mixed $index;
/** /**
* @var string * @var string
*/ */
private $indexLocation; private string $indexLocation;
/** /**
* @var string * @var string
*/ */
private $ttlLocation; private string $ttlLocation;
/** /**
* FilesystemAdapter constructor. * FilesystemAdapter constructor.
@@ -84,7 +84,7 @@ class FilesystemAdapter implements CacheAdapterInterface
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function get(string $key) public function get(string $key): mixed
{ {
$file = $this->getFilename($key); $file = $this->getFilename($key);
if ($this->exists($file)) { if ($this->exists($file)) {
@@ -126,9 +126,9 @@ class FilesystemAdapter implements CacheAdapterInterface
/** /**
* @param string $key * @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->deleteGlob($key);
$this->saveIndex(); $this->saveIndex();
} else { } else {
@@ -143,9 +143,9 @@ class FilesystemAdapter implements CacheAdapterInterface
/** /**
* @param string $key * @param string $key
* @return mixed * @return array
*/ */
public function keys($key = '*') public function keys($key = '*'): array
{ {
$matches = []; $matches = [];
foreach ($this->index as $name => $index) { foreach ($this->index as $name => $index) {

View File

@@ -8,17 +8,17 @@ final class MemoryAdapter implements CacheAdapterInterface
/** /**
* @var array * @var array
*/ */
private $data = []; private array $data = [];
/** /**
* @var array * @var array
*/ */
private $timeToLive = []; private array $timeToLive = [];
/** /**
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function get(string $key) public function get(string $key): mixed
{ {
if (!isset($this->data[$key])) { if (!isset($this->data[$key])) {
return null; return null;
@@ -61,9 +61,9 @@ final class MemoryAdapter implements CacheAdapterInterface
/** /**
* @param string $key * @param string $key
* @return mixed * @return array
*/ */
public function keys($key = '*') public function keys($key = '*'): array
{ {
$matches = []; $matches = [];
foreach ($this->data as $name => $value) { foreach ($this->data as $name => $value) {

View File

@@ -7,11 +7,11 @@ final class NamespaceAdapter implements CacheAdapterInterface
/** /**
* @var string * @var string
*/ */
private $namespace; private string $namespace;
/** /**
* @var CacheAdapterInterface * @var CacheAdapterInterface
*/ */
private $cacheAdapter; private CacheAdapterInterface $cacheAdapter;
/** /**
* NamespaceAdapter constructor. * NamespaceAdapter constructor.
@@ -28,7 +28,7 @@ final class NamespaceAdapter implements CacheAdapterInterface
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function get(string $key) public function get(string $key): mixed
{ {
return $this->cacheAdapter->get($this->namespace . $key); return $this->cacheAdapter->get($this->namespace . $key);
} }
@@ -69,7 +69,7 @@ final class NamespaceAdapter implements CacheAdapterInterface
$len = \strlen($this->namespace); $len = \strlen($this->namespace);
foreach ($keys as $cacheKey) { foreach ($keys as $cacheKey) {
if (0 === strpos($cacheKey, $this->namespace)) { if (str_starts_with($cacheKey, $this->namespace)) {
$allowed[] = substr($cacheKey, $len); $allowed[] = substr($cacheKey, $len);
} }
} }

View File

@@ -9,7 +9,7 @@ final class NullAdapter implements CacheAdapterInterface
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function get(string $key) public function get(string $key): mixed
{ {
return null; return null;
} }
@@ -34,9 +34,9 @@ final class NullAdapter implements CacheAdapterInterface
/** /**
* @param string $key * @param string $key
* @return mixed * @return array
*/ */
public function keys($key = '') public function keys($key = ''): array
{ {
return []; return [];
} }

View File

@@ -9,17 +9,17 @@ use Predis\ClientInterface;
final class PredisAdapter implements CacheAdapterInterface final class PredisAdapter implements CacheAdapterInterface
{ {
/** /**
* @var Client * @var ClientInterface
*/ */
private $client; private ClientInterface $client;
/** /**
* @var SerializerInterface * @var SerializerInterface
*/ */
private $serializer; private SerializerInterface $serializer;
/** /**
* @var null * @var null
*/ */
private $defaultTtl; private ?int $defaultTtl;
/** /**
* PredisAdapter constructor. * PredisAdapter constructor.
@@ -38,7 +38,7 @@ final class PredisAdapter implements CacheAdapterInterface
* @param string $key * @param string $key
* @return mixed * @return mixed
*/ */
public function get(string $key) public function get(string $key): mixed
{ {
return $this->serializer->deserialize($this->client->get($key)); return $this->serializer->deserialize($this->client->get($key));
} }
@@ -59,18 +59,18 @@ final class PredisAdapter implements CacheAdapterInterface
/** /**
* @param string $key * @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 * @param string $key
* @return mixed * @return array
*/ */
public function keys($key = '*') public function keys($key = '*'): array
{ {
return $this->client->keys($key); return $this->client->keys($key);
} }

View File

@@ -4,6 +4,10 @@ namespace IQParts\Cache\Serializer;
final class JsonSerializer implements SerializerInterface final class JsonSerializer implements SerializerInterface
{ {
/**
* @param $input
* @return string
*/
public function serialize($input): string public function serialize($input): string
{ {
if (\is_array($input)) { if (\is_array($input)) {
@@ -13,7 +17,11 @@ final class JsonSerializer implements SerializerInterface
return $input; return $input;
} }
public function deserialize($string) /**
* @param $string
* @return mixed
*/
public function deserialize($string): mixed
{ {
return json_decode($string, true) ?? $string; return json_decode($string, true) ?? $string;
} }

View File

@@ -5,12 +5,12 @@ namespace IQParts\Cache\Serializer;
final class NoSerializer implements SerializerInterface final class NoSerializer implements SerializerInterface
{ {
public function serialize($input) public function serialize($input): string
{ {
return $input; return $input;
} }
public function deserialize($string) public function deserialize($string): mixed
{ {
return $string; return $string;
} }

View File

@@ -4,7 +4,15 @@ namespace IQParts\Cache\Serializer;
interface SerializerInterface 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;
} }

View File

@@ -121,7 +121,7 @@ final class FilesystemAdapterTest extends AbstractTestCase
$adapter->delete('a/b'); $adapter->delete('a/b');
} }
protected function tearDown() protected function tearDown(): void
{ {
parent::tearDown(); parent::tearDown();
$files = new RecursiveIteratorIterator( $files = new RecursiveIteratorIterator(

View File

@@ -15,7 +15,7 @@ final class NamespaceAdapterTest extends AbstractTestCase
*/ */
private $adapter; private $adapter;
public function setUp() public function setUp(): void
{ {
$this->adapter = new MemoryAdapter(); $this->adapter = new MemoryAdapter();
} }

View File

@@ -13,9 +13,7 @@ final class JsonSerializerTest extends AbstractTestCase
$string = 'myString'; $string = 'myString';
$array = [0 => '1', 1 => '2', 2 => '3']; $array = [0 => '1', 1 => '2', 2 => '3'];
$this->assertInternalType('string', $serializer->serialize($array));
$this->assertEquals($string, $serializer->serialize($string)); $this->assertEquals($string, $serializer->serialize($string));
$this->assertEquals($array, $serializer->deserialize($serializer->serialize($array))); $this->assertEquals($array, $serializer->deserialize($serializer->serialize($array)));
} }