Skip to content

Commit c2f17a1

Browse files
janedbalondrejmirtes
authored andcommitted
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 091c5a7 commit c2f17a1

3 files changed

Lines changed: 14 additions & 14 deletions

File tree

‎src/Command/AnalyseCommand.php‎

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

172172
$tmpFile = $input->getOption('tmp-file');
173173
$insteadOfFile = $input->getOption('instead-of');
174+
$errorFormat = $input->getOption('error-format');
174175

175176
if (
176177
!is_array($paths)
@@ -180,6 +181,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
180181
|| (!is_string($level) && $level !== null)
181182
|| (!is_string($tmpFile) && $tmpFile !== null)
182183
|| (!is_string($insteadOfFile) && $insteadOfFile !== null)
184+
|| (!is_string($errorFormat) && $errorFormat !== null)
183185
|| (!is_bool($allowXdebug))
184186
) {
185187
throw new ShouldNotHappenException();
@@ -202,6 +204,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
202204
$insteadOfFile,
203205
true,
204206
deferBootstrapFiles: true,
207+
errorFormat: $errorFormat,
205208
);
206209
} catch (InceptionNotSuccessfulException) {
207210
return 1;
@@ -237,21 +240,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
237240
}
238241

239242
$errorOutput = $inceptionResult->getErrorOutput();
240-
$errorFormat = $input->getOption('error-format');
241-
242-
if (!is_string($errorFormat) && $errorFormat !== null) {
243-
throw new ShouldNotHappenException();
244-
}
245-
246-
if ($errorFormat === null) {
247-
$errorFormat = $inceptionResult->getContainer()->getParameter('errorFormat');
248-
}
243+
$container = $inceptionResult->getContainer();
249244

245+
$errorFormat = $container->getParameter('errorFormat');
250246
if ($errorFormat === null) {
251247
$errorFormat = 'table';
252248
}
253-
254-
$container = $inceptionResult->getContainer();
255249
$errorFormatterServiceName = sprintf('errorFormatter.%s', $errorFormat);
256250
if (!$container->hasService($errorFormatterServiceName)) {
257251
$errorOutput->writeLineFormatted(sprintf(

‎src/Command/CommandHelper.php‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ public static function begin(
9898
?string $singleReflectionInsteadOfFile,
9999
bool $cleanupContainerCache,
100100
bool $deferBootstrapFiles = false,
101+
?string $errorFormat = null,
101102
): InceptionResult
102103
{
103104
$stdOutput = new SymfonyOutput($output, new SymfonyStyle(new ErrorsConsoleStyle($input, $output)));
@@ -389,7 +390,7 @@ public static function begin(
389390
}
390391

391392
try {
392-
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile, $singleReflectionInsteadOfFile);
393+
$container = $containerFactory->create($tmpDir, $additionalConfigFiles, $paths, $composerAutoloaderProjectPaths, $analysedPathsFromConfig, $level ?? self::DEFAULT_LEVEL, $generateBaselineFile, $autoloadFile, $singleReflectionFile, $singleReflectionInsteadOfFile, [], $errorFormat);
393394
} catch (InvalidConfigurationException | AssertionException $e) {
394395
$errorOutput->writeLineFormatted('<error>Invalid configuration:</error>');
395396
$errorOutput->writeLineFormatted($e->getMessage());

‎src/DependencyInjection/ContainerFactory.php‎

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ public function create(
114114
?string $singleReflectionFile = null,
115115
?string $singleReflectionInsteadOfFile = null,
116116
array $additionalParameters = [],
117+
?string $errorFormat = null,
117118
): Container
118119
{
119120
[$allConfigFiles, $projectConfig] = $this->detectDuplicateIncludedFiles(
@@ -152,12 +153,16 @@ public function create(
152153
'cliAutoloadFile' => $cliAutoloadFile,
153154
'env' => Environment::getCleanedArray(),
154155
], $additionalParameters));
155-
$configurator->addDynamicParameters([
156+
$dynamicParameters = [
156157
'singleReflectionFile' => $singleReflectionFile,
157158
'singleReflectionInsteadOfFile' => $singleReflectionInsteadOfFile,
158159
'analysedPaths' => $analysedPaths,
159160
'analysedPathsFromConfig' => $analysedPathsFromConfig,
160-
]);
161+
];
162+
if ($errorFormat !== null) {
163+
$dynamicParameters['errorFormat'] = $errorFormat;
164+
}
165+
$configurator->addDynamicParameters($dynamicParameters);
161166
$configurator->addConfig($this->configDirectory . '/config.neon');
162167
foreach ($additionalConfigFiles as $additionalConfigFile) {
163168
$configurator->addConfig($additionalConfigFile);

0 commit comments

Comments
 (0)