Skip to content

Commit

Permalink
Follow PSR-2 in Grav\Framework
Browse files Browse the repository at this point in the history
  • Loading branch information
mahagr committed Aug 18, 2017
1 parent d15eb0e commit 96ee41a
Show file tree
Hide file tree
Showing 16 changed files with 119 additions and 81 deletions.
2 changes: 0 additions & 2 deletions system/src/Grav/Framework/Cache/AbstractCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion system/src/Grav/Framework/Cache/Adapter/ChainCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -43,7 +44,13 @@ public function __construct(array $caches, $defaultLifetime = null)

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

Expand Down
45 changes: 37 additions & 8 deletions system/src/Grav/Framework/Cache/CacheTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@

namespace Grav\Framework\Cache;

use Grav\Framework\Cache\CacheInterface;
use Grav\Framework\Cache\Exception\InvalidArgumentException;

/**
Expand Down Expand Up @@ -106,13 +105,19 @@ public function clear()

/**
* @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)) {
Expand Down Expand Up @@ -146,7 +151,12 @@ public function setMultiple($values, $ttl = null)
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);
Expand All @@ -171,7 +181,12 @@ public function deleteMultiple($keys)
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)) {
Expand Down Expand Up @@ -242,16 +257,25 @@ abstract public function doHas($key);
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)
);
}
}

Expand Down Expand Up @@ -281,6 +305,11 @@ protected function convertTtl($ttl, $ignoreDefault = false)
$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)
)
);
}
}
16 changes: 8 additions & 8 deletions system/src/Grav/Framework/Collection/ArrayCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()));
}

/**
Expand All @@ -42,12 +42,12 @@ public function shuffle()
$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()));
}

/**
Expand Down
5 changes: 3 additions & 2 deletions system/src/Grav/Framework/Collection/FileCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -268,7 +269,7 @@ protected function doInitializeChildren(array $children, $nestingLimit)
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(),
Expand Down
7 changes: 4 additions & 3 deletions system/src/Grav/Framework/ContentBlock/ContentBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,7 @@ public function __toString()

/**
* @param array $serialized
* @throws \RuntimeException
*/
public function build(array $serialized)
{
Expand Down Expand Up @@ -221,9 +222,9 @@ protected function generateId()
*/
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));
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,4 @@ public function setContent($content);
* @return $this
*/
public function addBlock(ContentBlockInterface $block);
}
}
21 changes: 16 additions & 5 deletions system/src/Grav/Framework/ContentBlock/HtmlBlock.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
class HtmlBlock extends ContentBlock implements HtmlBlockInterface
{
protected $version = 1;
protected $frameworks = [];
protected $styles = [];
protected $scripts = [];
Expand Down Expand Up @@ -73,7 +72,7 @@ public function getHtml($location = 'bottom')
}

/**
* @return array
* @return array[]
*/
public function toArray()
{
Expand All @@ -97,6 +96,7 @@ public function toArray()

/**
* @param array $serialized
* @throws \RuntimeException
*/
public function build(array $serialized)
{
Expand Down Expand Up @@ -144,7 +144,15 @@ public function addStyle($element, $priority = 0, $location = 'head')
$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',
Expand Down Expand Up @@ -354,10 +362,13 @@ protected function sortAssetsInLocation(array &$items)
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'];
}
);
}
Expand All @@ -371,4 +382,4 @@ protected function sortAssets(array &$array)
$this->sortAssetsInLocation($items);
}
}
}
}
50 changes: 13 additions & 37 deletions system/src/Grav/Framework/Object/Object.php
Original file line number Diff line number Diff line change
Expand Up @@ -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}().
*
Expand All @@ -63,7 +32,9 @@ public function getKey()
*/
public function offsetExists($offset)
{
return $this->parentOffsetExists($offset) || method_exists($this, "offsetLoad_{$offset}");
$methodName = "offsetLoad_{$offset}";

return $this->parentOffsetExists($offset) || method_exists($this, $methodName);
}

/**
Expand All @@ -74,24 +45,29 @@ public function offsetExists($offset)
*/
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);
}


/**
* 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);
Expand Down
19 changes: 13 additions & 6 deletions system/src/Grav/Framework/Object/ObjectCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

/**
Expand Down
Loading

0 comments on commit 96ee41a

Please sign in to comment.