Browse Source

Add native type hints for Cache

Corey Taylor 4 years ago
parent
commit
e27355b353

+ 1 - 1
phpstan-baseline.neon

@@ -162,7 +162,7 @@ parameters:
 
 		-
 			message: "#^PHPDoc tag @return with type mixed is not subtype of native type object\\.$#"
-			count: 2
+			count: 3
 			path: src/Core/ObjectRegistry.php
 
 		-

+ 6 - 6
src/Cache/Cache.php

@@ -239,7 +239,7 @@ class Cache
      * @param string $config Optional string configuration name to write to. Defaults to 'default'
      * @return bool True if the data was successfully cached, false on failure
      */
-    public static function write(string $key, $value, string $config = 'default'): bool
+    public static function write(string $key, mixed $value, string $config = 'default'): bool
     {
         if (is_resource($value)) {
             return false;
@@ -311,7 +311,7 @@ class Cache
      * @return mixed The cached data, or null if the data doesn't exist, has expired,
      *  or if there was an error fetching it.
      */
-    public static function read(string $key, string $config = 'default')
+    public static function read(string $key, string $config = 'default'): mixed
     {
         return static::pool($config)->get($key);
     }
@@ -354,7 +354,7 @@ class Cache
      *    or if there was an error fetching it.
      * @throws \Cake\Cache\InvalidArgumentException When offset < 0
      */
-    public static function increment(string $key, int $offset = 1, string $config = 'default')
+    public static function increment(string $key, int $offset = 1, string $config = 'default'): int|false
     {
         if ($offset < 0) {
             throw new InvalidArgumentException('Offset cannot be less than 0.');
@@ -373,7 +373,7 @@ class Cache
      *   or if there was an error fetching it
      * @throws \Cake\Cache\InvalidArgumentException when offset < 0
      */
-    public static function decrement(string $key, int $offset = 1, string $config = 'default')
+    public static function decrement(string $key, int $offset = 1, string $config = 'default'): int|false
     {
         if ($offset < 0) {
             throw new InvalidArgumentException('Offset cannot be less than 0.');
@@ -566,7 +566,7 @@ class Cache
      *   missing/expired, or an error. If the key is not found: boolean of the
      *   success of the write
      */
-    public static function remember(string $key, callable $callable, string $config = 'default')
+    public static function remember(string $key, callable $callable, string $config = 'default'): mixed
     {
         $existing = self::read($key, $config);
         if ($existing !== null) {
@@ -601,7 +601,7 @@ class Cache
      * @return bool True if the data was successfully cached, false on failure.
      *   Or if the key existed already.
      */
-    public static function add(string $key, $value, string $config = 'default'): bool
+    public static function add(string $key, mixed $value, string $config = 'default'): bool
     {
         if (is_resource($value)) {
             return false;

+ 6 - 9
src/Cache/CacheEngine.php

@@ -256,7 +256,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @param int $offset How much to add
      * @return int|false New incremented value, false otherwise
      */
-    abstract public function increment(string $key, int $offset = 1);
+    abstract public function increment(string $key, int $offset = 1): int|false;
 
     /**
      * Decrement a number under the key and return decremented value
@@ -265,7 +265,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @param int $offset How much to subtract
      * @return int|false New incremented value, false otherwise
      */
-    abstract public function decrement(string $key, int $offset = 1);
+    abstract public function decrement(string $key, int $offset = 1): int|false;
 
     /**
      * Delete a key from the cache
@@ -292,7 +292,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @param mixed $value Data to be cached.
      * @return bool True if the data was successfully cached, false on failure.
      */
-    public function add(string $key, $value): bool
+    public function add(string $key, mixed $value): bool
     {
         $cachedValue = $this->get($key);
         if ($cachedValue === null) {
@@ -334,7 +334,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      * @return string Prefixed key with potentially unsafe characters replaced.
      * @throws \Cake\Cache\InvalidArgumentException If key's value is invalid.
      */
-    protected function _key($key): string
+    protected function _key(string $key): string
     {
         $this->ensureValidKey($key);
 
@@ -370,7 +370,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
      *   driver's default duration will be used.
      * @return int
      */
-    protected function duration($ttl): int
+    protected function duration(DateInterval|int|null $ttl): int
     {
         if ($ttl === null) {
             return $this->_config['duration'];
@@ -378,10 +378,7 @@ abstract class CacheEngine implements CacheInterface, CacheEngineInterface
         if (is_int($ttl)) {
             return $ttl;
         }
-        if ($ttl instanceof DateInterval) {
-            return (int)$ttl->format('%s');
-        }
 
-        throw new InvalidArgumentException('TTL values must be one of null, int, \DateInterval');
+        return (int)$ttl->format('%s');
     }
 }

+ 3 - 3
src/Cache/CacheEngineInterface.php

@@ -35,7 +35,7 @@ interface CacheEngineInterface
      * @return bool True if the data was successfully cached, false on failure.
      *   Or if the key existed already.
      */
-    public function add(string $key, $value): bool;
+    public function add(string $key, mixed $value): bool;
 
     /**
      * Increment a number under the key and return incremented value
@@ -44,7 +44,7 @@ interface CacheEngineInterface
      * @param int $offset How much to add
      * @return int|false New incremented value, false otherwise
      */
-    public function increment(string $key, int $offset = 1);
+    public function increment(string $key, int $offset = 1): int|false;
 
     /**
      * Decrement a number under the key and return decremented value
@@ -53,7 +53,7 @@ interface CacheEngineInterface
      * @param int $offset How much to subtract
      * @return int|false New incremented value, false otherwise
      */
-    public function decrement(string $key, int $offset = 1);
+    public function decrement(string $key, int $offset = 1): int|false;
 
     /**
      * Clear all values belonging to the named group.

+ 4 - 4
src/Cache/Engine/ApcuEngine.php

@@ -78,7 +78,7 @@ class ApcuEngine extends CacheEngine
      *   has expired, or if there was an error fetching it
      * @link https://secure.php.net/manual/en/function.apcu-fetch.php
      */
-    public function get($key, $default = null)
+    public function get($key, $default = null): mixed
     {
         $value = apcu_fetch($this->_key($key), $success);
         if ($success === false) {
@@ -96,7 +96,7 @@ class ApcuEngine extends CacheEngine
      * @return int|false New incremented value, false otherwise
      * @link https://secure.php.net/manual/en/function.apcu-inc.php
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
         $key = $this->_key($key);
 
@@ -111,7 +111,7 @@ class ApcuEngine extends CacheEngine
      * @return int|false New decremented value, false otherwise
      * @link https://secure.php.net/manual/en/function.apcu-dec.php
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
         $key = $this->_key($key);
 
@@ -170,7 +170,7 @@ class ApcuEngine extends CacheEngine
      * @return bool True if the data was successfully cached, false on failure.
      * @link https://secure.php.net/manual/en/function.apcu-add.php
      */
-    public function add(string $key, $value): bool
+    public function add(string $key, mixed $value): bool
     {
         $key = $this->_key($key);
         $duration = $this->_config['duration'];

+ 2 - 2
src/Cache/Engine/ArrayEngine.php

@@ -92,7 +92,7 @@ class ArrayEngine extends CacheEngine
      * @param int $offset How much to increment
      * @return int|false New incremented value, false otherwise
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
         if ($this->get($key) === null) {
             $this->set($key, 0);
@@ -110,7 +110,7 @@ class ArrayEngine extends CacheEngine
      * @param int $offset How much to subtract
      * @return int|false New decremented value, false otherwise
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
         if ($this->get($key) === null) {
             $this->set($key, 0);

+ 2 - 2
src/Cache/Engine/FileEngine.php

@@ -332,7 +332,7 @@ class FileEngine extends CacheEngine
      * @return int|false
      * @throws \LogicException
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
         throw new LogicException('Files cannot be atomically decremented.');
     }
@@ -345,7 +345,7 @@ class FileEngine extends CacheEngine
      * @return int|false
      * @throws \LogicException
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
         throw new LogicException('Files cannot be atomically incremented.');
     }

+ 4 - 4
src/Cache/Engine/MemcachedEngine.php

@@ -287,7 +287,7 @@ class MemcachedEngine extends CacheEngine
      * @return string|int|bool|null
      * @see https://secure.php.net/manual/en/memcached.getoption.php
      */
-    public function getOption(int $name)
+    public function getOption(int $name): string|int|bool|null
     {
         return $this->_Memcached->getOption($name);
     }
@@ -383,7 +383,7 @@ class MemcachedEngine extends CacheEngine
      * @param int $offset How much to increment
      * @return int|false New incremented value, false otherwise
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
         return $this->_Memcached->increment($this->_key($key), $offset);
     }
@@ -395,7 +395,7 @@ class MemcachedEngine extends CacheEngine
      * @param int $offset How much to subtract
      * @return int|false New decremented value, false otherwise
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
         return $this->_Memcached->decrement($this->_key($key), $offset);
     }
@@ -457,7 +457,7 @@ class MemcachedEngine extends CacheEngine
      * @param mixed $value Data to be cached.
      * @return bool True if the data was successfully cached, false on failure.
      */
-    public function add(string $key, $value): bool
+    public function add(string $key, mixed $value): bool
     {
         $duration = $this->_config['duration'];
         $key = $this->_key($key);

+ 2 - 2
src/Cache/Engine/NullEngine.php

@@ -68,7 +68,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
         return 1;
     }
@@ -76,7 +76,7 @@ class NullEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
         return 0;
     }

+ 5 - 5
src/Cache/Engine/RedisEngine.php

@@ -181,7 +181,7 @@ class RedisEngine extends CacheEngine
      * @param int $offset How much to increment
      * @return int|false New incremented value, false otherwise
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
         $duration = $this->_config['duration'];
         $key = $this->_key($key);
@@ -201,7 +201,7 @@ class RedisEngine extends CacheEngine
      * @param int $offset How much to subtract
      * @return int|false New decremented value, false otherwise
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
         $duration = $this->_config['duration'];
         $key = $this->_key($key);
@@ -265,7 +265,7 @@ class RedisEngine extends CacheEngine
      * @return bool True if the data was successfully cached, false on failure.
      * @link https://github.com/phpredis/phpredis#set
      */
-    public function add(string $key, $value): bool
+    public function add(string $key, mixed $value): bool
     {
         $duration = $this->_config['duration'];
         $key = $this->_key($key);
@@ -322,7 +322,7 @@ class RedisEngine extends CacheEngine
      * @return string
      * @link https://github.com/phpredis/phpredis/issues/81
      */
-    protected function serialize($value): string
+    protected function serialize(mixed $value): string
     {
         if (is_int($value)) {
             return (string)$value;
@@ -337,7 +337,7 @@ class RedisEngine extends CacheEngine
      * @param string $value Value to unserialize.
      * @return mixed
      */
-    protected function unserialize(string $value)
+    protected function unserialize(string $value): mixed
     {
         if (preg_match('/^[-]?\d+$/', $value)) {
             return (int)$value;

+ 2 - 2
tests/test_app/Plugin/TestPlugin/src/Cache/Engine/TestPluginCacheEngine.php

@@ -43,14 +43,14 @@ class TestPluginCacheEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
     }
 
     /**
      * @inheritDoc
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
     }
 

+ 2 - 2
tests/test_app/TestApp/Cache/Engine/TestAppCacheEngine.php

@@ -48,14 +48,14 @@ class TestAppCacheEngine extends CacheEngine
     /**
      * @inheritDoc
      */
-    public function increment(string $key, int $offset = 1)
+    public function increment(string $key, int $offset = 1): int|false
     {
     }
 
     /**
      * @inheritDoc
      */
-    public function decrement(string $key, int $offset = 1)
+    public function decrement(string $key, int $offset = 1): int|false
     {
     }