Fix a bunch of phpstan issues
All checks were successful
Gitea - aviat/banker/pipeline/head This commit looks good

This commit is contained in:
Timothy Warren 2021-02-19 12:18:38 -05:00
parent 0f874ab0f9
commit 3362d66663
7 changed files with 45 additions and 21 deletions

View File

@ -17,6 +17,7 @@ namespace Aviat\Banker\Driver;
use Aviat\Banker\LoggerTrait; use Aviat\Banker\LoggerTrait;
use Aviat\Banker\KeyValidateTrait; use Aviat\Banker\KeyValidateTrait;
use DateInterval;
use Psr\Log\LoggerAwareInterface; use Psr\Log\LoggerAwareInterface;
/** /**
@ -66,10 +67,10 @@ abstract class AbstractDriver implements DriverInterface, LoggerAwareInterface {
* Set multiple cache values * Set multiple cache values
* *
* @param array $items * @param array $items
* @param int|null $expires * @param DateInterval|int|null $expires
* @return bool * @return bool
*/ */
public function setMultiple(array $items, ?int $expires = NULL): bool public function setMultiple(array $items, DateInterval|int|null $expires = NULL): bool
{ {
$this->validateKeys($items, TRUE); $this->validateKeys($items, TRUE);

View File

@ -98,18 +98,16 @@ class ApcuDriver extends AbstractDriver {
* Set multiple cache values * Set multiple cache values
* *
* @param array $items * @param array $items
* @param int|null $expires * @param DateInterval|int|null $expires
* @return bool * @return bool
*/ */
public function setMultiple(array $items, ?int $expires = NULL): bool public function setMultiple(array $items, DateInterval|int|null $expires = NULL): bool
{ {
$this->validateKeys($items, TRUE); $this->validateKeys($items, TRUE);
$ttl = $this->getTTLFromExpiration((int)$expires);
$errorKeys = ($expires === NULL) $errorKeys = ($expires === NULL)
? apcu_store($items) ? apcu_store($items)
: apcu_store($items, NULL, $ttl); : apcu_store($items, NULL, $this->getTTLFromExpiration($expires));
return empty($errorKeys); return empty($errorKeys);
} }
@ -173,11 +171,16 @@ class ApcuDriver extends AbstractDriver {
/** /**
* Convert expiration date argument into TTL argument * Convert expiration date argument into TTL argument
* *
* @param int|null $expires * @param DateInterval|int|null $expires
* @return int * @return int
*/ */
protected function getTTLFromExpiration(?int $expires): int protected function getTTLFromExpiration(DateInterval|int|null $expires): int
{ {
if ($expires instanceof DateInterval)
{
return $expires->s;
}
$ttl = (int)$expires - time(); $ttl = (int)$expires - time();
return ($ttl < 0) ? 0 : $ttl; return ($ttl < 0) ? 0 : $ttl;

View File

@ -130,6 +130,11 @@ class MemcachedDriver extends AbstractDriver {
{ {
$this->validateKey($key); $this->validateKey($key);
if ($expires instanceof DateInterval)
{
$expires = time() + $expires->s;
}
return ($expires === NULL) return ($expires === NULL)
? $this->conn->set($key, $value) ? $this->conn->set($key, $value)
: $this->conn->set($key, $value, $expires); : $this->conn->set($key, $value, $expires);
@ -139,13 +144,18 @@ class MemcachedDriver extends AbstractDriver {
* Set multiple cache values * Set multiple cache values
* *
* @param array $items * @param array $items
* @param int|null $expires * @param DateInterval|int|null $expires
* @return bool * @return bool
*/ */
public function setMultiple(array $items, ?int $expires = NULL): bool public function setMultiple(array $items, DateInterval|int|null $expires = NULL): bool
{ {
$this->validateKeys($items, TRUE); $this->validateKeys($items, TRUE);
if ($expires instanceof DateInterval)
{
$expires = $expires->s;
}
return ($expires === NULL) return ($expires === NULL)
? $this->conn->setMulti($items) ? $this->conn->setMulti($items)
: $this->conn->setMulti($items, $expires); : $this->conn->setMulti($items, $expires);
@ -174,6 +184,7 @@ class MemcachedDriver extends AbstractDriver {
$deleted = $this->conn->deleteMulti($keys); $deleted = $this->conn->deleteMulti($keys);
// @phpstan-ignore-next-line
if (is_array($deleted)) if (is_array($deleted))
{ {
foreach ($deleted as $key => $status) foreach ($deleted as $key => $status)

View File

@ -79,6 +79,11 @@ class RedisDriver extends AbstractDriver {
public function get(string $key): mixed public function get(string $key): mixed
{ {
$raw = $this->conn->get($key); $raw = $this->conn->get($key);
if ($raw === NULL)
{
return null;
}
return unserialize($raw); return unserialize($raw);
} }
@ -87,7 +92,7 @@ class RedisDriver extends AbstractDriver {
* *
* @param string $key * @param string $key
* @param mixed $value * @param mixed $value
* @param int|null $expires * @param DateInterval|int|null $expires
* @return bool * @return bool
*/ */
public function set(string $key, mixed $value, DateInterval|int|null $expires = NULL): bool public function set(string $key, mixed $value, DateInterval|int|null $expires = NULL): bool

View File

@ -51,9 +51,9 @@ class ItemCollection extends ArrayIterator implements JsonSerializable {
/** /**
* Specify what data to serialize when using `json_encode` * Specify what data to serialize when using `json_encode`
* *
* @return mixed - The full set of data to be serialized * @return array - The full set of data to be serialized
*/ */
public function jsonSerialize() public function jsonSerialize(): array
{ {
return $this->items; return $this->items;
} }

View File

@ -213,10 +213,15 @@ final class Pool implements CacheItemPoolInterface, LoggerAwareInterface {
* True if the item was successfully persisted. False if there was an error. * True if the item was successfully persisted. False if there was an error.
*/ */
public function save(CacheItemInterface $item): bool public function save(CacheItemInterface $item): bool
{
if (method_exists($item, 'save'))
{ {
return $item->save(); return $item->save();
} }
return FALSE;
}
/** /**
* Sets a cache item to be persisted later. * Sets a cache item to be persisted later.
* *

View File

@ -15,8 +15,7 @@
*/ */
namespace Aviat\Banker; namespace Aviat\Banker;
use Aviat\Banker\Driver\DriverInterface; use Aviat\Banker\Driver\AbstractDriver;
use Aviat\Banker\Exception\InvalidArgumentException;
/** /**
* Private trait for shared driver-related functionality * Private trait for shared driver-related functionality
@ -27,17 +26,17 @@ trait _Driver {
/** /**
* Driver class for handling the chosen caching backend * Driver class for handling the chosen caching backend
* *
* @var DriverInterface * @var AbstractDriver
*/ */
private DriverInterface $driver; private AbstractDriver $driver;
/** /**
* Instantiate the appropriate cache backend based on the config * Instantiate the appropriate cache backend based on the config
* *
* @param array $driverConfig * @param array $driverConfig
* @return DriverInterface * @return AbstractDriver
*/ */
protected function loadDriver(array $driverConfig = []): DriverInterface protected function loadDriver(array $driverConfig = []): AbstractDriver
{ {
$driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null')); $driver = ucfirst(strtolower($driverConfig['driver'] ?? 'null'));
$class = __NAMESPACE__ . "\\Driver\\${driver}Driver"; $class = __NAMESPACE__ . "\\Driver\\${driver}Driver";