From df61334bb42789477fc7a8e1f543b64a5d28aab5 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 16 Mar 2023 16:23:41 -0400 Subject: [PATCH] Code tweaks for slightly better test coverage --- src/Driver/ApcuDriver.php | 2 +- src/Driver/RedisDriver.php | 10 ++++------ src/ItemCollection.php | 5 +++-- src/Pool.php | 1 + src/Teller.php | 1 + src/_Driver.php | 8 +------- tests/ItemCollectionTest.php | 2 +- 7 files changed, 12 insertions(+), 17 deletions(-) diff --git a/src/Driver/ApcuDriver.php b/src/Driver/ApcuDriver.php index 320f6ea..ba58084 100644 --- a/src/Driver/ApcuDriver.php +++ b/src/Driver/ApcuDriver.php @@ -76,7 +76,7 @@ class ApcuDriver extends AbstractDriver { $this->validateKeys($keys); $status = FALSE; - return apcu_fetch($keys, $status); + return (array)apcu_fetch($keys, $status); } /** diff --git a/src/Driver/RedisDriver.php b/src/Driver/RedisDriver.php index 589a8df..4e5a788 100644 --- a/src/Driver/RedisDriver.php +++ b/src/Driver/RedisDriver.php @@ -78,13 +78,11 @@ class RedisDriver extends AbstractDriver { */ public function get(string $key): mixed { - $raw = $this->conn->get($key); - if ($raw === NULL) - { - return null; - } + $raw = $this->conn->get($key) ?? ''; + $parsed = @unserialize($raw); + $hasError = is_array(error_get_last()); - return unserialize($raw); + return ($hasError) ? NULL : $parsed; } /** diff --git a/src/ItemCollection.php b/src/ItemCollection.php index 9743774..6780bda 100644 --- a/src/ItemCollection.php +++ b/src/ItemCollection.php @@ -25,6 +25,7 @@ use JsonSerializable; * * @see http://php.net/manual/en/class.arrayiterator.php * @see http://php.net/manual/en/class.jsonserializable.php + * @extends ArrayIterator */ class ItemCollection extends ArrayIterator implements JsonSerializable { @@ -39,7 +40,7 @@ class ItemCollection extends ArrayIterator implements JsonSerializable { * Create the collection object from the raw * CacheItemInterface array * - * @param array $items - array of CacheItemInterface objects + * @param CacheItemInterface[] $items - array of CacheItemInterface objects * @param int $flags - flags */ public function __construct(array $items = [], int $flags = 0) @@ -51,7 +52,7 @@ class ItemCollection extends ArrayIterator implements JsonSerializable { /** * Specify what data to serialize when using `json_encode` * - * @return array - The full set of data to be serialized + * @return CacheItemInterface[] - The full set of data to be serialized */ public function jsonSerialize(): array { diff --git a/src/Pool.php b/src/Pool.php index 2837238..7867ba7 100644 --- a/src/Pool.php +++ b/src/Pool.php @@ -26,6 +26,7 @@ use function is_string; */ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface { use _Driver; + use KeyValidateTrait; use LoggerTrait; /** diff --git a/src/Teller.php b/src/Teller.php index c5cf126..e1b3d4d 100644 --- a/src/Teller.php +++ b/src/Teller.php @@ -24,6 +24,7 @@ use Psr\SimpleCache; */ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface { use _Driver; + use KeyValidateTrait; use LoggerTrait; /** diff --git a/src/_Driver.php b/src/_Driver.php index 6f1c17c..e19cd5d 100644 --- a/src/_Driver.php +++ b/src/_Driver.php @@ -21,20 +21,13 @@ use Aviat\Banker\Driver\AbstractDriver; * Private trait for shared driver-related functionality */ trait _Driver { - use KeyValidateTrait; - /** * Driver class for handling the chosen caching backend - * - * @var AbstractDriver */ private AbstractDriver $driver; /** * Instantiate the appropriate cache backend based on the config - * - * @param array $driverConfig - * @return AbstractDriver */ protected function loadDriver(array $driverConfig = []): AbstractDriver { @@ -44,6 +37,7 @@ trait _Driver { $driverConfig['connection'] = $driverConfig['connection'] ?? []; $driverConfig['options'] = $driverConfig['options'] ?? []; + // @phpstan-ignore-next-line return new $class($driverConfig['connection'], $driverConfig['options']); } } \ No newline at end of file diff --git a/tests/ItemCollectionTest.php b/tests/ItemCollectionTest.php index d8f82c2..9346114 100644 --- a/tests/ItemCollectionTest.php +++ b/tests/ItemCollectionTest.php @@ -20,7 +20,7 @@ use PHPUnit\Framework\TestCase; class ItemCollectionTest extends TestCase { - protected $collection; + protected ItemCollection $collection; public function setUp(): void {