Skip to content

Commit

Permalink
Merge pull request #239 from samsonasik/apply-php80
Browse files Browse the repository at this point in the history
Apply PHP 8.0 Syntax and constructor promotion
  • Loading branch information
Ocramius committed Nov 8, 2022
2 parents 3c8cfd0 + 81980ff commit b756eb5
Show file tree
Hide file tree
Showing 29 changed files with 96 additions and 221 deletions.
8 changes: 3 additions & 5 deletions src/Pattern/AbstractPattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@

namespace Laminas\Cache\Pattern;

use Laminas\Cache\Pattern\PatternOptions;

abstract class AbstractPattern implements PatternInterface
{
/** @var PatternOptions|null */
protected $options;

public function __construct(?PatternOptions $options = null)
public function __construct(protected ?PatternOptions $options = null)
{
$this->options = $options;
}

/**
Expand Down
6 changes: 1 addition & 5 deletions src/Pattern/AbstractStorageCapablePattern.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,9 @@

abstract class AbstractStorageCapablePattern extends AbstractPattern implements StorageCapableInterface
{
/** @var StorageInterface */
protected $storage;

public function __construct(StorageInterface $storage, ?PatternOptions $options = null)
public function __construct(protected StorageInterface $storage, ?PatternOptions $options = null)
{
parent::__construct($options);
$this->storage = $storage;
}

public function getStorage(): StorageInterface
Expand Down
1 change: 0 additions & 1 deletion src/Pattern/CallbackCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,6 @@ protected function generateCallbackKey($callback, array $args)
/**
* Generate a unique key of the argument part.
*
* @param array $args
* @throws Exception\RuntimeException
* @return string
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Pattern/CaptureCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@
use function ob_implicit_flush;
use function ob_start;
use function rtrim;
use function str_ends_with;
use function str_replace;
use function substr;
use function umask;
use function unlink;

Expand Down Expand Up @@ -218,7 +218,7 @@ protected function detectPageId()
*/
protected function pageId2Filename($pageId)
{
if (substr($pageId, -1) === '/') {
if (str_ends_with($pageId, '/')) {
return $this->getOptions()->getIndexFilename();
}

Expand All @@ -233,7 +233,7 @@ protected function pageId2Filename($pageId)
*/
protected function pageId2Path($pageId)
{
if (substr($pageId, -1) === '/') {
if (str_ends_with($pageId, '/')) {
$path = rtrim($pageId, '/');
} else {
$path = dirname($pageId);
Expand Down
12 changes: 5 additions & 7 deletions src/Pattern/ObjectCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laminas\Cache\Pattern;

use Laminas\Cache\Exception;
use Stringable;

use function array_shift;
use function array_unshift;
Expand All @@ -13,7 +14,7 @@
use function property_exists;
use function strtolower;

class ObjectCache extends CallbackCache
class ObjectCache extends CallbackCache implements Stringable
{
/**
* @return ObjectCache
Expand Down Expand Up @@ -207,10 +208,9 @@ public function __call($method, array $args)
* @phpcs:disable Squiz.Commenting.FunctionComment.InvalidReturnVoid
*
* @param string $name
* @param mixed $value
* @return void
*/
public function __set($name, $value)
public function __set($name, mixed $value)
{
return $this->call('__set', [$name, $value]);
}
Expand Down Expand Up @@ -274,12 +274,10 @@ public function __unset($name)
* Handle casting to string
*
* @see http://php.net/manual/language.oop5.magic.php#language.oop5.magic.tostring
*
* @return string
*/
public function __toString()
public function __toString(): string
{
return $this->call('__toString');
return (string) $this->call('__toString');
}

/**
Expand Down
9 changes: 2 additions & 7 deletions src/Pattern/PatternOptions.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
use function array_map;
use function array_unique;
use function array_values;
use function get_class;
use function gettype;
use function is_dir;
use function is_object;
Expand Down Expand Up @@ -398,11 +397,10 @@ public function getIndexFilename()
/**
* Set object to cache
*
* @param mixed $object
* @throws Exception\InvalidArgumentException
* @return PatternOptions Provides a fluent interface
*/
public function setObject($object)
public function setObject(mixed $object)
{
if (! is_object($object)) {
throw new Exception\InvalidArgumentException(
Expand Down Expand Up @@ -454,7 +452,6 @@ public function getObjectCacheMagicProperties()
/**
* Set list of object methods for which to cache return values
*
* @param array $objectCacheMethods
* @return PatternOptions Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -506,15 +503,14 @@ public function setObjectKey($objectKey)
public function getObjectKey()
{
if ($this->objectKey === null) {
return get_class($this->getObject());
return $this->getObject()::class;
}
return $this->objectKey;
}

/**
* Set list of object methods for which NOT to cache return values
*
* @param array $objectNonCacheMethods
* @return PatternOptions Provides a fluent interface
* @throws Exception\InvalidArgumentException
*/
Expand Down Expand Up @@ -583,7 +579,6 @@ public function getPublicDir()
* Recursively apply strtolower on all values of an array, and return as a
* list of unique values
*
* @param array $array
* @return array
*/
protected function recursiveStrtolower(array $array)
Expand Down
25 changes: 9 additions & 16 deletions src/Psr/CacheItemPool/CacheItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,36 +20,29 @@
*/
final class CacheItem implements CacheItemInterface
{
/**
* Cache key
*/
private string $key;

/**
* Cache value
*
* @var mixed|null
*/
private $value;

/**
* True if the cache item lookup resulted in a cache hit or if they item is deferred or successfully saved
*/
private bool $isHit;

/**
* Timestamp item will expire at if expiresAt() called, null otherwise
*/
private ?int $expiration = null;

private ClockInterface $clock;

/**
* @param mixed $value
*/
public function __construct(string $key, $value, bool $isHit, ?ClockInterface $clock = null)
{
$this->key = $key;
public function __construct(
private string $key,
mixed $value,
/**
* True if the cache item lookup resulted in a cache hit or if they item is deferred or successfully saved
*/
private bool $isHit,
?ClockInterface $clock = null
) {
$this->value = $isHit ? $value : null;
$this->isHit = $isHit;
$clock ??= new class implements ClockInterface
Expand Down
15 changes: 7 additions & 8 deletions src/Psr/CacheItemPool/CacheItemPoolDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ public function getItem($key)
$value = $this->storage->getItem($key, $isHit);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
// ignore
}

Expand Down Expand Up @@ -127,7 +127,7 @@ public function getItems(array $keys = [])
$cacheItems = $this->storage->getItems($keys);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
$cacheItems = [];
}

Expand Down Expand Up @@ -161,7 +161,7 @@ public function hasItem($key)
return $this->storage->hasItem($key);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
return false;
}
}
Expand All @@ -184,7 +184,7 @@ public function clear()
} else {
$cleared = $this->storage->flush();
}
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
$cleared = false;
}

Expand Down Expand Up @@ -214,7 +214,7 @@ public function deleteItems(array $keys)
$result = $this->storage->removeItems($keys);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
return false;
}

Expand Down Expand Up @@ -362,10 +362,9 @@ private function hasDeferredItem($key)
/**
* Throws exception if given key is invalid
*
* @param mixed $key
* @throws InvalidArgumentException
*/
private function validateKey($key)
private function validateKey(mixed $key)
{
if (! is_string($key) || preg_match('#[{}()/\\\\@:]#', $key)) {
throw new InvalidArgumentException(sprintf(
Expand Down Expand Up @@ -429,7 +428,7 @@ private function saveMultipleItems(array $items, ?int $itemTtl): array
$notSavedKeys = $this->storage->setItems($keyValuePair);
} catch (Exception\InvalidArgumentException $e) {
throw new InvalidArgumentException($e->getMessage(), $e->getCode(), $e);
} catch (Exception\ExceptionInterface $e) {
} catch (Exception\ExceptionInterface) {
$notSavedKeys = array_keys($keyValuePair);
} finally {
$options->setTtl($ttl);
Expand Down
14 changes: 6 additions & 8 deletions src/Psr/SimpleCache/SimpleCacheDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@
use Traversable;

use function array_keys;
use function get_debug_type;
use function gettype;
use function is_array;
use function is_int;
use function is_object;
use function is_string;
use function preg_match;
use function preg_quote;
Expand Down Expand Up @@ -139,7 +139,7 @@ public function delete($key)

try {
return null !== $this->storage->removeItem($key);
} catch (Throwable $e) {
} catch (Throwable) {
return false;
}
}
Expand Down Expand Up @@ -267,7 +267,7 @@ public function deleteMultiple($keys)

try {
$result = $this->storage->removeItems($keys);
} catch (Throwable $e) {
} catch (Throwable) {
return false;
}

Expand Down Expand Up @@ -399,13 +399,12 @@ private function convertTtlToInteger($ttl)
// All others are invalid
throw new SimpleCacheInvalidArgumentException(sprintf(
'Invalid TTL "%s" provided; must be null, an integer, or a %s instance',
is_object($ttl) ? $ttl::class : var_export($ttl, true),
get_debug_type($ttl),
DateInterval::class
));
}

/**
* @param iterable $keys
* @psalm-return list<string|int>
* @throws SimpleCacheInvalidArgumentException For invalid $iterable values.
*/
Expand All @@ -416,7 +415,7 @@ private function convertIterableKeysToList(iterable $keys): array
if (! is_string($key) && ! is_int($key)) {
throw new SimpleCacheInvalidArgumentException(sprintf(
'Invalid key detected of type "%s"; must be a scalar',
is_object($key) ? $key::class : gettype($key)
get_debug_type($key)
));
}

Expand All @@ -428,7 +427,6 @@ private function convertIterableKeysToList(iterable $keys): array
}

/**
* @param iterable $values
* @psalm-return array<int|string,mixed>
*/
private function convertIterableToKeyValueMap(iterable $values): array
Expand All @@ -438,7 +436,7 @@ private function convertIterableToKeyValueMap(iterable $values): array
if (! is_string($key) && ! is_int($key)) {
throw new SimpleCacheInvalidArgumentException(sprintf(
'Invalid key detected of type "%s"; must be a scalar',
is_object($key) ? $key::class : gettype($key)
get_debug_type($key)
));
}

Expand Down
12 changes: 4 additions & 8 deletions src/Service/StorageAdapterFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,10 @@ final class StorageAdapterFactory implements StorageAdapterFactoryInterface
{
public const DEFAULT_PLUGIN_PRIORITY = 1;

private PluginManagerInterface $adapters;

private StoragePluginFactoryInterface $pluginFactory;

public function __construct(PluginManagerInterface $adapters, StoragePluginFactoryInterface $pluginFactory)
{
$this->adapters = $adapters;
$this->pluginFactory = $pluginFactory;
public function __construct(
private PluginManagerInterface $adapters,
private StoragePluginFactoryInterface $pluginFactory
) {
}

public function createFromArrayConfiguration(array $configuration): StorageInterface
Expand Down
5 changes: 1 addition & 4 deletions src/Service/StoragePluginFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,8 @@

final class StoragePluginFactory implements StoragePluginFactoryInterface
{
private PluginManagerInterface $plugins;

public function __construct(PluginManagerInterface $plugins)
public function __construct(private PluginManagerInterface $plugins)
{
$this->plugins = $plugins;
}

public function createFromArrayConfiguration(array $configuration): PluginInterface
Expand Down

0 comments on commit b756eb5

Please sign in to comment.