Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 5 additions & 11 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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();
Expand All @@ -202,6 +204,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$insteadOfFile,
true,
deferBootstrapFiles: true,
errorFormat: $errorFormat,
);
} catch (InceptionNotSuccessfulException) {
return 1;
Expand Down Expand Up @@ -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(
Expand Down
3 changes: 2 additions & 1 deletion src/Command/CommandHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)));
Expand Down Expand Up @@ -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('<error>Invalid configuration:</error>');
$errorOutput->writeLineFormatted($e->getMessage());
Expand Down
9 changes: 7 additions & 2 deletions src/DependencyInjection/ContainerFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@ public function create(
?string $singleReflectionFile = null,
?string $singleReflectionInsteadOfFile = null,
array $additionalParameters = [],
?string $errorFormat = null,
): Container
{
[$allConfigFiles, $projectConfig] = $this->detectDuplicateIncludedFiles(
Expand Down Expand Up @@ -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);
Expand Down
49 changes: 49 additions & 0 deletions tests/PHPStan/Command/CommandHelperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'));
}

}
Loading