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/
tmp/
.vs/
.vscode/
.vscode/
.phpunit*

View File

@@ -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": {

View File

@@ -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

View File

@@ -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) {

View File

@@ -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) {

View File

@@ -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);
}
}

View File

@@ -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 [];
}

View File

@@ -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);
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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;
}

View File

@@ -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(

View File

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

View File

@@ -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)));
}