diff --git a/src/Command/AnalyseCommand.php b/src/Command/AnalyseCommand.php index b8a93670725..58d6cfa5954 100644 --- a/src/Command/AnalyseCommand.php +++ b/src/Command/AnalyseCommand.php @@ -171,6 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $tmpFile = $input->getOption('tmp-file'); $insteadOfFile = $input->getOption('instead-of'); + $errorFormat = $input->getOption('error-format'); if ( !is_array($paths) @@ -180,6 +181,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int || (!is_string($level) && $level !== null) || (!is_string($tmpFile) && $tmpFile !== null) || (!is_string($insteadOfFile) && $insteadOfFile !== null) + || (!is_string($errorFormat) && $errorFormat !== null) || (!is_bool($allowXdebug)) ) { throw new ShouldNotHappenException(); @@ -202,6 +204,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $insteadOfFile, true, deferBootstrapFiles: true, + errorFormat: $errorFormat, ); } catch (InceptionNotSuccessfulException) { return 1; @@ -237,21 +240,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int } $errorOutput = $inceptionResult->getErrorOutput(); - $errorFormat = $input->getOption('error-format'); - - if (!is_string($errorFormat) && $errorFormat !== null) { - throw new ShouldNotHappenException(); - } - - if ($errorFormat === null) { - $errorFormat = $inceptionResult->getContainer()->getParameter('errorFormat'); - } + $container = $inceptionResult->getContainer(); + $errorFormat = $container->getParameter('errorFormat'); if ($errorFormat === null) { $errorFormat = 'table'; } - - $container = $inceptionResult->getContainer(); $errorFormatterServiceName = sprintf('errorFormatter.%s', $errorFormat); if (!$container->hasService($errorFormatterServiceName)) { $errorOutput->writeLineFormatted(sprintf( diff --git a/src/Command/CommandHelper.php b/src/Command/CommandHelper.php index 801dbe4939e..9c85e5e1fed 100644 --- a/src/Command/CommandHelper.php +++ b/src/Command/CommandHelper.php @@ -98,6 +98,7 @@ public static function begin( ?string $singleReflectionInsteadOfFile, bool $cleanupContainerCache, bool $deferBootstrapFiles = false, + ?string $errorFormat = null, ): InceptionResult { $stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output))); @@ -389,7 +390,7 @@ public static function begin( } try { - $container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile, $singleReflectionInsteadOfFile); + $container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile, $singleReflectionInsteadOfFile, [], $errorFormat); } catch (InvalidConfigurationException | AssertionException $e) { $errorOutput->writeLineFormatted('Invalid configuration:'); $errorOutput->writeLineFormatted($e->getMessage()); diff --git a/src/DependencyInjection/ContainerFactory.php b/src/DependencyInjection/ContainerFactory.php index 653e493b906..7248458eea3 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -114,6 +114,7 @@ public function create( ?string $singleReflectionFile = null, ?string $singleReflectionInsteadOfFile = null, array $additionalParameters = [], + ?string $errorFormat = null, ): Container { [$allConfigFiles, $projectConfig] = $this->detectDuplicateIncludedFiles( @@ -152,12 +153,16 @@ public function create( 'cliAutoloadFile' => $cliAutoloadFile, 'env' => Environment::getCleanedArray(), ], $additionalParameters)); - $configurator->addDynamicParameters([ + $dynamicParameters = [ 'singleReflectionFile' => $singleReflectionFile, 'singleReflectionInsteadOfFile' => $singleReflectionInsteadOfFile, 'analysedPaths' => $analysedPaths, 'analysedPathsFromConfig' => $analysedPathsFromConfig, - ]); + ]; + if ($errorFormat !== null) { + $dynamicParameters['errorFormat'] = $errorFormat; + } + $configurator->addDynamicParameters($dynamicParameters); $configurator->addConfig($this->configDirectory . '/config.neon'); foreach ($additionalConfigFiles as $additionalConfigFile) { $configurator->addConfig($additionalConfigFile); diff --git a/tests/PHPStan/Command/CommandHelperTest.php b/tests/PHPStan/Command/CommandHelperTest.php index c2795c5b4da..35992ed9233 100644 --- a/tests/PHPStan/Command/CommandHelperTest.php +++ b/tests/PHPStan/Command/CommandHelperTest.php @@ -393,4 +393,53 @@ public function testAutoloadFileAndConfigurationAreNormalized(): void $this->assertContains($relativePaths . 'root.neon', $parameters['additionalConfigFiles']); } + /** + * @throws InceptionNotSuccessfulException + */ + public function testErrorFormatFromCli(): void + { + $result = CommandHelper::begin( + new StringInput(''), + new NullOutput(), + [__DIR__], + null, + null, + [], + null, + null, + '0', + false, + false, + null, + null, + false, + errorFormat: 'json', + ); + $this->assertSame('json', $result->getContainer()->getParameter('errorFormat')); + } + + /** + * @throws InceptionNotSuccessfulException + */ + public function testErrorFormatDefault(): void + { + $result = CommandHelper::begin( + new StringInput(''), + new NullOutput(), + [__DIR__], + null, + null, + [], + null, + null, + '0', + false, + false, + null, + null, + false, + ); + $this->assertNull($result->getContainer()->getParameter('errorFormat')); + } + }