From 00512b60e561b431559c6bfae33117c0cf2d1fb9 Mon Sep 17 00:00:00 2001 From: mscherer Date: Sat, 29 Aug 2020 14:16:15 +0200 Subject: [PATCH] Fix up PHPstan level 7 --- composer.json | 2 +- src/Controller/Component/CacheComponent.php | 11 +++++------ src/Routing/Middleware/CacheMiddleware.php | 4 ++-- src/Shell/CacheShell.php | 1 + src/Utility/FileCache.php | 2 +- tests/phpstan.neon | 7 ++++++- 6 files changed, 16 insertions(+), 11 deletions(-) diff --git a/composer.json b/composer.json index 2885a8c..36deb19 100644 --- a/composer.json +++ b/composer.json @@ -32,7 +32,7 @@ "test": "php phpunit.phar", "test-setup": "[ ! -f phpunit.phar ] && wget https://phar.phpunit.de/phpunit-8.5.2.phar && mv phpunit-8.5.2.phar phpunit.phar || true", "test-coverage" : "php phpunit.phar --log-junit tmp/coverage/unitreport.xml --coverage-html tmp/coverage --coverage-clover tmp/coverage/coverage.xml", - "stan": "phpstan analyse -c tests/phpstan.neon -l 5 src/", + "stan": "phpstan analyse -c tests/phpstan.neon src/", "stan-setup": "cp composer.json composer.backup && composer require --dev phpstan/phpstan:^0.12 && mv composer.backup composer.json", "cs-check": "phpcs -p -v --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php src/ tests/ config/", "cs-fix": "phpcbf -p --standard=vendor/fig-r/psr2r-sniffer/PSR2R/ruleset.xml --extensions=php src/ tests/ config/" diff --git a/src/Controller/Component/CacheComponent.php b/src/Controller/Component/CacheComponent.php index 46d6c46..020bf19 100644 --- a/src/Controller/Component/CacheComponent.php +++ b/src/Controller/Component/CacheComponent.php @@ -51,14 +51,13 @@ public function shutdown(EventInterface $event): void { } /** @var callable $when */ $when = $this->getConfig('when'); - if ($when !== null && $when($event->getSubject()->getRequest()) !== true) { + /** @var \Cake\Controller\Controller $controller */ + $controller = $event->getSubject(); + if ($when !== null && $when($controller->getRequest()) !== true) { return; } - /** @var \Cake\Http\Response $response */ - $response = $event->getSubject()->getResponse(); - - $content = (string)$response->getBody(); + $content = (string)$controller->getResponse()->getBody(); if (!$content) { return; } @@ -112,7 +111,7 @@ protected function _writeContent(string $content, $duration) { $url = str_replace($this->getController()->getRequest()->getAttribute('base'), '', $url); $cacheKey = CacheKey::generate($url, $this->getConfig('prefix')); - $ext = $this->getController()->getResponse()->mapType($this->getController()->getResponse()->getType()); + $ext = (string)$this->getController()->getResponse()->mapType($this->getController()->getResponse()->getType()); $content = $this->_compress($content, $ext); $content = '' . $content; diff --git a/src/Routing/Middleware/CacheMiddleware.php b/src/Routing/Middleware/CacheMiddleware.php index 2a2a682..39aea9d 100644 --- a/src/Routing/Middleware/CacheMiddleware.php +++ b/src/Routing/Middleware/CacheMiddleware.php @@ -124,7 +124,7 @@ protected function getContent(string $url, string $cacheKey) { return null; } - return file_get_contents($file); + return file_get_contents($file) ?: null; } return Cache::read($cacheKey, $engine) ?: null; @@ -229,7 +229,7 @@ protected function _deliverCacheFile(ServerRequest $request, Response $response, $modifiedTime = $cacheStart ?: time(); if (!$cacheEnd) { - $cacheEnd = $this->getConfig('cacheTime'); + $cacheEnd = $this->getConfig('cacheTime', '+1 hour'); } $response = $response->withCache($modifiedTime, $cacheEnd); diff --git a/src/Shell/CacheShell.php b/src/Shell/CacheShell.php index f5fac85..e1bb20d 100644 --- a/src/Shell/CacheShell.php +++ b/src/Shell/CacheShell.php @@ -52,6 +52,7 @@ protected function fileStatus($url) { $count = iterator_count($fi); $this->out($count . ' cache files found.'); if ($this->param('verbose')) { + /** @var \SplFileInfo $f */ foreach ($fi as $f) { $this->out(' - ' . $f->getFileName()); } diff --git a/src/Utility/FileCache.php b/src/Utility/FileCache.php index 9fcfb43..26a5d3c 100644 --- a/src/Utility/FileCache.php +++ b/src/Utility/FileCache.php @@ -52,7 +52,7 @@ public function getContent($url) { return null; } - return file_get_contents($file); + return file_get_contents($file) ?: null; } return Cache::read($cacheKey, $engine) ?: null; diff --git a/tests/phpstan.neon b/tests/phpstan.neon index 6bdca8e..8afb997 100644 --- a/tests/phpstan.neon +++ b/tests/phpstan.neon @@ -1,6 +1,11 @@ parameters: - autoload_files: + level: 7 + + checkMissingIterableValueType: false + + bootstrapFiles: - %rootDir%/../../../tests/bootstrap.php - %rootDir%/../../../tests/shim.php ignoreErrors: - '#Parameter \#1 \$function of function call_user_func expects callable\(\): mixed, mixed given.#' + - '#Cannot cast array\|string\|null to string#'