diff --git a/CHANGELOG.md b/CHANGELOG.md index 2153254a6..901bd1dc6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/system/src/Grav/Framework/Cache/Adapter/ChainCache.php b/system/src/Grav/Framework/Cache/Adapter/ChainCache.php index e1eea7b6d..b530b3e89 100644 --- a/system/src/Grav/Framework/Cache/Adapter/ChainCache.php +++ b/system/src/Grav/Framework/Cache/Adapter/ChainCache.php @@ -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) { diff --git a/system/src/Grav/Framework/Cache/Adapter/FileCache.php b/system/src/Grav/Framework/Cache/Adapter/FileCache.php index a5a60fa5f..3d3a2ec1d 100644 --- a/system/src/Grav/Framework/Cache/Adapter/FileCache.php +++ b/system/src/Grav/Framework/Cache/Adapter/FileCache.php @@ -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]); } } diff --git a/system/src/Grav/Framework/Collection/AbstractFileCollection.php b/system/src/Grav/Framework/Collection/AbstractFileCollection.php index 61b3dfeb3..41471012f 100644 --- a/system/src/Grav/Framework/Collection/AbstractFileCollection.php +++ b/system/src/Grav/Framework/Collection/AbstractFileCollection.php @@ -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); } diff --git a/system/src/Grav/Framework/Collection/AbstractIndexCollection.php b/system/src/Grav/Framework/Collection/AbstractIndexCollection.php index 7bb5af620..059dd9d48 100644 --- a/system/src/Grav/Framework/Collection/AbstractIndexCollection.php +++ b/system/src/Grav/Framework/Collection/AbstractIndexCollection.php @@ -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]); } /** diff --git a/system/src/Grav/Framework/Collection/CollectionInterface.php b/system/src/Grav/Framework/Collection/CollectionInterface.php index 9239d4ace..4d0b59120 100644 --- a/system/src/Grav/Framework/Collection/CollectionInterface.php +++ b/system/src/Grav/Framework/Collection/CollectionInterface.php @@ -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); } diff --git a/system/src/Grav/Framework/File/AbstractFile.php b/system/src/Grav/Framework/File/AbstractFile.php index 0411701d3..9f34ab938 100644 --- a/system/src/Grav/Framework/File/AbstractFile.php +++ b/system/src/Grav/Framework/File/AbstractFile.php @@ -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; diff --git a/system/src/Grav/Framework/File/DataFile.php b/system/src/Grav/Framework/File/DataFile.php index 491487132..3a0e79270 100644 --- a/system/src/Grav/Framework/File/DataFile.php +++ b/system/src/Grav/Framework/File/DataFile.php @@ -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); } diff --git a/system/src/Grav/Framework/File/Formatter/AbstractFormatter.php b/system/src/Grav/Framework/File/Formatter/AbstractFormatter.php index 549ff6ff3..bd8e0c025 100644 --- a/system/src/Grav/Framework/File/Formatter/AbstractFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/AbstractFormatter.php @@ -57,7 +57,7 @@ abstract class AbstractFormatter implements FileFormatterInterface $extensions = $this->getSupportedFileExtensions(); // Call fails on bad configuration. - return reset($extensions); + return reset($extensions) ?: ''; } /** diff --git a/system/src/Grav/Framework/File/Formatter/CsvFormatter.php b/system/src/Grav/Framework/File/Formatter/CsvFormatter.php index 6c1c3b4f4..667cdad1e 100644 --- a/system/src/Grav/Framework/File/Formatter/CsvFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/CsvFormatter.php @@ -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; diff --git a/system/src/Grav/Framework/File/Formatter/JsonFormatter.php b/system/src/Grav/Framework/File/Formatter/JsonFormatter.php index 1ae16ae29..5411bbc47 100644 --- a/system/src/Grav/Framework/File/Formatter/JsonFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/JsonFormatter.php @@ -80,7 +80,7 @@ class JsonFormatter extends AbstractFormatter throw new \RuntimeException('Encoding JSON failed: ' . json_last_error_msg()); } - return $encoded; + return $encoded ?: ''; } /** diff --git a/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php b/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php index 73ed8122f..55f46cfec 100644 --- a/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php +++ b/system/src/Grav/Framework/File/Formatter/SerializeFormatter.php @@ -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; } -} \ No newline at end of file +} diff --git a/system/src/Grav/Framework/Filesystem/Filesystem.php b/system/src/Grav/Framework/Filesystem/Filesystem.php index 473a2eca5..9cd206df4 100644 --- a/system/src/Grav/Framework/Filesystem/Filesystem.php +++ b/system/src/Grav/Framework/Filesystem/Filesystem.php @@ -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; diff --git a/system/src/Grav/Framework/Flex/FlexCollection.php b/system/src/Grav/Framework/Flex/FlexCollection.php index 63528d1ed..55fcb8825 100644 --- a/system/src/Grav/Framework/Flex/FlexCollection.php +++ b/system/src/Grav/Framework/Flex/FlexCollection.php @@ -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 */ diff --git a/system/src/Grav/Framework/Flex/FlexDirectory.php b/system/src/Grav/Framework/Flex/FlexDirectory.php index 8d7487fe8..557f82d32 100644 --- a/system/src/Grav/Framework/Flex/FlexDirectory.php +++ b/system/src/Grav/Framework/Flex/FlexDirectory.php @@ -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, diff --git a/system/src/Grav/Framework/Flex/FlexIndex.php b/system/src/Grav/Framework/Flex/FlexIndex.php index 1cb088a40..9adcb5087 100644 --- a/system/src/Grav/Framework/Flex/FlexIndex.php +++ b/system/src/Grav/Framework/Flex/FlexIndex.php @@ -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)); } /** diff --git a/system/src/Grav/Framework/Flex/Pages/FlexPageCollection.php b/system/src/Grav/Framework/Flex/Pages/FlexPageCollection.php index 1b7a65277..1267bebc0 100644 --- a/system/src/Grav/Framework/Flex/Pages/FlexPageCollection.php +++ b/system/src/Grav/Framework/Flex/Pages/FlexPageCollection.php @@ -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) { diff --git a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php index 0b2815347..0beb2e7bf 100644 --- a/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php +++ b/system/src/Grav/Framework/Flex/Storage/SimpleStorage.php @@ -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 = [];