From 96ee41a3ddea70ea89c73b0034ec67858d4e97fd Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Fri, 18 Aug 2017 12:28:13 +0300 Subject: [PATCH] Follow PSR-2 in Grav\Framework --- .../Grav/Framework/Cache/AbstractCache.php | 2 - .../Framework/Cache/Adapter/ChainCache.php | 9 +++- .../src/Grav/Framework/Cache/CacheTrait.php | 45 ++++++++++++++--- .../Framework/Collection/ArrayCollection.php | 16 +++--- .../Framework/Collection/FileCollection.php | 5 +- .../Framework/ContentBlock/ContentBlock.php | 7 +-- .../ContentBlock/ContentBlockInterface.php | 2 +- .../Grav/Framework/ContentBlock/HtmlBlock.php | 21 ++++++-- system/src/Grav/Framework/Object/Object.php | 50 +++++-------------- .../Framework/Object/ObjectCollection.php | 19 ++++--- .../Object/ObjectCollectionInterface.php | 6 +++ .../Object/ObjectCollectionTrait.php | 3 +- .../src/Grav/Framework/Object/ObjectTrait.php | 4 +- .../Page/PageCollectionInterface.php | 3 +- .../Framework/Page/PageHeaderInterface.php | 5 +- .../Page/PageMediaCollectionInterface.php | 3 +- 16 files changed, 119 insertions(+), 81 deletions(-) diff --git a/system/src/Grav/Framework/Cache/AbstractCache.php b/system/src/Grav/Framework/Cache/AbstractCache.php index 1713b754c..90269213c 100644 --- a/system/src/Grav/Framework/Cache/AbstractCache.php +++ b/system/src/Grav/Framework/Cache/AbstractCache.php @@ -8,8 +8,6 @@ namespace Grav\Framework\Cache; -use Grav\Framework\Cache\Exception\InvalidArgumentException; - /** * Cache trait for PSR-16 compatible "Simple Cache" implementation * @package Grav\Framework\Cache diff --git a/system/src/Grav/Framework/Cache/Adapter/ChainCache.php b/system/src/Grav/Framework/Cache/Adapter/ChainCache.php index d211516aa..af0784c95 100644 --- a/system/src/Grav/Framework/Cache/Adapter/ChainCache.php +++ b/system/src/Grav/Framework/Cache/Adapter/ChainCache.php @@ -32,6 +32,7 @@ class ChainCache extends AbstractCache * Chain Cache constructor. * @param array $caches * @param null|int|\DateInterval $defaultLifetime + * @throws \InvalidArgumentException */ public function __construct(array $caches, $defaultLifetime = null) { @@ -43,7 +44,13 @@ class ChainCache extends AbstractCache foreach ($caches as $cache) { if (!$cache instanceof CacheInterface) { - throw new \InvalidArgumentException(sprintf("The class '%s' does not implement the '%s' interface", get_class($cache), CacheInterface::class)); + throw new \InvalidArgumentException( + sprintf( + "The class '%s' does not implement the '%s' interface", + get_class($cache), + CacheInterface::class + ) + ); } } diff --git a/system/src/Grav/Framework/Cache/CacheTrait.php b/system/src/Grav/Framework/Cache/CacheTrait.php index 9d07547fb..8be0165bd 100644 --- a/system/src/Grav/Framework/Cache/CacheTrait.php +++ b/system/src/Grav/Framework/Cache/CacheTrait.php @@ -8,7 +8,6 @@ namespace Grav\Framework\Cache; -use Grav\Framework\Cache\CacheInterface; use Grav\Framework\Cache\Exception\InvalidArgumentException; /** @@ -106,13 +105,19 @@ trait CacheTrait /** * @inheritdoc + * @throws InvalidArgumentException */ public function getMultiple($keys, $default = null) { if ($keys instanceof \Traversable) { $keys = iterator_to_array($keys, false); } elseif (!is_array($keys)) { - throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys))); + throw new InvalidArgumentException( + sprintf( + 'Cache keys must be array or Traversable, "%s" given', + is_object($keys) ? get_class($keys) : gettype($keys) + ) + ); } if (empty($keys)) { @@ -146,7 +151,12 @@ trait CacheTrait if ($values instanceof \Traversable) { $values = iterator_to_array($values, true); } elseif (!is_array($values)) { - throw new InvalidArgumentException(sprintf('Cache values must be array or Traversable, "%s" given', is_object($values) ? get_class($values) : gettype($values))); + throw new InvalidArgumentException( + sprintf( + 'Cache values must be array or Traversable, "%s" given', + is_object($values) ? get_class($values) : gettype($values) + ) + ); } $keys = array_keys($values); @@ -171,7 +181,12 @@ trait CacheTrait if ($keys instanceof \Traversable) { $keys = iterator_to_array($keys, false); } elseif (!is_array($keys)) { - throw new InvalidArgumentException(sprintf('Cache keys must be array or Traversable, "%s" given', is_object($keys) ? get_class($keys) : gettype($keys))); + throw new InvalidArgumentException( + sprintf( + 'Cache keys must be array or Traversable, "%s" given', + is_object($keys) ? get_class($keys) : gettype($keys) + ) + ); } if (empty($keys)) { @@ -242,16 +257,25 @@ trait CacheTrait protected function validateKey($key) { if (!is_string($key)) { - throw new InvalidArgumentException(sprintf('Cache key must be string, "%s" given', is_object($key) ? get_class($key) : gettype($key))); + throw new InvalidArgumentException( + sprintf( + 'Cache key must be string, "%s" given', + is_object($key) ? get_class($key) : gettype($key) + ) + ); } if (!isset($key[0])) { throw new InvalidArgumentException('Cache key length must be greater than zero'); } if (strlen($key) > 64) { - throw new InvalidArgumentException(sprintf('Cache key length must be less than 65 characters, key had %s characters', strlen($key))); + throw new InvalidArgumentException( + sprintf('Cache key length must be less than 65 characters, key had %s characters', strlen($key)) + ); } if (strpbrk($key, '{}()/\@:') !== false) { - throw new InvalidArgumentException(sprintf('Cache key "%s" contains reserved characters {}()/\@:', $key)); + throw new InvalidArgumentException( + sprintf('Cache key "%s" contains reserved characters {}()/\@:', $key) + ); } } @@ -281,6 +305,11 @@ trait CacheTrait $ttl = (int) \DateTime::createFromFormat('U', 0)->add($ttl)->format('U'); } - throw new InvalidArgumentException(sprintf('Expiration date must be an integer, a DateInterval or null, "%s" given', is_object($ttl) ? get_class($ttl) : gettype($ttl))); + throw new InvalidArgumentException( + sprintf( + 'Expiration date must be an integer, a DateInterval or null, "%s" given', + is_object($ttl) ? get_class($ttl) : gettype($ttl) + ) + ); } } diff --git a/system/src/Grav/Framework/Collection/ArrayCollection.php b/system/src/Grav/Framework/Collection/ArrayCollection.php index 917abd6b7..b49423162 100644 --- a/system/src/Grav/Framework/Collection/ArrayCollection.php +++ b/system/src/Grav/Framework/Collection/ArrayCollection.php @@ -24,12 +24,12 @@ class ArrayCollection extends BaseArrayCollection implements CollectionInterface */ public function reverse() { - if (method_exists($this, 'createFrom')) { - return $this->createFrom(array_reverse($this->toArray())); - } else { - // TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4). + // TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4). + if (!method_exists($this, 'createFrom')) { return new static(array_reverse($this->toArray())); } + + return $this->createFrom(array_reverse($this->toArray())); } /** @@ -42,12 +42,12 @@ class ArrayCollection extends BaseArrayCollection implements CollectionInterface $keys = $this->getKeys(); shuffle($keys); - if (method_exists($this, 'createFrom')) { - return $this->createFrom(array_replace(array_flip($keys), $this->toArray())); - } else { - // TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4). + // TODO: remove when PHP 5.6 is minimum (with doctrine/collections v1.4). + if (!method_exists($this, 'createFrom')) { return new static(array_replace(array_flip($keys), $this->toArray())); } + + return $this->createFrom(array_replace(array_flip($keys), $this->toArray())); } /** diff --git a/system/src/Grav/Framework/Collection/FileCollection.php b/system/src/Grav/Framework/Collection/FileCollection.php index da9764448..65e49701f 100644 --- a/system/src/Grav/Framework/Collection/FileCollection.php +++ b/system/src/Grav/Framework/Collection/FileCollection.php @@ -62,7 +62,8 @@ class FileCollection extends AbstractLazyCollection public function __construct($path, $flags = null) { $this->path = $path; - $this->flags = (int) ($flags ?: FileCollection::INCLUDE_FILES | FileCollection::INCLUDE_FOLDERS | FileCollection::RECURSIVE); + $this->flags = (int) ($flags + ?: FileCollection::INCLUDE_FILES | FileCollection::INCLUDE_FOLDERS | FileCollection::RECURSIVE); $this->setIterator(); $this->setFilter(); @@ -268,7 +269,7 @@ class FileCollection extends AbstractLazyCollection protected function createObject($file) { return (object) [ - 'key' => $file->getSubPathName(), + 'key' => $file->getSubPathname(), 'type' => $file->isDir() ? 'folder' : 'file:' . $file->getExtension(), 'url' => method_exists($file, 'getUrl') ? $file->getUrl() : null, 'pathname' => $file->getPathname(), diff --git a/system/src/Grav/Framework/ContentBlock/ContentBlock.php b/system/src/Grav/Framework/ContentBlock/ContentBlock.php index c446c3a1b..5c50937b5 100644 --- a/system/src/Grav/Framework/ContentBlock/ContentBlock.php +++ b/system/src/Grav/Framework/ContentBlock/ContentBlock.php @@ -151,6 +151,7 @@ class ContentBlock implements ContentBlockInterface /** * @param array $serialized + * @throws \RuntimeException */ public function build(array $serialized) { @@ -221,9 +222,9 @@ class ContentBlock implements ContentBlockInterface */ protected function checkVersion(array $serialized) { - $version = isset($serialized['_version']) ? (string) $serialized['_version'] : 1; - if ($version != $this->version) { + $version = isset($serialized['_version']) ? (string) $serialized['_version'] : '1'; + if ($version !== $this->version) { throw new \RuntimeException(sprintf('Unsupported version %s', $version)); } } -} \ No newline at end of file +} diff --git a/system/src/Grav/Framework/ContentBlock/ContentBlockInterface.php b/system/src/Grav/Framework/ContentBlock/ContentBlockInterface.php index 9412ef82f..914f4eae6 100644 --- a/system/src/Grav/Framework/ContentBlock/ContentBlockInterface.php +++ b/system/src/Grav/Framework/ContentBlock/ContentBlockInterface.php @@ -72,4 +72,4 @@ interface ContentBlockInterface extends \Serializable * @return $this */ public function addBlock(ContentBlockInterface $block); -} \ No newline at end of file +} diff --git a/system/src/Grav/Framework/ContentBlock/HtmlBlock.php b/system/src/Grav/Framework/ContentBlock/HtmlBlock.php index c2537da86..477c5e0ae 100644 --- a/system/src/Grav/Framework/ContentBlock/HtmlBlock.php +++ b/system/src/Grav/Framework/ContentBlock/HtmlBlock.php @@ -15,7 +15,6 @@ namespace Grav\Framework\ContentBlock; */ class HtmlBlock extends ContentBlock implements HtmlBlockInterface { - protected $version = 1; protected $frameworks = []; protected $styles = []; protected $scripts = []; @@ -73,7 +72,7 @@ class HtmlBlock extends ContentBlock implements HtmlBlockInterface } /** - * @return array + * @return array[] */ public function toArray() { @@ -97,6 +96,7 @@ class HtmlBlock extends ContentBlock implements HtmlBlockInterface /** * @param array $serialized + * @throws \RuntimeException */ public function build(array $serialized) { @@ -144,7 +144,15 @@ class HtmlBlock extends ContentBlock implements HtmlBlockInterface $href = $element['href']; $type = !empty($element['type']) ? (string) $element['type'] : 'text/css'; $media = !empty($element['media']) ? (string) $element['media'] : null; - unset($element['tag'], $element['id'], $element['rel'], $element['content'], $element['href'], $element['type'], $element['media']); + unset( + $element['tag'], + $element['id'], + $element['rel'], + $element['content'], + $element['href'], + $element['type'], + $element['media'] + ); $this->styles[$location][md5($href) . sha1($href)] = [ ':type' => 'file', @@ -354,10 +362,13 @@ class HtmlBlock extends ContentBlock implements HtmlBlockInterface foreach ($items as &$item) { $item[':order'] = ++$count; } + unset($item); + uasort( $items, function ($a, $b) { - return ($a[':priority'] == $b[':priority']) ? $a[':order'] - $b[':order'] : $a[':priority'] - $b[':priority']; + return ($a[':priority'] === $b[':priority']) + ? $a[':order'] - $b[':order'] : $a[':priority'] - $b[':priority']; } ); } @@ -371,4 +382,4 @@ class HtmlBlock extends ContentBlock implements HtmlBlockInterface $this->sortAssetsInLocation($items); } } -} \ No newline at end of file +} diff --git a/system/src/Grav/Framework/Object/Object.php b/system/src/Grav/Framework/Object/Object.php index fe4f77060..fbbdab919 100644 --- a/system/src/Grav/Framework/Object/Object.php +++ b/system/src/Grav/Framework/Object/Object.php @@ -24,37 +24,6 @@ class Object implements ObjectInterface NestedArrayAccessWithGetters::offsetSet as private parentOffsetSet; } - /** - * Properties of the object. - * @var array - */ - protected $items; - - /** - * @var string - */ - private $key; - - /** - * @param array $elements - * @param string $key - */ - public function __construct(array $elements = [], $key = null) - { - - $this->items = $elements; - $this->key = $key !== null ? $key : $this->getKey(); - - if ($this->key === null) { - throw new \InvalidArgumentException('Object cannot be created without assigning a key'); - } - } - - public function getKey() - { - return $this->key; - } - /** * Checks whether or not an offset exists with a possibility to load the field by $this->offsetLoad_{$offset}(). * @@ -63,7 +32,9 @@ class Object implements ObjectInterface */ public function offsetExists($offset) { - return $this->parentOffsetExists($offset) || method_exists($this, "offsetLoad_{$offset}"); + $methodName = "offsetLoad_{$offset}"; + + return $this->parentOffsetExists($offset) || method_exists($this, $methodName); } /** @@ -74,8 +45,10 @@ class Object implements ObjectInterface */ public function offsetGet($offset) { - if (!$this->parentOffsetExists($offset) && method_exists($this, "offsetLoad_{$offset}")) { - $this->offsetSet($offset, call_user_func([$this, "offsetLoad_{$offset}"])); + $methodName = "offsetLoad_{$offset}"; + + if (!$this->parentOffsetExists($offset) && method_exists($this, $methodName)) { + $this->offsetSet($offset, $this->{$methodName}()); } return $this->parentOffsetGet($offset); @@ -83,15 +56,18 @@ class Object implements ObjectInterface /** - * Assigns a value to the specified offset with a possibility to check or alter the value by $this->offsetPrepare_{$offset}(). + * Assigns a value to the specified offset with a possibility to check or alter the value by + * $this->offsetPrepare_{$offset}(). * * @param mixed $offset The offset to assign the value to. * @param mixed $value The value to set. */ public function offsetSet($offset, $value) { - if (method_exists($this, "offsetPrepare_{$offset}")) { - $value = call_user_func([$this, "offsetPrepare_{$offset}"], $value); + $methodName = "offsetPrepare_{$offset}"; + + if (method_exists($this, $methodName)) { + $value = $this->{$methodName}($value); } $this->parentOffsetSet($offset, $value); diff --git a/system/src/Grav/Framework/Object/ObjectCollection.php b/system/src/Grav/Framework/Object/ObjectCollection.php index 799a0cbe9..81fa198a1 100644 --- a/system/src/Grav/Framework/Object/ObjectCollection.php +++ b/system/src/Grav/Framework/Object/ObjectCollection.php @@ -18,20 +18,27 @@ class ObjectCollection extends ArrayCollection implements ObjectCollectionInterf { use ObjectCollectionTrait; - /** - * @var string - */ - private $key; - /** * @param array $elements * @param string $key + * @throws \InvalidArgumentException */ public function __construct(array $elements = [], $key = null) { parent::__construct($elements); - $this->key = $key !== null ? $key : $this->getKey(); + $this->key = $key ?: $this->getKey() ?: __CLASS__ . '@' . spl_object_hash($this); + } + + /** + * @param string $key + * @return $this + */ + public function setKey($key) + { + $this->key = $key; + + return $this; } /** diff --git a/system/src/Grav/Framework/Object/ObjectCollectionInterface.php b/system/src/Grav/Framework/Object/ObjectCollectionInterface.php index 2248417be..c4764ea1a 100644 --- a/system/src/Grav/Framework/Object/ObjectCollectionInterface.php +++ b/system/src/Grav/Framework/Object/ObjectCollectionInterface.php @@ -23,6 +23,12 @@ interface ObjectCollectionInterface extends CollectionInterface, ObjectInterface */ public function copy(); + /** + * @param string $key + * @return $this + */ + public function setKey($key); + /** * @return array */ diff --git a/system/src/Grav/Framework/Object/ObjectCollectionTrait.php b/system/src/Grav/Framework/Object/ObjectCollectionTrait.php index 8eab19169..52d8e592c 100644 --- a/system/src/Grav/Framework/Object/ObjectCollectionTrait.php +++ b/system/src/Grav/Framework/Object/ObjectCollectionTrait.php @@ -80,7 +80,8 @@ trait ObjectCollectionTrait $list = []; foreach ($this as $id => $element) { - $list[$id] = method_exists($element, $method) ? call_user_func_array([$element, $method], $arguments) : null; + $list[$id] = method_exists($element, $method) + ? call_user_func_array([$element, $method], $arguments) : null; } return $list; diff --git a/system/src/Grav/Framework/Object/ObjectTrait.php b/system/src/Grav/Framework/Object/ObjectTrait.php index c20a12aee..1c353eb58 100644 --- a/system/src/Grav/Framework/Object/ObjectTrait.php +++ b/system/src/Grav/Framework/Object/ObjectTrait.php @@ -8,9 +8,6 @@ namespace Grav\Framework\Object; -use RocketTheme\Toolbox\ArrayTraits\ArrayAccessWithGetters; -use RocketTheme\Toolbox\ArrayTraits\Export; - /** * Object trait. * @@ -32,6 +29,7 @@ trait ObjectTrait /** * @param array $elements * @param string $key + * @throws \InvalidArgumentException */ public function __construct(array $elements = [], $key = null) { diff --git a/system/src/Grav/Framework/Page/PageCollectionInterface.php b/system/src/Grav/Framework/Page/PageCollectionInterface.php index a932ed888..f2b486f94 100644 --- a/system/src/Grav/Framework/Page/PageCollectionInterface.php +++ b/system/src/Grav/Framework/Page/PageCollectionInterface.php @@ -10,5 +10,6 @@ namespace Grav\Framework\Page; use Grav\Framework\Collection\CollectionInterface; -interface PageCollectionInterface extends CollectionInterface { +interface PageCollectionInterface extends CollectionInterface +{ } diff --git a/system/src/Grav/Framework/Page/PageHeaderInterface.php b/system/src/Grav/Framework/Page/PageHeaderInterface.php index 6398d4da5..55a1d2517 100644 --- a/system/src/Grav/Framework/Page/PageHeaderInterface.php +++ b/system/src/Grav/Framework/Page/PageHeaderInterface.php @@ -8,5 +8,6 @@ namespace Grav\Framework\Page; -interface PageHeaderInterface extends \ArrayAccess { -} \ No newline at end of file +interface PageHeaderInterface extends \ArrayAccess +{ +} diff --git a/system/src/Grav/Framework/Page/PageMediaCollectionInterface.php b/system/src/Grav/Framework/Page/PageMediaCollectionInterface.php index c7fee7550..6f18ca6ee 100644 --- a/system/src/Grav/Framework/Page/PageMediaCollectionInterface.php +++ b/system/src/Grav/Framework/Page/PageMediaCollectionInterface.php @@ -10,5 +10,6 @@ namespace Grav\Framework\Page; use Grav\Framework\Collection\CollectionInterface; -interface PageMediaCollectionInterface extends CollectionInterface { +interface PageMediaCollectionInterface extends CollectionInterface +{ }