From 5170937fe449f39334b01afafdd11542c13bdfca Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 4 Aug 2021 16:10:39 +0200 Subject: [PATCH 1/7] phpstan fixes --- composer.json | 3 ++- lib/Application.php | 9 ++++----- lib/Baseline.php | 7 +++++-- 3 files changed, 11 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 2cf6410..2dcb95d 100644 --- a/composer.json +++ b/composer.json @@ -12,7 +12,8 @@ "require": { "php": ">=7.4 || ^8.0", "nette/neon": "^3.2", - "symfony/polyfill-php80": "^1.23" + "symfony/polyfill-php80": "^1.23", + "thecodingmachine/safe": "^1.3" }, "require-dev": { "phpunit/phpunit": "^9.5", diff --git a/lib/Application.php b/lib/Application.php index 59655a1..7880c72 100644 --- a/lib/Application.php +++ b/lib/Application.php @@ -2,15 +2,14 @@ namespace staabm\PHPStanBaselineAnalysis; -use Nette\Neon\Neon; +final class Application +{ -final class Application { - - public function start(string $glob) + public function start(string $glob): void { $baselines = BaselineFinder::forGlob($glob); - foreach($baselines as $baseline) { + foreach ($baselines as $baseline) { printf("Analyzing %s\n", $baseline->getFilePath()); $analyzer = new BaselineAnalyzer($baseline); diff --git a/lib/Baseline.php b/lib/Baseline.php index 79910e0..dc33496 100644 --- a/lib/Baseline.php +++ b/lib/Baseline.php @@ -5,6 +5,7 @@ use Iterator; use Nette\Neon\Neon; use RuntimeException; +use Safe\Exceptions\FilesystemException; final class Baseline { /** @@ -17,9 +18,11 @@ final class Baseline { */ private $filePath; + /** + * @throws FilesystemException + */ static public function forFile(string $filePath):self { - - $content = file_get_contents($filePath); + $content = \Safe\file_get_contents($filePath); $decoded = Neon::decode($content); if (!is_array($decoded)) { From 5dd96da4d9ac50aa8fc892c092abb6cbbafda93b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 4 Aug 2021 16:16:54 +0200 Subject: [PATCH 2/7] phpdocs --- composer.json | 5 +++-- lib/Baseline.php | 5 +---- lib/BaselineAnalyzer.php | 3 +++ lib/BaselineFinder.php | 8 ++++++-- phpstan.neon.dist | 3 +++ 5 files changed, 16 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 2dcb95d..84d1acf 100644 --- a/composer.json +++ b/composer.json @@ -16,9 +16,10 @@ "thecodingmachine/safe": "^1.3" }, "require-dev": { - "phpunit/phpunit": "^9.5", "phpstan/phpstan": "^0.12", - "symfony/var-dumper": "^5.3" + "phpunit/phpunit": "^9.5", + "symfony/var-dumper": "^5.3", + "thecodingmachine/phpstan-safe-rule": "^1.0" }, "config": { "optimize-autoloader": true, diff --git a/lib/Baseline.php b/lib/Baseline.php index dc33496..875bafd 100644 --- a/lib/Baseline.php +++ b/lib/Baseline.php @@ -9,7 +9,7 @@ final class Baseline { /** - * @var array + * @var array{parameters?: array{ignoreErrors?: array{message: string, count: int, path: string}}} */ private $content; @@ -49,9 +49,6 @@ public function getIgnoreErrors(): Iterator { } $ignoreErrors = $parameters['ignoreErrors']; - /** - * @var array{message: string, count: int, path: string} $error - */ foreach($ignoreErrors as $error) { yield $error['message']; } diff --git a/lib/BaselineAnalyzer.php b/lib/BaselineAnalyzer.php index 37775b2..c5cfcdb 100644 --- a/lib/BaselineAnalyzer.php +++ b/lib/BaselineAnalyzer.php @@ -3,6 +3,9 @@ namespace staabm\PHPStanBaselineAnalysis; final class BaselineAnalyzer { + /** + * @var Baseline + */ private $baseline; public function __construct(Baseline $baseline) { diff --git a/lib/BaselineFinder.php b/lib/BaselineFinder.php index 04a9fd9..ed6b1e4 100644 --- a/lib/BaselineFinder.php +++ b/lib/BaselineFinder.php @@ -28,8 +28,12 @@ static public function forGlob(string $glob): array return $baselines; } - // from https://stackoverflow.com/a/17161106 - static private function rglob($pattern, $flags = 0) + /** + * from https://stackoverflow.com/a/17161106 + * + * @return string[] + */ + static private function rglob(string $pattern,int $flags = 0):array { $files = glob($pattern, $flags); foreach (glob(dirname($pattern) . '/*', GLOB_ONLYDIR | GLOB_NOSORT) as $dir) { diff --git a/phpstan.neon.dist b/phpstan.neon.dist index e739b1b..c2334f0 100644 --- a/phpstan.neon.dist +++ b/phpstan.neon.dist @@ -1,3 +1,6 @@ +includes: + - vendor/thecodingmachine/phpstan-safe-rule/phpstan-safe-rule.neon + parameters: level: max From 3a308083bebbd9f7947b2f5f2f552ab482473949 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 4 Aug 2021 16:18:02 +0200 Subject: [PATCH 3/7] Update BaselineFinder.php --- lib/BaselineFinder.php | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/BaselineFinder.php b/lib/BaselineFinder.php index ed6b1e4..206fadb 100644 --- a/lib/BaselineFinder.php +++ b/lib/BaselineFinder.php @@ -2,12 +2,15 @@ namespace staabm\PHPStanBaselineAnalysis; -use Nette\Neon\Neon; +use Safe\Exceptions\FilesystemException; +use function Safe\glob; final class BaselineFinder { /** * @return Baseline[] + * + * @throws FilesystemException */ static public function forGlob(string $glob): array { @@ -32,6 +35,8 @@ static public function forGlob(string $glob): array * from https://stackoverflow.com/a/17161106 * * @return string[] + * + * @throws FilesystemException */ static private function rglob(string $pattern,int $flags = 0):array { From 1f68e94f7b03084f7cbdf6463ebf37d11143a0c8 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 4 Aug 2021 16:19:53 +0200 Subject: [PATCH 4/7] fixes --- lib/Baseline.php | 1 + lib/BaselineAnalyzer.php | 2 ++ 2 files changed, 3 insertions(+) diff --git a/lib/Baseline.php b/lib/Baseline.php index 875bafd..aa127c9 100644 --- a/lib/Baseline.php +++ b/lib/Baseline.php @@ -6,6 +6,7 @@ use Nette\Neon\Neon; use RuntimeException; use Safe\Exceptions\FilesystemException; +use function Safe\sprintf; final class Baseline { /** diff --git a/lib/BaselineAnalyzer.php b/lib/BaselineAnalyzer.php index c5cfcdb..e1b88db 100644 --- a/lib/BaselineAnalyzer.php +++ b/lib/BaselineAnalyzer.php @@ -2,6 +2,8 @@ namespace staabm\PHPStanBaselineAnalysis; +use function Safe\preg_match + final class BaselineAnalyzer { /** * @var Baseline From 1468931504473fe0e7eb72f61e30ec9dcfb8b21e Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 4 Aug 2021 16:21:02 +0200 Subject: [PATCH 5/7] fix types --- lib/Baseline.php | 2 +- lib/BaselineAnalyzer.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/Baseline.php b/lib/Baseline.php index aa127c9..fe6d634 100644 --- a/lib/Baseline.php +++ b/lib/Baseline.php @@ -10,7 +10,7 @@ final class Baseline { /** - * @var array{parameters?: array{ignoreErrors?: array{message: string, count: int, path: string}}} + * @var array{parameters?: array{ignoreErrors?: list}} */ private $content; diff --git a/lib/BaselineAnalyzer.php b/lib/BaselineAnalyzer.php index e1b88db..e0e8b0c 100644 --- a/lib/BaselineAnalyzer.php +++ b/lib/BaselineAnalyzer.php @@ -2,7 +2,7 @@ namespace staabm\PHPStanBaselineAnalysis; -use function Safe\preg_match +use function Safe\preg_match; final class BaselineAnalyzer { /** From 72d3f37d4ac143fa9868df06d17cc726ad5c0809 Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 4 Aug 2021 16:28:33 +0200 Subject: [PATCH 6/7] added help command --- bin/phpstan-baseline-analyze | 4 ++++ bin/phpstan-baseline-analyze.php | 10 ++++++++-- composer.json | 2 +- lib/Application.php | 5 +++++ 4 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 bin/phpstan-baseline-analyze diff --git a/bin/phpstan-baseline-analyze b/bin/phpstan-baseline-analyze new file mode 100644 index 0000000..6c16832 --- /dev/null +++ b/bin/phpstan-baseline-analyze @@ -0,0 +1,4 @@ +#!/usr/bin/env php +start($argv[1]); \ No newline at end of file + +if ($argc <= 1) { + $app->help(); + exit(1); +} + +$app->start($argv[1]); +exit(0); \ No newline at end of file diff --git a/composer.json b/composer.json index 84d1acf..1390a3f 100644 --- a/composer.json +++ b/composer.json @@ -30,6 +30,6 @@ "phpunit": "phpunit" }, "bin": [ - "bin/phpstan-baseline-analyze.php" + "bin/phpstan-baseline-analyze" ] } diff --git a/lib/Application.php b/lib/Application.php index 7880c72..2c4c0d4 100644 --- a/lib/Application.php +++ b/lib/Application.php @@ -18,4 +18,9 @@ public function start(string $glob): void printf(" Overall-Complexity: %s\n", $result->overallComplexity); } } + + public function help() + { + printf('USAGE: phpstan-baseline-analyze '); + } } From a73923df970079da94dc2d8339a1069e19a1343b Mon Sep 17 00:00:00 2001 From: Markus Staab Date: Wed, 4 Aug 2021 16:29:49 +0200 Subject: [PATCH 7/7] phpstan --- lib/Application.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/Application.php b/lib/Application.php index 2c4c0d4..adef674 100644 --- a/lib/Application.php +++ b/lib/Application.php @@ -19,7 +19,7 @@ public function start(string $glob): void } } - public function help() + public function help(): void { printf('USAGE: phpstan-baseline-analyze '); }