Skip to content

Commit

Permalink
PHPStan Pro - present all errors with non-ignorable exceptions as int…
Browse files Browse the repository at this point in the history
…ernal errors
  • Loading branch information
ondrejmirtes committed Jun 17, 2024
1 parent 108277a commit 7d9800c
Showing 1 changed file with 37 additions and 16 deletions.
53 changes: 37 additions & 16 deletions src/Command/FixerWorkerCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,22 @@ protected function execute(InputInterface $input, OutputInterface $output): int
$configuration,
$input,
function (array $errors, array $locallyIgnoredErrors, array $analysedFiles) use ($out, $ignoredErrorHelperResult, $isOnlyFiles, $inceptionFiles): void {
$internalErrors = [];
foreach ($errors as $fileSpecificError) {
if (!$fileSpecificError->hasNonIgnorableException()) {
continue;
}

$internalErrors[] = $this->transformErrorIntoInternalError($fileSpecificError);
}

if (count($internalErrors) > 0) {
$out->write(['action' => 'analysisCrash', 'data' => [
'internalErrors' => $internalErrors,
]]);
return;
}

[$errors, $ignoredErrors] = $this->filterErrors($errors, $ignoredErrorHelperResult, $isOnlyFiles, $inceptionFiles, false);
foreach ($locallyIgnoredErrors as $locallyIgnoredError) {
$ignoredErrors[] = [$locallyIgnoredError, null];
Expand Down Expand Up @@ -249,22 +265,7 @@ function (array $errors, array $locallyIgnoredErrors, array $analysedFiles) use
continue;
}

$message = $fileSpecificError->getMessage();
$metadata = $fileSpecificError->getMetadata();
if (
$fileSpecificError->getIdentifier() === 'phpstan.internal'
&& array_key_exists(InternalError::STACK_TRACE_AS_STRING_METADATA_KEY, $metadata)
) {
$message = sprintf('Internal error: %s', $message);
}

$internalErrors[] = new InternalError(
$message,
sprintf('analysing file %s', $fileSpecificError->getTraitFilePath() ?? $fileSpecificError->getFilePath()),
$metadata[InternalError::STACK_TRACE_METADATA_KEY] ?? [],
$metadata[InternalError::STACK_TRACE_AS_STRING_METADATA_KEY] ?? null,
true,
);
$internalErrors[] = $this->transformErrorIntoInternalError($fileSpecificError);
}

$hasInternalErrors = count($internalErrors) > 0 || $finalizerResult->getAnalyserResult()->hasReachedInternalErrorsCountLimit();
Expand Down Expand Up @@ -327,6 +328,26 @@ function (array $errors, array $locallyIgnoredErrors, array $analysedFiles) use
return 0;
}

private function transformErrorIntoInternalError(Error $error): InternalError
{
$message = $error->getMessage();
$metadata = $error->getMetadata();
if (
$error->getIdentifier() === 'phpstan.internal'
&& array_key_exists(InternalError::STACK_TRACE_AS_STRING_METADATA_KEY, $metadata)
) {
$message = sprintf('Internal error: %s', $message);
}

return new InternalError(
$message,
sprintf('analysing file %s', $error->getTraitFilePath() ?? $error->getFilePath()),
$metadata[InternalError::STACK_TRACE_METADATA_KEY] ?? [],
$metadata[InternalError::STACK_TRACE_AS_STRING_METADATA_KEY] ?? null,
true,
);
}

/**
* @param string[] $inceptionFiles
* @param array<Error> $errors
Expand Down

0 comments on commit 7d9800c

Please sign in to comment.