Skip to content

Commit 4f75ba6

Browse files
committed
Do not output some internal errors twice
1 parent 4c4695e commit 4f75ba6

File tree

1 file changed

+30
-15
lines changed

1 file changed

+30
-15
lines changed

src/Command/AnalyseCommand.php

+30-15
Original file line numberDiff line numberDiff line change
@@ -330,45 +330,62 @@ protected function execute(InputInterface $input, OutputInterface $output): int
330330
throw $t;
331331
}
332332

333-
$internalErrors = [];
333+
/**
334+
* Variable $internalErrorsTuples contains both "internal errors"
335+
* and "errors with non-ignorable exception" as InternalError objects.
336+
*/
337+
$internalErrorsTuples = [];
334338
$internalFileSpecificErrors = [];
335339
foreach ($analysisResult->getInternalErrorObjects() as $internalError) {
336-
$internalErrors[$internalError->getMessage()] = new InternalError(
340+
$internalErrorsTuples[$internalError->getMessage()] = [new InternalError(
337341
$internalError->getTraceAsString() !== null ? sprintf('Internal error: %s', $internalError->getMessage()) : $internalError->getMessage(),
338342
$internalError->getContextDescription(),
339343
$internalError->getTrace(),
340344
$internalError->getTraceAsString(),
341345
$internalError->shouldReportBug(),
342-
);
346+
), false];
343347
}
344348
foreach ($analysisResult->getFileSpecificErrors() as $fileSpecificError) {
345349
if (!$fileSpecificError->hasNonIgnorableException()) {
346350
continue;
347351
}
348352

349-
$internalFileSpecificErrors[] = $fileSpecificError;
350-
351353
$message = $fileSpecificError->getMessage();
352354
$metadata = $fileSpecificError->getMetadata();
355+
$hasStackTrace = false;
353356
if (
354357
$fileSpecificError->getIdentifier() === 'phpstan.internal'
355358
&& array_key_exists(InternalError::STACK_TRACE_AS_STRING_METADATA_KEY, $metadata)
356359
) {
357360
$message = sprintf('Internal error: %s', $message);
361+
$hasStackTrace = true;
358362
}
359363

360-
$internalErrors[$fileSpecificError->getMessage()] = new InternalError(
364+
if (!$hasStackTrace) {
365+
$internalFileSpecificErrors[] = $fileSpecificError;
366+
}
367+
368+
$internalErrorsTuples[$fileSpecificError->getMessage()] = [new InternalError(
361369
$message,
362370
sprintf('analysing file %s', $fileSpecificError->getTraitFilePath() ?? $fileSpecificError->getFilePath()),
363371
$metadata[InternalError::STACK_TRACE_METADATA_KEY] ?? [],
364372
$metadata[InternalError::STACK_TRACE_AS_STRING_METADATA_KEY] ?? null,
365373
true,
366-
);
374+
), !$hasStackTrace];
367375
}
368376

369-
$internalErrors = array_values($internalErrors);
377+
$internalErrorsTuples = array_values($internalErrorsTuples);
370378
$bugReportUrl = 'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml';
371-
foreach ($internalErrors as $i => $internalError) {
379+
380+
/**
381+
* Variable $internalErrors only contains non-file-specific "internal errors".
382+
*/
383+
$internalErrors = [];
384+
foreach ($internalErrorsTuples as [$internalError, $isInFileSpecificErrors]) {
385+
if ($isInFileSpecificErrors) {
386+
continue;
387+
}
388+
372389
$message = sprintf('%s while %s', $internalError->getMessage(), $internalError->getContextDescription());
373390
if ($internalError->getTraceAsString() !== null) {
374391
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
@@ -393,7 +410,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
393410
}
394411
}
395412

396-
$internalErrors[$i] = new InternalError(
413+
$internalErrors[] = new InternalError(
397414
$message,
398415
$internalError->getContextDescription(),
399416
$internalError->getTrace(),
@@ -402,11 +419,9 @@ protected function execute(InputInterface $input, OutputInterface $output): int
402419
);
403420
}
404421

405-
$internalErrors = array_values($internalErrors);
406-
407422
if ($generateBaselineFile !== null) {
408-
if (count($internalErrors) > 0) {
409-
foreach ($internalErrors as $internalError) {
423+
if (count($internalErrorsTuples) > 0) {
424+
foreach ($internalErrorsTuples as [$internalError]) {
410425
$inceptionResult->getStdOutput()->writeLineFormatted($internalError->getMessage());
411426
$inceptionResult->getStdOutput()->writeLineFormatted('');
412427
}
@@ -425,7 +440,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
425440
/** @var ErrorFormatter $errorFormatter */
426441
$errorFormatter = $container->getService($errorFormatterServiceName);
427442

428-
if (count($internalErrors) > 0) {
443+
if (count($internalErrorsTuples) > 0) {
429444
$analysisResult = new AnalysisResult(
430445
$internalFileSpecificErrors,
431446
array_map(static fn (InternalError $internalError) => $internalError->getMessage(), $internalErrors),

0 commit comments

Comments
 (0)