Fixed phpstan level 3 errors in Grav\Framework

This commit is contained in:
Matias Griese 2019-03-14 13:20:29 +02:00
parent 2b392055c1
commit c09a8fbbc4
10 changed files with 99 additions and 63 deletions

View File

@ -90,6 +90,7 @@
"scripts": {
"post-create-project-cmd": "bin/grav install",
"phpstan": "vendor/bin/phpstan analyse -l 2 -c ./tests/phpstan/phpstan.neon system/src --memory-limit=256M",
"phpstan-framework": "vendor/bin/phpstan analyse -l 3 -c ./tests/phpstan/phpstan.neon system/src/Grav/Framework --memory-limit=256M",
"test": "vendor/bin/codecept run unit",
"test-windows": "vendor\\bin\\codecept run unit"
},

View File

@ -22,19 +22,19 @@ class AbstractFile implements FileInterface
/** @var string */
private $filepath;
/** @var string */
/** @var string|null */
private $filename;
/** @var string */
/** @var string|null */
private $path;
/** @var string */
/** @var string|null */
private $basename;
/** @var string */
/** @var string|null */
private $extension;
/** @var resource */
/** @var resource|null */
private $handle;
/** @var bool */

View File

@ -15,11 +15,16 @@ use Grav\Common\Grav;
use Grav\Common\Twig\Twig;
use Grav\Common\User\Interfaces\UserInterface;
use Grav\Framework\Cache\CacheInterface;
use Grav\Framework\ContentBlock\ContentBlockInterface;
use Grav\Framework\ContentBlock\HtmlBlock;
use Grav\Framework\Flex\Interfaces\FlexIndexInterface;
use Grav\Framework\Object\ObjectCollection;
use Grav\Framework\Flex\Interfaces\FlexCollectionInterface;
use Psr\SimpleCache\InvalidArgumentException;
use RocketTheme\Toolbox\Event\Event;
use Twig\Error\LoaderError;
use Twig\Error\SyntaxError;
use Twig\TemplateWrapper;
/**
* Class FlexCollection
@ -158,11 +163,11 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*
* @param string $layout
* @param array $context
* @return HtmlBlock
* @return ContentBlockInterface|HtmlBlock
* @throws \Exception
* @throws \Throwable
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws SyntaxError
*/
public function render($layout = null, array $context = [])
{
@ -301,7 +306,10 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
public function getTimestamps()
{
return $this->call('getTimestamp');
/** @var int[] $timestamps */
$timestamps = $this->call('getTimestamp');
return $timestamps;
}
/**
@ -309,7 +317,10 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
public function getStorageKeys()
{
return $this->call('getStorageKey');
/** @var string[] $keys */
$keys = $this->call('getStorageKey');
return $keys;
}
/**
@ -317,13 +328,16 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
public function getFlexKeys()
{
return $this->call('getFlexKey');
/** @var string[] $keys */
$keys = $this->call('getFlexKey');
return $keys;
}
/**
* @return FlexIndex
* @return FlexIndexInterface
*/
public function getIndex(): FlexIndex
public function getIndex(): FlexIndexInterface
{
return $this->getFlexDirectory()->getIndex($this->getKeys(), $this->getKeyField());
}
@ -422,9 +436,9 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
/**
* @param string $layout
* @return \Twig_Template
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
* @return TemplateWrapper
* @throws LoaderError
* @throws SyntaxError
*/
protected function getTemplate($layout) //: \Twig_Template
{
@ -435,7 +449,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
try {
return $twig->twig()->resolveTemplate(["flex-objects/layouts/{$this->getType(false)}/collection/{$layout}.html.twig"]);
} catch (\Twig_Error_Loader $e) {
} catch (LoaderError $e) {
/** @var Debugger $debugger */
$debugger = Grav::instance()['debugger'];
$debugger->addException($e);

View File

@ -18,10 +18,10 @@ use Grav\Common\Utils;
use Grav\Framework\Cache\Adapter\DoctrineCache;
use Grav\Framework\Cache\Adapter\MemoryCache;
use Grav\Framework\Cache\CacheInterface;
use Grav\Framework\Collection\CollectionInterface;
use Grav\Framework\Flex\Interfaces\FlexAuthorizeInterface;
use Grav\Framework\Flex\Interfaces\FlexCollectionInterface;
use Grav\Framework\Flex\Interfaces\FlexIndexInterface;
use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
use Grav\Framework\Flex\Interfaces\FlexStorageInterface;
use Grav\Framework\Flex\Storage\SimpleStorage;
use Grav\Framework\Flex\Traits\FlexAuthorizeTrait;
@ -45,9 +45,9 @@ class FlexDirectory implements FlexAuthorizeInterface
protected $blueprints;
/** @var bool[] */
protected $blueprints_init;
/** @var FlexIndex */
/** @var FlexIndexInterface|null */
protected $index;
/** @var FlexCollection */
/** @var FlexCollectionInterface|null */
protected $collection;
/** @var bool */
protected $enabled;
@ -55,7 +55,7 @@ class FlexDirectory implements FlexAuthorizeInterface
protected $defaults;
/** @var Config */
protected $config;
/** @var object */
/** @var FlexStorageInterface */
protected $storage;
/** @var CacheInterface */
protected $cache;
@ -170,9 +170,9 @@ class FlexDirectory implements FlexAuthorizeInterface
*
* @param array|null $keys Array of keys.
* @param string|null $keyField Field to be used as the key.
* @return FlexIndex|FlexCollection
* @return FlexCollectionInterface
*/
public function getCollection(array $keys = null, string $keyField = null): CollectionInterface
public function getCollection(array $keys = null, string $keyField = null): FlexCollectionInterface
{
// Get all selected entries.
$index = $this->getIndex($keys, $keyField);
@ -196,10 +196,9 @@ class FlexDirectory implements FlexAuthorizeInterface
*
* @param array|null $keys Array of keys.
* @param string|null $keyField Field to be used as the key.
* @return FlexIndex|FlexCollection
* @internal
* @return FlexIndexInterface
*/
public function getIndex(array $keys = null, string $keyField = null): CollectionInterface
public function getIndex(array $keys = null, string $keyField = null): FlexIndexInterface
{
$index = clone $this->loadIndex();
$index = $index->withKeyField($keyField);
@ -208,7 +207,7 @@ class FlexDirectory implements FlexAuthorizeInterface
$index = $index->select($keys);
}
return $index;
return $index->getIndex();
}
/**
@ -218,9 +217,9 @@ class FlexDirectory implements FlexAuthorizeInterface
*
* @param string $key
* @param string|null $keyField Field to be used as the key.
* @return FlexObject|null
* @return FlexObjectInterface|null
*/
public function getObject($key, string $keyField = null): ?FlexObject
public function getObject($key, string $keyField = null): ?FlexObjectInterface
{
return $this->getIndex(null, $keyField)->get($key);
}
@ -228,9 +227,9 @@ class FlexDirectory implements FlexAuthorizeInterface
/**
* @param array $data
* @param string|null $key
* @return FlexObject
* @return FlexObjectInterface
*/
public function update(array $data, string $key = null): FlexObject
public function update(array $data, string $key = null): FlexObjectInterface
{
$object = null !== $key ? $this->getIndex()->get($key): null;
@ -274,9 +273,9 @@ class FlexDirectory implements FlexAuthorizeInterface
/**
* @param string $key
* @return FlexObject|null
* @return FlexObjectInterface|null
*/
public function remove(string $key): ?FlexObject
public function remove(string $key): ?FlexObjectInterface
{
$object = null !== $key ? $this->getIndex()->get($key): null;
if (!$object) {
@ -295,8 +294,9 @@ class FlexDirectory implements FlexAuthorizeInterface
public function getCache(string $namespace = null): CacheInterface
{
$namespace = $namespace ?: 'index';
$cache = $this->cache[$namespace] ?? null;
if (!isset($this->cache[$namespace])) {
if (null === $cache) {
try {
$grav = Grav::instance();
@ -312,20 +312,21 @@ class FlexDirectory implements FlexAuthorizeInterface
if (Utils::isAdminPlugin()) {
$key = substr($key, 0, -1);
}
$this->cache[$namespace] = new DoctrineCache($gravCache->getCacheDriver(), 'flex-objects-' . $this->getType() . $key, $timeout);
$cache = new DoctrineCache($gravCache->getCacheDriver(), 'flex-objects-' . $this->getType() . $key, $timeout);
} catch (\Exception $e) {
/** @var Debugger $debugger */
$debugger = Grav::instance()['debugger'];
$debugger->addException($e);
$this->cache[$namespace] = new MemoryCache('flex-objects-' . $this->getType());
$cache = new MemoryCache('flex-objects-' . $this->getType());
}
// Disable cache key validation.
$this->cache[$namespace]->setValidation(false);
$cache->setValidation(false);
$this->cache[$namespace] = $cache;
}
return $this->cache[$namespace];
return $cache;
}
/**
@ -386,11 +387,11 @@ class FlexDirectory implements FlexAuthorizeInterface
* @param array $data
* @param string $key
* @param bool $validate
* @return FlexObject
* @return FlexObjectInterface
*/
public function createObject(array $data, string $key = '', bool $validate = false): FlexObject
public function createObject(array $data, string $key = '', bool $validate = false): FlexObjectInterface
{
/** @var string|FlexObject $className */
/** @var string|FlexObjectInterface $className */
$className = $this->objectClassName ?: $this->getObjectClass();
return new $className($data, $key, $this, $validate);
@ -412,7 +413,7 @@ class FlexDirectory implements FlexAuthorizeInterface
/**
* @param array $entries
* @param string $keyField
* @return FlexCollectionInterface
* @return FlexCollectionInterface|FlexIndexInterface
*/
public function createIndex(array $entries, string $keyField = null): FlexCollectionInterface
{
@ -472,7 +473,7 @@ class FlexDirectory implements FlexAuthorizeInterface
/**
* @param array $entries
* @return FlexObject[]
* @return FlexObjectInterface[]
* @internal
*/
public function loadObjects(array $entries): array
@ -600,13 +601,15 @@ class FlexDirectory implements FlexAuthorizeInterface
}
/**
* @return FlexIndex|FlexCollection
* @return FlexIndexInterface
*/
protected function loadIndex(): CollectionInterface
protected function loadIndex(): FlexIndexInterface
{
static $i = 0;
if (null === $this->index) {
$index = $this->index;
if (null === $index) {
$i++; $j = $i;
/** @var Debugger $debugger */
$debugger = Grav::instance()['debugger'];
@ -636,12 +639,14 @@ class FlexDirectory implements FlexAuthorizeInterface
}
// We need to do this in two steps as orderBy() calls loadIndex() again and we do not want infinite loop.
$this->index = $this->createIndex($keys);
$this->index = $this->index->orderBy($this->getConfig('data.ordering', []));
$index = $this->createIndex($keys);
/** @var FlexCollectionInterface $collection */
$collection = $index->orderBy($this->getConfig('data.ordering', []));
$this->index = $index = $collection->getIndex();
$debugger->stopTimer('flex-keys-' . $this->type . $j);
}
return $this->index;
return $index;
}
}

View File

@ -147,7 +147,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
/**
* @return $this
*/
public function getIndex()
public function getIndex(): FlexIndexInterface
{
return $this;
}

View File

@ -16,6 +16,7 @@ use Grav\Common\Grav;
use Grav\Common\Twig\Twig;
use Grav\Common\Utils;
use Grav\Framework\Cache\CacheInterface;
use Grav\Framework\ContentBlock\ContentBlockInterface;
use Grav\Framework\ContentBlock\HtmlBlock;
use Grav\Framework\Flex\Interfaces\FlexAuthorizeInterface;
use Grav\Framework\Flex\Interfaces\FlexFormInterface;
@ -28,6 +29,9 @@ use Grav\Framework\Flex\Interfaces\FlexObjectInterface;
use Grav\Framework\Object\Property\LazyPropertyTrait;
use Psr\SimpleCache\InvalidArgumentException;
use RocketTheme\Toolbox\Event\Event;
use Twig\Error\LoaderError;
use Twig\Error\SyntaxError;
use Twig\TemplateWrapper;
/**
* Class FlexObject
@ -46,7 +50,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
/** @var FlexDirectory */
private $_flexDirectory;
/** @var FlexForm[] */
/** @var FlexFormInterface[] */
private $_forms = [];
/** @var array */
private $_storage;
@ -272,9 +276,9 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
/**
* @param string $name
* @param array|null $form
* @return FlexForm
* @return FlexFormInterface
*/
public function getForm(string $name = '', array $form = null)
public function getForm(string $name = '', array $form = null): FlexFormInterface
{
if (!isset($this->_forms[$name])) {
$this->_forms[$name] = $this->createFormObject($name, $form);
@ -389,11 +393,11 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
*
* @param string $layout
* @param array $context
* @return HtmlBlock
* @return ContentBlockInterface|HtmlBlock
* @throws \Exception
* @throws \Throwable
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
* @throws LoaderError
* @throws SyntaxError
*/
public function render($layout = null, array $context = [])
{
@ -706,6 +710,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
$collection = $directory->getCollection();
$list = $this->getNestedProperty($property) ?: [];
/** @var FlexCollection $collection */
$collection = $collection->filter(function ($object) use ($list) { return \in_array($object->id, $list, true); });
return $collection;
@ -730,9 +735,9 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
/**
* @param string $layout
* @return \Twig_Template
* @throws \Twig_Error_Loader
* @throws \Twig_Error_Syntax
* @return TemplateWrapper
* @throws LoaderError
* @throws SyntaxError
*/
protected function getTemplate($layout)
{
@ -743,7 +748,7 @@ class FlexObject implements FlexObjectInterface, FlexAuthorizeInterface
try {
return $twig->twig()->resolveTemplate(["flex-objects/layouts/{$this->getType(false)}/object/{$layout}.html.twig"]);
} catch (\Twig_Error_Loader $e) {
} catch (LoaderError $e) {
/** @var Debugger $debugger */
$debugger = Grav::instance()['debugger'];
$debugger->addException($e);

View File

@ -55,4 +55,9 @@ interface FlexCollectionInterface extends ObjectCollectionInterface, NestedObjec
* @return FlexCollectionInterface
*/
public function withKeyField(string $keyField = null): FlexCollectionInterface;
/**
* @return FlexIndexInterface
*/
public function getIndex(): FlexIndexInterface;
}

View File

@ -94,6 +94,11 @@ interface FlexObjectInterface extends NestedObjectInterface, \ArrayAccess
*/
public function exists();
/**
* @return array
*/
public function prepareStorage();
/**
* @param array $data
* @param array $files

View File

@ -36,11 +36,11 @@ trait FormTrait
private $submitted;
/** @var string[] */
private $errors;
/** @var \ArrayAccess */
/** @var \ArrayAccess|null */
private $data;
/** @var array|UploadedFileInterface[] */
private $files;
/** @var FormFlash */
/** @var FormFlash|null */
private $flash;
/** @var Blueprint */
private $blueprint;

View File

@ -9,6 +9,7 @@
namespace Grav\Framework\Object;
use Doctrine\Common\Collections\Collection;
use Doctrine\Common\Collections\Criteria;
use Grav\Framework\Collection\ArrayCollection;
use Grav\Framework\Object\Access\NestedPropertyCollectionTrait;
@ -41,7 +42,7 @@ class ObjectCollection extends ArrayCollection implements ObjectCollectionInterf
/**
* @param array $ordering
* @return static
* @return Collection|static
*/
public function orderBy(array $ordering)
{