Properly use PHP argument types instead of the silly Exception type juggling

This commit is contained in:
Timothy Warren 2021-12-02 14:26:59 -05:00
parent f3b958d17f
commit 0f46dcb2ee
2 changed files with 47 additions and 204 deletions

View File

@ -15,11 +15,9 @@
*/
namespace Aviat\Banker;
use Aviat\Banker\Exception\InvalidArgumentException;
use DateInterval;
use Psr\Log\{LoggerInterface, LoggerAwareInterface};
use Psr\SimpleCache;
use TypeError;
/**
* Implements PSR-16 (SimpleCache)
@ -65,16 +63,11 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function get($key, $default = null): mixed
public function get(string $key, mixed $default = null): mixed
{
try
{
return $this->_get($key, $default);
}
catch (TypeError $e)
{
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
}
$this->validateKey($key);
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
}
/**
@ -91,16 +84,11 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function set($key, mixed $value, $ttl = null): bool
public function set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
try
{
return $this->_set($key, $value, $ttl);
}
catch (TypeError $e)
{
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
}
$this->validateKey($key);
return $this->driver->set($key, $value, $ttl);
}
/**
@ -113,16 +101,11 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function delete($key): bool
public function delete(string $key): bool
{
try
{
return $this->_delete($key);
}
catch (TypeError $e)
{
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
}
$this->validateKey($key);
return $this->driver->delete($key);
}
/**
@ -137,131 +120,7 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
* MUST be thrown if $keys is neither an array nor a Traversable,
* or if any of the $keys are not a legal value.
*/
public function getMultiple($keys, mixed $default = null): iterable
{
try
{
return $this->_getMultiple($keys, $default);
}
catch (TypeError $e)
{
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
}
}
/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*
* @param iterable $values A list of key => value pairs for a multiple-set operation.
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if $values is neither an array nor a Traversable,
* or if any of the $values are not a legal value.
*/
public function setMultiple($values, $ttl = null): bool
{
try
{
return $this->_setMultiple($values, $ttl);
}
catch (TypeError $e)
{
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
}
}
/**
* Deletes multiple cache items in a single operation.
*
* @param iterable $keys A list of string-based keys to be deleted.
*
* @return bool True if the items were successfully removed. False if there was an error.
*
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if $keys is neither an array nor a Traversable,
* or if any of the $keys are not a legal value.
*/
public function deleteMultiple($keys): bool
{
try
{
return $this->_deleteMultiple($keys);
}
catch (TypeError $e)
{
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
}
}
/**
* Determines whether an item is present in the cache.
*
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
* and not to be used within your live applications operations for get/set, as this method
* is subject to a race condition where your has() will return true and immediately after,
* another script can remove it making the state of your app out of date.
*
* @param string $key The cache item key.
*
* @return bool
*
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
public function has($key): bool
{
try
{
return $this->_has($key);
}
catch (TypeError $e)
{
throw new InvalidArgumentException($e->getMessage(), $e->getCode());
}
}
/**
* Fetches a value from the cache.
* @throws Exception\InvalidArgumentException
*/
protected function _get(string $key, mixed $default = null): mixed
{
$this->validateKey($key);
return ($this->driver->exists($key)) ? $this->driver->get($key) : $default;
}
/**
* Persists data in the cache, uniquely referenced by a key with an optional expiration TTL time.
* @throws Exception\InvalidArgumentException
*/
protected function _set(string $key, mixed $value, null|int|DateInterval $ttl = null): bool
{
$this->validateKey($key);
return $this->driver->set($key, $value, $ttl);
}
/**
* Delete an item from the cache by its unique key.
* @throws Exception\InvalidArgumentException
*/
protected function _delete(string $key): bool
{
$this->validateKey($key);
return $this->driver->delete($key);
}
/**
* Obtains multiple cache items by their unique keys.
*/
protected function _getMultiple(iterable $keys, mixed $default = null): iterable
public function getMultiple(iterable $keys, mixed $default = null): iterable
{
$keys = (array)$keys;
$this->validateKeys($keys);
@ -286,8 +145,19 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
/**
* Persists a set of key => value pairs in the cache, with an optional TTL.
*
* @param iterable $values A list of key => value pairs for a multiple-set operation.
* @param null|int|DateInterval $ttl Optional. The TTL value of this item. If no value is sent and
* the driver supports TTL then the library may set a default value
* for it or let the driver take care of that.
*
* @return bool True on success and false on failure.
*
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if $values is neither an array nor a Traversable,
* or if any of the $values are not a legal value.
*/
protected function _setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
public function setMultiple(iterable $values, null|int|DateInterval $ttl = null): bool
{
$this->validateKeys($values, TRUE);
@ -298,8 +168,16 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
/**
* Deletes multiple cache items in a single operation.
*
* @param iterable $keys A list of string-based keys to be deleted.
*
* @return bool True if the items were successfully removed. False if there was an error.
*
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if $keys is neither an array nor a Traversable,
* or if any of the $keys are not a legal value.
*/
protected function _deleteMultiple(iterable $keys): bool
public function deleteMultiple(iterable $keys): bool
{
$this->validateKeys($keys);
@ -308,9 +186,20 @@ class Teller implements LoggerAwareInterface, SimpleCache\CacheInterface {
/**
* Determines whether an item is present in the cache.
* @throws Exception\InvalidArgumentException
*
* NOTE: It is recommended that has() is only to be used for cache warming type purposes
* and not to be used within your live applications operations for get/set, as this method
* is subject to a race condition where your has() will return true and immediately after,
* another script can remove it making the state of your app out of date.
*
* @param string $key The cache item key.
*
* @return bool
*
* @throws SimpleCache\InvalidArgumentException
* MUST be thrown if the $key string is not a legal value.
*/
protected function _has(string $key): bool
public function has(string $key): bool
{
$this->validateKey($key);

View File

@ -15,7 +15,6 @@
*/
namespace Aviat\Banker\Tests;
use Aviat\Banker\Pool;
use Aviat\Banker\Teller;
use Aviat\Banker\Exception\InvalidArgumentException;
use Monolog\Handler\SyslogHandler;
@ -210,51 +209,6 @@ class TellerTest extends TestCase {
array_walk($hasKeys, fn ($key) => $this->assertTrue($this->teller->has($key)));
}
public function testBadKeyType(): void
{
$this->expectException(InvalidArgumentException::class);
$this->teller->get(546567);
}
public function testBadKeyTypeSet(): void
{
$this->expectException(InvalidArgumentException::class);
$this->teller->set(546567, 'foo');
}
public function testBadKeyTypeDelete(): void
{
$this->expectException(InvalidArgumentException::class);
$this->teller->delete(546567);
}
public function testBadKeyTypeHas(): void
{
$this->expectException(InvalidArgumentException::class);
$this->teller->has(546567);
}
public function testBadKeysType (): void
{
$this->expectException(InvalidArgumentException::class);
$keys = (object)[];
$this->teller->getMultiple($keys);
}
public function testBadKeysTypeSet (): void
{
$this->expectException(InvalidArgumentException::class);
$keys = (object)[];
$this->teller->setMultiple($keys);
}
public function testBadKeysTypeDelete (): void
{
$this->expectException(InvalidArgumentException::class);
$keys = (object)[];
$this->teller->deleteMultiple($keys);
}
/**
* @dataProvider keyValidationTests
* @param string $key