Follow PSR-2 in Grav\Framework

This commit is contained in:
Matias Griese 2017-08-18 12:28:13 +03:00
parent d15eb0e6ad
commit 96ee41a3dd
16 changed files with 119 additions and 81 deletions

View File

@ -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

View File

@ -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
)
);
}
}

View File

@ -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)
)
);
}
}

View File

@ -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()));
}
/**

View File

@ -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(),

View File

@ -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));
}
}
}
}

View File

@ -72,4 +72,4 @@ interface ContentBlockInterface extends \Serializable
* @return $this
*/
public function addBlock(ContentBlockInterface $block);
}
}

View File

@ -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);
}
}
}
}

View File

@ -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);

View File

@ -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;
}
/**

View File

@ -23,6 +23,12 @@ interface ObjectCollectionInterface extends CollectionInterface, ObjectInterface
*/
public function copy();
/**
* @param string $key
* @return $this
*/
public function setKey($key);
/**
* @return array
*/

View File

@ -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;

View File

@ -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)
{

View File

@ -10,5 +10,6 @@ namespace Grav\Framework\Page;
use Grav\Framework\Collection\CollectionInterface;
interface PageCollectionInterface extends CollectionInterface {
interface PageCollectionInterface extends CollectionInterface
{
}

View File

@ -8,5 +8,6 @@
namespace Grav\Framework\Page;
interface PageHeaderInterface extends \ArrayAccess {
}
interface PageHeaderInterface extends \ArrayAccess
{
}

View File

@ -10,5 +10,6 @@ namespace Grav\Framework\Page;
use Grav\Framework\Collection\CollectionInterface;
interface PageMediaCollectionInterface extends CollectionInterface {
interface PageMediaCollectionInterface extends CollectionInterface
{
}