Fixed phpstan issues in Framework up to level 6

This commit is contained in:
Matias Griese
2019-10-09 10:41:15 +03:00
parent efd4e52571
commit c325c24897
18 changed files with 65 additions and 49 deletions

View File

@@ -6,6 +6,7 @@
1. [](#bugfix)
* Fixed `Page::untranslatedLanguages()` not being symmetrical to `Page::translatedLanguages()`
* Fixed `Flex Pages` not calling `onPageProcessed` event when cached
* Fixed phpstan issues in Framework up to level 6
# v1.7.0-beta.10
## 10/03/2019

View File

@@ -130,6 +130,10 @@ class ChainCache extends AbstractCache
public function doGetMultiple($keys, $miss)
{
$list = [];
/**
* @var int $i
* @var CacheInterface $cache
*/
foreach ($this->caches as $i => $cache) {
$list[$i] = $cache->doGetMultiple($keys, $miss);
@@ -140,8 +144,12 @@ class ChainCache extends AbstractCache
}
}
$values = [];
// Update all the previous caches with missing values.
$values = [];
/**
* @var int $i
* @var CacheInterface $cache
*/
foreach (array_reverse($list) as $i => $items) {
$values += $items;
if ($i && $values) {

View File

@@ -51,12 +51,12 @@ class FileCache extends AbstractCache
fclose($h);
@unlink($file);
} else {
$i = rawurldecode(rtrim(fgets($h)));
$value = stream_get_contents($h);
$i = rawurldecode(rtrim((string)fgets($h)));
$value = stream_get_contents($h) ?: '';
fclose($h);
if ($i === $key) {
return unserialize($value);
return unserialize($value, ['allowed_classes' => true]);
}
}

View File

@@ -94,6 +94,10 @@ class AbstractFileCollection extends AbstractLazyCollection implements FileColle
if ($orderings = $criteria->getOrderings()) {
$next = null;
/**
* @var string $field
* @var string $ordering
*/
foreach (array_reverse($orderings) as $field => $ordering) {
$next = ClosureExpressionVisitor::sortByField($field, $ordering === Criteria::DESC ? -1 : 1, $next);
}

View File

@@ -44,7 +44,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
public function first()
{
$value = reset($this->entries);
$key = key($this->entries);
$key = (string)key($this->entries);
return $this->loadElement($key, $value);
}
@@ -55,7 +55,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
public function last()
{
$value = end($this->entries);
$key = key($this->entries);
$key = (string)key($this->entries);
return $this->loadElement($key, $value);
}
@@ -65,7 +65,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
*/
public function key()
{
return key($this->entries);
return (string)key($this->entries);
}
/**
@@ -74,7 +74,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
public function next()
{
$value = next($this->entries);
$key = key($this->entries);
$key = (string)key($this->entries);
return $this->loadElement($key, $value);
}
@@ -85,7 +85,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
public function current()
{
$value = current($this->entries);
$key = key($this->entries);
$key = (string)key($this->entries);
return $this->loadElement($key, $value);
}
@@ -102,7 +102,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
$value = $this->entries[$key];
unset($this->entries[$key]);
return $this->loadElement($key, $value);
return $this->loadElement((string)$key, $value);
}
/**
@@ -210,7 +210,7 @@ abstract class AbstractIndexCollection implements CollectionInterface
return null;
}
return $this->loadElement($key, $this->entries[$key]);
return $this->loadElement((string)$key, $this->entries[$key]);
}
/**

View File

@@ -21,14 +21,14 @@ interface CollectionInterface extends Collection, \JsonSerializable
/**
* Reverse the order of the items.
*
* @return static
* @return static|CollectionInterface
*/
public function reverse();
/**
* Shuffle items.
*
* @return static
* @return static|CollectionInterface
*/
public function shuffle();
@@ -46,7 +46,7 @@ interface CollectionInterface extends Collection, \JsonSerializable
* Collection is returned in the order of $keys given to the function.
*
* @param array $keys
* @return static
* @return static|CollectionInterface
*/
public function select(array $keys);
@@ -54,7 +54,7 @@ interface CollectionInterface extends Collection, \JsonSerializable
* Un-select items from collection.
*
* @param array $keys
* @return static
* @return static|CollectionInterface
*/
public function unselect(array $keys);
}

View File

@@ -158,7 +158,7 @@ class AbstractFile implements FileInterface
*/
public function getCreationTime(): int
{
return is_file($this->filepath) ? filectime($this->filepath) : time();
return is_file($this->filepath) ? (int)filectime($this->filepath) : time();
}
/**
@@ -167,7 +167,7 @@ class AbstractFile implements FileInterface
*/
public function getModificationTime(): int
{
return is_file($this->filepath) ? filemtime($this->filepath) : time();
return is_file($this->filepath) ? (int)filemtime($this->filepath) : time();
}
/**
@@ -180,7 +180,7 @@ class AbstractFile implements FileInterface
if (!$this->mkdir($this->getPath())) {
throw new \RuntimeException('Creating directory failed for ' . $this->filepath);
}
$this->handle = @fopen($this->filepath, 'cb+');
$this->handle = @fopen($this->filepath, 'cb+') ?: null;
if (!$this->handle) {
$error = error_get_last();
@@ -382,6 +382,7 @@ class AbstractFile implements FileInterface
protected function setPathInfo(): void
{
/** @var array $pathInfo */
$pathInfo = $this->filesystem->pathinfo($this->filepath);
$this->filename = $pathInfo['filename'] ?? null;

View File

@@ -40,7 +40,11 @@ class DataFile extends AbstractFile
$raw = parent::load();
try {
return $raw !== false ? $this->formatter->decode($raw) : false;
if (!is_string($raw)) {
throw new RuntimeException('Bad Data');
}
return $this->formatter->decode($raw);
} catch (RuntimeException $e) {
throw new RuntimeException(sprintf("Failed to load file '%s': %s", $this->getFilePath(), $e->getMessage()), $e->getCode(), $e);
}

View File

@@ -57,7 +57,7 @@ abstract class AbstractFormatter implements FileFormatterInterface
$extensions = $this->getSupportedFileExtensions();
// Call fails on bad configuration.
return reset($extensions);
return reset($extensions) ?: '';
}
/**

View File

@@ -91,7 +91,7 @@ class CsvFormatter extends AbstractFormatter
$csv_line = str_getcsv($line, $delimiter);
if ($null_replace) {
array_walk($csv_line, function(&$el) use ($null_replace) {
array_walk($csv_line, static function(&$el) use ($null_replace) {
$el = str_replace($null_replace, null, $el);
});
}
@@ -100,7 +100,7 @@ class CsvFormatter extends AbstractFormatter
}
}
} catch (\Exception $e) {
throw new \Exception('Badly formatted CSV line: ' . $line);
throw new \RuntimeException('Badly formatted CSV line: ' . $line);
}
return $list;

View File

@@ -80,7 +80,7 @@ class JsonFormatter extends AbstractFormatter
throw new \RuntimeException('Encoding JSON failed: ' . json_last_error_msg());
}
return $encoded;
return $encoded ?: '';
}
/**

View File

@@ -34,7 +34,7 @@ class SerializeFormatter extends AbstractFormatter
*
* By default only allow stdClass class.
*
* @return array|bool
* @return array
*/
public function getOptions()
{
@@ -56,7 +56,8 @@ class SerializeFormatter extends AbstractFormatter
*/
public function decode($data)
{
$decoded = @unserialize($data, $this->getOptions());
$classes = $this->getOptions()['allowed_classes'] ?? false;
$decoded = @unserialize($data, ['allowed_classes' => $classes]);
if ($decoded === false && $data !== serialize(false)) {
throw new \RuntimeException('Decoding serialized data failed');
@@ -86,4 +87,4 @@ class SerializeFormatter extends AbstractFormatter
return $data;
}
}
}

View File

@@ -184,11 +184,15 @@ class Filesystem implements FilesystemInterface
* @param string $path
* @param int|null $options
*
* @return array
* @return array|string
*/
protected function pathinfoInternal(?string $scheme, string $path, int $options = null)
{
$info = $options ? \pathinfo($path, $options) : \pathinfo($path);
if ($options) {
return \pathinfo($path, $options);
}
$info = \pathinfo($path);
if (null !== $scheme) {
$info['scheme'] = $scheme;

View File

@@ -25,6 +25,7 @@ use Psr\SimpleCache\InvalidArgumentException;
use RocketTheme\Toolbox\Event\Event;
use Twig\Error\LoaderError;
use Twig\Error\SyntaxError;
use Twig\Template;
use Twig\TemplateWrapper;
/**
@@ -201,7 +202,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
*/
public function getCacheKey(): string
{
return $this->getTypePrefix() . $this->getFlexType() . '.' . sha1(json_encode($this->call('getKey')));
return $this->getTypePrefix() . $this->getFlexType() . '.' . sha1((string)json_encode($this->call('getKey')));
}
/**
@@ -219,7 +220,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
$list[$key] = $object->getCacheChecksum();
}
return sha1(json_encode($list));
return sha1((string)json_encode($list));
}
/**
@@ -331,7 +332,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
}
try {
$data = $cache ? $cache->get($key) : null;
$data = $cache && $key ? $cache->get($key) : null;
$block = $data ? HtmlBlock::fromArray($data) : null;
} catch (InvalidArgumentException $e) {
@@ -371,7 +372,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
$block->setContent($output);
try {
$cache && $block->isCached() && $cache->set($key, $block->toArray());
$cache && $key && $block->isCached() && $cache->set($key, $block->toArray());
} catch (InvalidArgumentException $e) {
$debugger->addException($e);
}
@@ -522,7 +523,7 @@ class FlexCollection extends ObjectCollection implements FlexCollectionInterface
/**
* @param string $layout
* @return TemplateWrapper
* @return Template|TemplateWrapper
* @throws LoaderError
* @throws SyntaxError
*/

View File

@@ -221,6 +221,7 @@ class FlexDirectory implements FlexAuthorizeInterface
$index = $index->withKeyField($keyField);
if (null !== $keys) {
/** @var FlexCollectionInterface $index */
$index = $index->select($keys);
}
@@ -694,17 +695,9 @@ class FlexDirectory implements FlexAuthorizeInterface
$params = [$object instanceof PageInterface && $object->modular() ? 'modular' : 'standard'];
}
[$o, $f] = explode('::', $function, 2);
$data = null;
if (!$f) {
if (\function_exists($o)) {
$data = \call_user_func_array($o, $params);
}
} else {
if (method_exists($o, $f)) {
$data = \call_user_func_array([$o, $f], $params);
}
if (\is_callable($function)) {
$data = \call_user_func_array($function, $params);
}
// If function returns a value,

View File

@@ -185,7 +185,7 @@ class FlexIndex extends ObjectIndex implements FlexCollectionInterface, FlexInde
$list[$key] = $value['checksum'] ?? $value['storage_timestamp'];
}
return sha1(json_encode($list));
return sha1((string)json_encode($list));
}
/**

View File

@@ -11,7 +11,6 @@ declare(strict_types=1);
namespace Grav\Framework\Flex\Pages;
use Grav\Common\Page\Interfaces\PageCollectionInterface;
use Grav\Common\Page\Interfaces\PageInterface;
use Grav\Framework\Flex\FlexCollection;
use Grav\Framework\Flex\Interfaces\FlexCollectionInterface;
@@ -116,7 +115,7 @@ class FlexPageCollection extends FlexCollection
* @param string $path
* @param int $direction either -1 or +1
*
* @return PageInterface|PageCollectionInterface|null The sibling item.
* @return PageInterface|null The sibling item.
*/
public function adjacentSibling($path, $direction = 1)
{

View File

@@ -243,7 +243,7 @@ class SimpleStorage extends AbstractFilesystemStorage
$this->save();
foreach ($list as $key => $row) {
$list[$key]['__META'] = $this->getObjectMeta($key, true);
$list[$key]['__META'] = $this->getObjectMeta((string)$key, true);
}
}
@@ -347,7 +347,7 @@ class SimpleStorage extends AbstractFilesystemStorage
}
$file = $this->getFile($path);
$file->save($this->data);
$this->modified = $file->modified();
$this->modified = (int)$file->modified(); // cast false to 0
$file->free();
} catch (\RuntimeException $e) {
throw new \RuntimeException(sprintf('Flex save(): %s', $e->getMessage()));
@@ -378,7 +378,7 @@ class SimpleStorage extends AbstractFilesystemStorage
}
$file = $this->getFile($path);
$this->modified = $file->modified();
$this->modified = (int)$file->modified(); // cast false to 0
$this->data = (array) $file->content();
$list = [];