Skip to content

Commit 96e1c27

Browse files
committed
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.
1 parent 0c740e3 commit 96e1c27

File tree

3 files changed

+14
-14
lines changed

3 files changed

+14
-14
lines changed

src/Command/AnalyseCommand.php

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
161161

162162
$tmpFile = $input->getOption('tmp-file');
163163
$insteadOfFile = $input->getOption('instead-of');
164+
$errorFormat = $input->getOption('error-format');
164165

165166
if (
166167
!is_array($paths)
@@ -170,6 +171,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
170171
|| (!is_string($level) && $level !== null)
171172
|| (!is_string($tmpFile) && $tmpFile !== null)
172173
|| (!is_string($insteadOfFile) && $insteadOfFile !== null)
174+
|| (!is_string($errorFormat) && $errorFormat !== null)
173175
|| (!is_bool($allowXdebug))
174176
) {
175177
throw new ShouldNotHappenException();
@@ -191,6 +193,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
191193
$tmpFile,
192194
$insteadOfFile,
193195
true,
196+
$errorFormat,
194197
);
195198
} catch (InceptionNotSuccessfulException $e) {
196199
return 1;
@@ -226,21 +229,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
226229
}
227230

228231
$errorOutput = $inceptionResult->getErrorOutput();
229-
$errorFormat = $input->getOption('error-format');
230-
231-
if (!is_string($errorFormat) && $errorFormat !== null) {
232-
throw new ShouldNotHappenException();
233-
}
234-
235-
if ($errorFormat === null) {
236-
$errorFormat = $inceptionResult->getContainer()->getParameter('errorFormat');
237-
}
232+
$container = $inceptionResult->getContainer();
238233

234+
$errorFormat = $container->getParameter('errorFormat');
239235
if ($errorFormat === null) {
240236
$errorFormat = 'table';
241237
}
242-
243-
$container = $inceptionResult->getContainer();
244238
$errorFormatterServiceName = sprintf('errorFormatter.%s', $errorFormat);
245239
if (!$container->hasService($errorFormatterServiceName)) {
246240
$errorOutput->writeLineFormatted(sprintf(

src/Command/CommandHelper.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ public static function begin(
9595
?string $singleReflectionFile,
9696
?string $singleReflectionInsteadOfFile,
9797
bool $cleanupContainerCache,
98+
?string $errorFormat = null,
9899
): InceptionResult
99100
{
100101
$stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output)));
@@ -386,7 +387,7 @@ public static function begin(
386387
}
387388

388389
try {
389-
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile, $singleReflectionInsteadOfFile);
390+
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile, $singleReflectionInsteadOfFile, $errorFormat);
390391
} catch (InvalidConfigurationException | AssertionException $e) {
391392
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
392393
$errorOutput->writeLineFormatted($e->getMessage());

src/DependencyInjection/ContainerFactory.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ public function create(
109109
?string $cliAutoloadFile = null,
110110
?string $singleReflectionFile = null,
111111
?string $singleReflectionInsteadOfFile = null,
112+
?string $errorFormat = null,
112113
): Container
113114
{
114115
[$allConfigFiles, $projectConfig] = $this->detectDuplicateIncludedFiles(
@@ -146,12 +147,16 @@ public function create(
146147
'cliAutoloadFile' => $cliAutoloadFile,
147148
'env' => getenv(),
148149
]);
149-
$configurator->addDynamicParameters([
150+
$dynamicParameters = [
150151
'singleReflectionFile' => $singleReflectionFile,
151152
'singleReflectionInsteadOfFile' => $singleReflectionInsteadOfFile,
152153
'analysedPaths' => $analysedPaths,
153154
'analysedPathsFromConfig' => $analysedPathsFromConfig,
154-
]);
155+
];
156+
if ($errorFormat !== null) {
157+
$dynamicParameters['errorFormat'] = $errorFormat;
158+
}
159+
$configurator->addDynamicParameters($dynamicParameters);
155160
$configurator->addConfig($this->configDirectory . '/config.neon');
156161
foreach ($additionalConfigFiles as $additionalConfigFile) {
157162
$configurator->addConfig($additionalConfigFile);

0 commit comments

Comments
 (0)