Skip to content

Commit

Permalink
Extract getMessageFromInternalError
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrejmirtes committed Sep 12, 2024
1 parent 31f6737 commit d047c7f
Showing 1 changed file with 31 additions and 26 deletions.
57 changes: 31 additions & 26 deletions src/Command/AnalyseCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,6 @@ protected function execute(InputInterface $input, OutputInterface $output): int
}

$internalErrorsTuples = array_values($internalErrorsTuples);
$bugReportUrl = 'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml';

/**
* Variable $internalErrors only contains non-file-specific "internal errors".
Expand All @@ -396,32 +395,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
continue;
}

$message = sprintf('%s while %s', $internalError->getMessage(), $internalError->getContextDescription());
if ($internalError->getTraceAsString() !== null) {
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
$firstTraceItem = $internalError->getTrace()[0] ?? null;
$trace = '';
if ($firstTraceItem !== null && $firstTraceItem['file'] !== null && $firstTraceItem['line'] !== null) {
$trace = sprintf('## %s(%d)%s', $firstTraceItem['file'], $firstTraceItem['line'], "\n");
}
$trace .= $internalError->getTraceAsString();

if ($internalError->shouldReportBug()) {
$message .= sprintf('%sPost the following stack trace to %s: %s%s', "\n", $bugReportUrl, "\n", $trace);
} else {
$message .= sprintf('%s%s', "\n\n", $trace);
}
} else {
if ($internalError->shouldReportBug()) {
$message .= sprintf('%sRun PHPStan with -v option and post the stack trace to:%s%s%s', "\n\n", "\n", $bugReportUrl, "\n");
} else {
$message .= sprintf('%sRun PHPStan with -v option to see the stack trace', "\n");
}
}
}

$internalErrors[] = new InternalError(
$message,
$this->getMessageFromInternalError($internalError, $output->getVerbosity()),
$internalError->getContextDescription(),
$internalError->getTrace(),
$internalError->getTraceAsString(),
Expand Down Expand Up @@ -555,6 +530,36 @@ private function createStreamOutput(): StreamOutput
return new StreamOutput($resource);
}

private function getMessageFromInternalError(InternalError $internalError, int $verbosity): string
{
$bugReportUrl = 'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml';
$message = sprintf('%s while %s', $internalError->getMessage(), $internalError->getContextDescription());
if ($internalError->getTraceAsString() !== null) {
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
$firstTraceItem = $internalError->getTrace()[0] ?? null;
$trace = '';
if ($firstTraceItem !== null && $firstTraceItem['file'] !== null && $firstTraceItem['line'] !== null) {
$trace = sprintf('## %s(%d)%s', $firstTraceItem['file'], $firstTraceItem['line'], "\n");
}
$trace .= $internalError->getTraceAsString();

if ($internalError->shouldReportBug()) {
$message .= sprintf('%sPost the following stack trace to %s: %s%s', "\n", $bugReportUrl, "\n", $trace);
} else {
$message .= sprintf('%s%s', "\n\n", $trace);
}
} else {
if ($internalError->shouldReportBug()) {
$message .= sprintf('%sRun PHPStan with -v option and post the stack trace to:%s%s%s', "\n\n", "\n", $bugReportUrl, "\n");
} else {
$message .= sprintf('%sRun PHPStan with -v option to see the stack trace', "\n");
}
}
}

return $message;
}

private function generateBaseline(string $generateBaselineFile, InceptionResult $inceptionResult, AnalysisResult $analysisResult, OutputInterface $output, bool $allowEmptyBaseline, string $baselineExtension, bool $failWithoutResultCache): int
{
if (!$allowEmptyBaseline && !$analysisResult->hasErrors()) {
Expand Down

0 comments on commit d047c7f

Please sign in to comment.