From 7e52112b212d205bdaa994781210216f57c9149d Mon Sep 17 00:00:00 2001 From: Matias Griese Date: Sat, 19 Feb 2022 12:01:50 +0200 Subject: [PATCH] Fixed phpstan issues (All level 2, Framework level 5) --- CHANGELOG.md | 1 + composer.json | 4 ++-- composer.lock | 24 +++++++++---------- system/src/Grav/Common/Assets.php | 17 +++++++++++-- system/src/Grav/Common/Assets/BlockAssets.php | 2 +- .../Common/Flex/Types/Pages/PageIndex.php | 6 ++++- .../Common/Flex/Types/Pages/PageObject.php | 9 ++++++- .../Media/Interfaces/MediaObjectInterface.php | 2 ++ .../Common/Page/Medium/VectorImageMedium.php | 8 +++---- system/src/Grav/Common/Page/Pages.php | 19 +++++++++++---- .../src/Grav/Common/Twig/TwigEnvironment.php | 10 ++++++-- 11 files changed, 73 insertions(+), 29 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 08737d2bc..3b3d19901 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ 1. [](#new) * Added support to get image size for SVG vector images [#3533](https://github.com/getgrav/grav/pull/3533) + * Fixed phpstan issues (All level 2, Framework level 5) 2. [](#bugfix) * Fixed `'mbstring' extension is not loaded` error, use Polyfill instead [#3504](https://github.com/getgrav/grav/pull/3504) diff --git a/composer.json b/composer.json index 511690104..db5449738 100644 --- a/composer.json +++ b/composer.json @@ -115,8 +115,8 @@ "scripts": { "api-17": "vendor/bin/phpdoc-md generate system/src > user/pages/14.api/default.17.md", "post-create-project-cmd": "bin/grav install", - "phpstan": "vendor/bin/phpstan analyse -l 1 -c ./tests/phpstan/phpstan.neon --memory-limit=720M system/src", - "phpstan-framework": "vendor/bin/phpstan analyse -l 4 -c ./tests/phpstan/phpstan.neon --memory-limit=480M system/src/Grav/Framework system/src/Grav/Events system/src/Grav/Installer", + "phpstan": "vendor/bin/phpstan analyse -l 2 -c ./tests/phpstan/phpstan.neon --memory-limit=720M system/src", + "phpstan-framework": "vendor/bin/phpstan analyse -l 5 -c ./tests/phpstan/phpstan.neon --memory-limit=480M system/src/Grav/Framework system/src/Grav/Events system/src/Grav/Installer", "phpstan-plugins": "vendor/bin/phpstan analyse -l 1 -c ./tests/phpstan/plugins.neon --memory-limit=400M user/plugins", "test": "vendor/bin/codecept run unit", "test-windows": "vendor\\bin\\codecept run unit" diff --git a/composer.lock b/composer.lock index a90114272..06c0bbf38 100644 --- a/composer.lock +++ b/composer.lock @@ -4748,16 +4748,16 @@ }, { "name": "phpunit/php-code-coverage", - "version": "9.2.10", + "version": "9.2.11", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/php-code-coverage.git", - "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687" + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/d5850aaf931743067f4bfc1ae4cbd06468400687", - "reference": "d5850aaf931743067f4bfc1ae4cbd06468400687", + "url": "https://api.github.com/repos/sebastianbergmann/php-code-coverage/zipball/665a1ac0a763c51afc30d6d130dac0813092b17f", + "reference": "665a1ac0a763c51afc30d6d130dac0813092b17f", "shasum": "" }, "require": { @@ -4813,7 +4813,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/php-code-coverage/issues", - "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.10" + "source": "https://github.com/sebastianbergmann/php-code-coverage/tree/9.2.11" }, "funding": [ { @@ -4821,7 +4821,7 @@ "type": "github" } ], - "time": "2021-12-05T09:12:13+00:00" + "time": "2022-02-18T12:46:09+00:00" }, { "name": "phpunit/php-file-iterator", @@ -5066,16 +5066,16 @@ }, { "name": "phpunit/phpunit", - "version": "9.5.13", + "version": "9.5.14", "source": { "type": "git", "url": "https://github.com/sebastianbergmann/phpunit.git", - "reference": "597cb647654ede35e43b137926dfdfef0fb11743" + "reference": "1883687169c017d6ae37c58883ca3994cfc34189" }, "dist": { "type": "zip", - "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/597cb647654ede35e43b137926dfdfef0fb11743", - "reference": "597cb647654ede35e43b137926dfdfef0fb11743", + "url": "https://api.github.com/repos/sebastianbergmann/phpunit/zipball/1883687169c017d6ae37c58883ca3994cfc34189", + "reference": "1883687169c017d6ae37c58883ca3994cfc34189", "shasum": "" }, "require": { @@ -5153,7 +5153,7 @@ ], "support": { "issues": "https://github.com/sebastianbergmann/phpunit/issues", - "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.13" + "source": "https://github.com/sebastianbergmann/phpunit/tree/9.5.14" }, "funding": [ { @@ -5165,7 +5165,7 @@ "type": "github" } ], - "time": "2022-01-24T07:33:35+00:00" + "time": "2022-02-18T12:54:07+00:00" }, { "name": "psr/http-client", diff --git a/system/src/Grav/Common/Assets.php b/system/src/Grav/Common/Assets.php index 134c62fba..23878a390 100644 --- a/system/src/Grav/Common/Assets.php +++ b/system/src/Grav/Common/Assets.php @@ -525,8 +525,8 @@ class Assets extends PropertyObject /** * Build the Javascript Modules tags * - * @param $group - * @param $attributes + * @param string $group + * @param array $attributes * @return string */ public function jsModule($group = 'head', $attributes = []) @@ -534,6 +534,11 @@ class Assets extends PropertyObject return $this->render(self::JS_MODULE, $group, $attributes); } + /** + * @param string $group + * @param array $attributes + * @return string + */ public function all($group = 'head', $attributes = []) { $output = $this->css($group, $attributes, false); @@ -543,11 +548,19 @@ class Assets extends PropertyObject return $output; } + /** + * @param class-string $type + * @return bool + */ protected function isValidType($type) { return in_array($type, [self::CSS_TYPE, self::JS_TYPE, self::JS_MODULE_TYPE]); } + /** + * @param class-string $type + * @return string + */ protected function getBaseType($type) { switch ($type) { diff --git a/system/src/Grav/Common/Assets/BlockAssets.php b/system/src/Grav/Common/Assets/BlockAssets.php index 798bdd304..7d9796090 100644 --- a/system/src/Grav/Common/Assets/BlockAssets.php +++ b/system/src/Grav/Common/Assets/BlockAssets.php @@ -55,7 +55,7 @@ class BlockAssets /** * @param Assets $assets - * @param array $groups + * @param array $list * @return void */ protected static function registerFrameworks(Assets $assets, array $list): void diff --git a/system/src/Grav/Common/Flex/Types/Pages/PageIndex.php b/system/src/Grav/Common/Flex/Types/Pages/PageIndex.php index c0ca5c405..f3978bc1d 100644 --- a/system/src/Grav/Common/Flex/Types/Pages/PageIndex.php +++ b/system/src/Grav/Common/Flex/Types/Pages/PageIndex.php @@ -606,8 +606,10 @@ class PageIndex extends FlexPageIndex implements PageCollectionInterface } } + /** @var PageCollection|PageIndex $children */ + $children = $page->children(); /** @var PageIndex $children */ - $children = $page->children()->getIndex(); + $children = $children->getIndex(); $selectedChildren = $children->filterBy($filters, true); /** @var Header $header */ @@ -686,6 +688,8 @@ class PageIndex extends FlexPageIndex implements PageCollectionInterface $extras = array_filter($extras, static function ($v) { return $v !== null; }); + + /** @var PageIndex $tmp */ $tmp = $child->children()->getIndex(); $child_count = $tmp->count(); $count = $filters ? $tmp->filterBy($filters, true)->count() : null; diff --git a/system/src/Grav/Common/Flex/Types/Pages/PageObject.php b/system/src/Grav/Common/Flex/Types/Pages/PageObject.php index fed11b437..a05f0eb42 100644 --- a/system/src/Grav/Common/Flex/Types/Pages/PageObject.php +++ b/system/src/Grav/Common/Flex/Types/Pages/PageObject.php @@ -625,7 +625,14 @@ class PageObject extends FlexPageObject // If current filter does not match, we still may have match as a parent. if ($matches === false) { - return $recursive && $this->children()->getIndex()->filterBy($filters, true)->count() > 0; + if (!$recursive) { + return false; + } + + /** @var PageIndex $index */ + $index = $this->children()->getIndex(); + + return $index->filterBy($filters, true)->count() > 0; } } diff --git a/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php b/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php index 4af3052b0..933475962 100644 --- a/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php +++ b/system/src/Grav/Common/Media/Interfaces/MediaObjectInterface.php @@ -16,6 +16,8 @@ use Grav\Common\Data\Data; * Class implements media object interface. * * @property string $type + * @property string $filename + * @property string $filepath */ interface MediaObjectInterface extends \Grav\Framework\Media\Interfaces\MediaObjectInterface, ArrayAccess { diff --git a/system/src/Grav/Common/Page/Medium/VectorImageMedium.php b/system/src/Grav/Common/Page/Medium/VectorImageMedium.php index 846870131..c44f35a77 100644 --- a/system/src/Grav/Common/Page/Medium/VectorImageMedium.php +++ b/system/src/Grav/Common/Page/Medium/VectorImageMedium.php @@ -53,10 +53,10 @@ class VectorImageMedium extends StaticImageMedium } // Get the size from svg image. - if ($attr->width > 0 && $attr->height > 0) { - $width = $attr->width; - $height = $attr->height; - } elseif ($attr->viewBox && \count($size = explode(' ', $attr->viewBox)) === 4) { + if ($attr->width && $attr->height) { + $width = (string)$attr->width; + $height = (string)$attr->height; + } elseif ($attr->viewBox && \count($size = explode(' ', (string)$attr->viewBox)) === 4) { [,$width,$height,] = $size; } diff --git a/system/src/Grav/Common/Page/Pages.php b/system/src/Grav/Common/Page/Pages.php index 8ad2ccd8f..ec48b5173 100644 --- a/system/src/Grav/Common/Page/Pages.php +++ b/system/src/Grav/Common/Page/Pages.php @@ -736,7 +736,13 @@ class Pages break; case 'siblings': $parent = $page->parent(); - $collection = $parent ? $parent->children()->remove($page->path()) : new Collection(); + if ($parent) { + /** @var Collection $collection */ + $collection = $parent->children(); + $collection = $collection->remove($page->path()); + } else { + $collection = new Collection(); + } break; case 'descendants': $collection = $this->all($page)->remove($page->path())->pages(); @@ -1041,9 +1047,14 @@ class Pages $this->grav->redirectLangSafe($page->redirect()); } - if (!$routable && ($child = $page->children()->visible()->routable()->published()->first()) !== null) { - // Redirect to the first visible child as current page isn't routable. - $this->grav->redirectLangSafe($child->route()); + if (!$routable) { + /** @var Collection $children */ + $children = $page->children()->visible()->routable()->published(); + $child = $children->first(); + if ($child !== null) { + // Redirect to the first visible child as current page isn't routable. + $this->grav->redirectLangSafe($child->route()); + } } } diff --git a/system/src/Grav/Common/Twig/TwigEnvironment.php b/system/src/Grav/Common/Twig/TwigEnvironment.php index 6b2a86bef..dba884693 100644 --- a/system/src/Grav/Common/Twig/TwigEnvironment.php +++ b/system/src/Grav/Common/Twig/TwigEnvironment.php @@ -11,6 +11,8 @@ namespace Grav\Common\Twig; use Twig\Environment; use Twig\Error\LoaderError; +use Twig\Loader\ExistsLoaderInterface; +use Twig\Loader\LoaderInterface; use Twig\Template; use Twig\TemplateWrapper; @@ -41,8 +43,12 @@ class TwigEnvironment extends Environment } // Optimization: Avoid throwing an exception when it would be ignored anyway. - if (1 !== $count && !$this->getLoader()->exists($name)) { - continue; + if (1 !== $count) { + /** @var LoaderInterface|ExistsLoaderInterface $loader */ + $loader = $this->getLoader(); + if (!$loader->exists($name)) { + continue; + } } // Throws LoaderError: Unable to find template "%s".