From 96e1c27624e4f001682cf92ca6ecf36b2da28f7d Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Wed, 4 Mar 2026 13:21:11 +0100 Subject: [PATCH 1/2] Pass CLI --error-format option into the DIC container parameter The errorFormat DIC parameter was not updated when the --error-format CLI option was used - it always held the config file value (default null). This meant reading the parameter from the container would not reflect the actually-used value. Thread the CLI error format through CommandHelper::begin() and ContainerFactory::create() as a dynamic parameter, so it properly overrides the config file value. Simplify the resolution in AnalyseCommand to just read from the container. --- src/Command/AnalyseCommand.php | 16 +++++----------- src/Command/CommandHelper.php | 3 ++- src/DependencyInjection/ContainerFactory.php | 9 +++++++-- 3 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Command/AnalyseCommand.php b/src/Command/AnalyseCommand.php index a6c0943d61..a978f9d1ce 100644 --- a/src/Command/AnalyseCommand.php +++ b/src/Command/AnalyseCommand.php @@ -161,6 +161,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) @@ -170,6 +171,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(); @@ -191,6 +193,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int $tmpFile, $insteadOfFile, true, + $errorFormat, ); } catch (InceptionNotSuccessfulException $e) { return 1; @@ -226,21 +229,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 612d1ed762..34f03622df 100644 --- a/src/Command/CommandHelper.php +++ b/src/Command/CommandHelper.php @@ -95,6 +95,7 @@ public static function begin( ?string $singleReflectionFile, ?string $singleReflectionInsteadOfFile, bool $cleanupContainerCache, + ?string $errorFormat = null, ): InceptionResult { $stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output))); @@ -386,7 +387,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 d2e163b837..a60c6b9beb 100644 --- a/src/DependencyInjection/ContainerFactory.php +++ b/src/DependencyInjection/ContainerFactory.php @@ -109,6 +109,7 @@ public function create( ?string $cliAutoloadFile = null, ?string $singleReflectionFile = null, ?string $singleReflectionInsteadOfFile = null, + ?string $errorFormat = null, ): Container { [$allConfigFiles, $projectConfig] = $this->detectDuplicateIncludedFiles( @@ -146,12 +147,16 @@ public function create( 'cliAutoloadFile' => $cliAutoloadFile, 'env' => getenv(), ]); - $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); From bbf6649bf2ecf5072ea909adb553f47f3cd04d48 Mon Sep 17 00:00:00 2001 From: Jan Nedbal Date: Wed, 4 Mar 2026 15:59:15 +0100 Subject: [PATCH 2/2] Add tests for errorFormat DIC parameter from CLI option --- tests/PHPStan/Command/CommandHelperTest.php | 43 +++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/tests/PHPStan/Command/CommandHelperTest.php b/tests/PHPStan/Command/CommandHelperTest.php index a9c2c8414e..8ce8b9bed0 100644 --- a/tests/PHPStan/Command/CommandHelperTest.php +++ b/tests/PHPStan/Command/CommandHelperTest.php @@ -319,4 +319,47 @@ public function testResolveParameters( } } + public function testErrorFormatFromCli(): void + { + $result = CommandHelper::begin( + new StringInput(''), + new NullOutput(), + [__DIR__], + null, + null, + [], + null, + null, + '0', + false, + false, + null, + null, + false, + 'json', + ); + $this->assertSame('json', $result->getContainer()->getParameter('errorFormat')); + } + + 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')); + } + }