diff --git a/src/Tools/Analyzer/Phpstan.php b/src/Tools/Analyzer/Phpstan.php index 09b09ca..1a63a90 100644 --- a/src/Tools/Analyzer/Phpstan.php +++ b/src/Tools/Analyzer/Phpstan.php @@ -28,23 +28,14 @@ function ($relativeDir) { ))); }; - $defaultConfig = $this->config->path('phpstan.standard') ?: (getcwd() . '/phpstan.neon'); - if (file_exists($defaultConfig)) { - $config = \Nette\Neon\Neon::decode(file_get_contents($defaultConfig)); - $config['parameters'] += [ - 'excludes_analyse' => [], - ]; - } else { - $config = [ - 'parameters' => [ - 'autoload_directories' => $createAbsolutePaths($this->options->getAnalyzedDirs()), - 'excludes_analyse' => [], - ], - ]; - } + $defaultConfigFile = $this->config->path('phpstan.standard') ?: (getcwd() . '/phpstan.neon'); + $existingConfig = file_exists($defaultConfigFile) + ? \Nette\Neon\Neon::decode(file_get_contents($defaultConfigFile)) + : []; - $config['parameters']['excludes_analyse'] = array_merge( - $config['parameters']['excludes_analyse'], + $config = self::buildConfig( + $existingConfig, + $createAbsolutePaths($this->options->getAnalyzedDirs()), $createAbsolutePaths($this->options->ignore->phpstan()) ); @@ -69,4 +60,45 @@ private function getErrorFormatOption() { return $this->toolVersionIs('<', '0.10.3') ? 'errorFormat' : 'error-format'; } + + public static function buildConfig($existingConfig, $autoloadDirectories, $ignoredPaths) + { + if ($existingConfig !== []) { + $config = $existingConfig + [ + 'parameters' => [], + ]; + } else { + $config = [ + 'parameters' => [ + 'autoload_directories' => $autoloadDirectories, + 'excludePaths' => [ + 'analyseAndScan' => [], + ], + ], + ]; + } + + if (isset($config['parameters']['excludePaths']['analyseAndScan'])) { + $config['parameters']['excludePaths']['analyseAndScan'] = array_merge( + $config['parameters']['excludePaths']['analyseAndScan'], + $ignoredPaths + ); + } elseif (isset($config['parameters']['excludePaths'])) { + $config['parameters']['excludePaths'] = array_merge( + $config['parameters']['excludePaths'], + $ignoredPaths + ); + } elseif (isset($config['parameters']['excludes_analyse'])) { + $config['parameters']['excludes_analyse'] = array_merge( + $config['parameters']['excludes_analyse'], + $ignoredPaths + ); + } else { + $config['parameters']['excludePaths'] = [ + 'analyseAndScan' => $ignoredPaths, + ]; + } + + return $config; + } } diff --git a/tests/.ci/phpstan.neon b/tests/.ci/phpstan.neon index 8cb4a9f..01e2ce3 100644 --- a/tests/.ci/phpstan.neon +++ b/tests/.ci/phpstan.neon @@ -1,11 +1,12 @@ parameters: reportUnmatchedIgnoredErrors: false # exclude robo v0/v1 compatibility classes - it's not possible to analyze them with phsptan (only one robo version is installed) - excludes_analyse: - - %currentWorkingDirectory%/src/Task/ParallelExec.php - - %currentWorkingDirectory%/src/Task/NonParallelExecV0.php - - %currentWorkingDirectory%/src/Task/NonParallelExecV1.php - - %currentWorkingDirectory%/src/Task/RoboAdapter.php + excludePaths: + analyseAndScan: + - %currentWorkingDirectory%/src/Task/ParallelExec.php + - %currentWorkingDirectory%/src/Task/NonParallelExecV0.php + - %currentWorkingDirectory%/src/Task/NonParallelExecV1.php + - %currentWorkingDirectory%/src/Task/RoboAdapter.php ignoreErrors: # constants from phpqa - message: '#Constant COMPOSER_(.+) not found.#' diff --git a/tests/Tools/Analyzer/PhpstanTest.php b/tests/Tools/Analyzer/PhpstanTest.php new file mode 100644 index 0000000..6bc2404 --- /dev/null +++ b/tests/Tools/Analyzer/PhpstanTest.php @@ -0,0 +1,106 @@ +autoloadDirectories, $this->ignoredPaths); + $this->assertEquals($expectedConfig, $config); + } + + public function provideConfig() + { + return [ + 'No config' => [ + 'existingConfig' => [], + 'expectedConfig' => [ + 'parameters' => [ + 'autoload_directories' => $this->autoloadDirectories, + 'excludePaths' => [ + 'analyseAndScan' => $this->ignoredPaths, + ], + ], + ], + ], + 'No parameters in config' => [ + 'existingConfig' => [ + 'includes' => $this->autoloadDirectories, + ], + 'expectedConfig' => [ + 'includes' => $this->autoloadDirectories, + 'parameters' => [ + 'excludePaths' => [ + 'analyseAndScan' => $this->ignoredPaths, + ], + ], + ], + ], + 'excludePaths shortcut' => [ + 'existingConfig' => [ + 'parameters' => [ + 'excludePaths' => [ + 'File.php', + ], + ], + ], + 'expectedConfig' => [ + 'parameters' => [ + 'excludePaths' => [ + 'File.php', + 'Test.php', + ], + ], + ], + ], + 'excludePaths + analyseAndScan' => [ + 'existingConfig' => [ + 'parameters' => [ + 'excludePaths' => [ + 'analyseAndScan' => [ + 'File.php', + ], + ], + ], + ], + 'expectedConfig' => [ + 'parameters' => [ + 'excludePaths' => [ + 'analyseAndScan' => [ + 'File.php', + 'Test.php', + ], + ], + ], + ], + ], + 'Deprecated excludes_analyse' => [ + 'existingConfig' => [ + 'parameters' => [ + 'reportUnmatchedIgnoredErrors' => false, + 'excludes_analyse' => [ + 'File.php', + ], + ], + ], + 'expectedConfig' => [ + 'parameters' => [ + 'reportUnmatchedIgnoredErrors' => false, + 'excludes_analyse' => [ + 'File.php', + 'Test.php', + ], + ], + ], + ], + ]; + } +}