Skip to content

Commit c28ee25

Browse files
authored
Merge branch refs/heads/1.12.x into 2.0.x
2 parents 14a0718 + 475a18c commit c28ee25

File tree

1 file changed

+80
-26
lines changed

1 file changed

+80
-26
lines changed

src/Command/AnalyseCommand.php

Lines changed: 80 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use PHPStan\Diagnose\DiagnoseExtension;
1414
use PHPStan\Diagnose\PHPStanDiagnoseExtension;
1515
use PHPStan\File\CouldNotWriteFileException;
16+
use PHPStan\File\FileHelper;
1617
use PHPStan\File\FileReader;
1718
use PHPStan\File\FileWriter;
1819
use PHPStan\File\ParentDirectoryRelativePathHelper;
@@ -34,6 +35,7 @@
3435
use function array_key_exists;
3536
use function array_keys;
3637
use function array_map;
38+
use function array_reverse;
3739
use function array_unique;
3840
use function array_values;
3941
use function count;
@@ -50,12 +52,16 @@
5052
use function pathinfo;
5153
use function rewind;
5254
use function sprintf;
55+
use function str_contains;
5356
use function stream_get_contents;
5457
use function strlen;
5558
use function substr;
5659
use const PATHINFO_BASENAME;
5760
use const PATHINFO_EXTENSION;
5861

62+
/**
63+
* @phpstan-import-type Trace from InternalError as InternalErrorTrace
64+
*/
5965
final class AnalyseCommand extends Command
6066
{
6167

@@ -385,7 +391,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
385391
}
386392

387393
$internalErrorsTuples = array_values($internalErrorsTuples);
388-
$bugReportUrl = 'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml';
394+
395+
$fileHelper = $container->getByType(FileHelper::class);
389396

390397
/**
391398
* Variable $internalErrors only contains non-file-specific "internal errors".
@@ -396,32 +403,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
396403
continue;
397404
}
398405

399-
$message = sprintf('%s while %s', $internalError->getMessage(), $internalError->getContextDescription());
400-
if ($internalError->getTraceAsString() !== null) {
401-
if (OutputInterface::VERBOSITY_VERBOSE <= $output->getVerbosity()) {
402-
$firstTraceItem = $internalError->getTrace()[0] ?? null;
403-
$trace = '';
404-
if ($firstTraceItem !== null && $firstTraceItem['file'] !== null && $firstTraceItem['line'] !== null) {
405-
$trace = sprintf('## %s(%d)%s', $firstTraceItem['file'], $firstTraceItem['line'], "\n");
406-
}
407-
$trace .= $internalError->getTraceAsString();
408-
409-
if ($internalError->shouldReportBug()) {
410-
$message .= sprintf('%sPost the following stack trace to %s: %s%s', "\n", $bugReportUrl, "\n", $trace);
411-
} else {
412-
$message .= sprintf('%s%s', "\n\n", $trace);
413-
}
414-
} else {
415-
if ($internalError->shouldReportBug()) {
416-
$message .= sprintf('%sRun PHPStan with -v option and post the stack trace to:%s%s%s', "\n\n", "\n", $bugReportUrl, "\n");
417-
} else {
418-
$message .= sprintf('%sRun PHPStan with -v option to see the stack trace', "\n");
419-
}
420-
}
421-
}
422-
423406
$internalErrors[] = new InternalError(
424-
$message,
407+
$this->getMessageFromInternalError($fileHelper, $internalError, $output->getVerbosity()),
425408
$internalError->getContextDescription(),
426409
$internalError->getTrace(),
427410
$internalError->getTraceAsString(),
@@ -555,6 +538,77 @@ private function createStreamOutput(): StreamOutput
555538
return new StreamOutput($resource);
556539
}
557540

541+
private function getMessageFromInternalError(FileHelper $fileHelper, InternalError $internalError, int $verbosity): string
542+
{
543+
$message = sprintf('%s while %s', $internalError->getMessage(), $internalError->getContextDescription());
544+
$hasLarastan = false;
545+
$isLaravelLast = false;
546+
547+
foreach (array_reverse($internalError->getTrace()) as $traceItem) {
548+
if ($traceItem['file'] === null) {
549+
continue;
550+
}
551+
552+
$file = $fileHelper->normalizePath($traceItem['file'], '/');
553+
554+
if (str_contains($file, '/larastan/')) {
555+
$hasLarastan = true;
556+
$isLaravelLast = false;
557+
continue;
558+
}
559+
560+
if (!str_contains($file, '/laravel/framework/')) {
561+
continue;
562+
}
563+
564+
$isLaravelLast = true;
565+
}
566+
if ($hasLarastan) {
567+
if ($isLaravelLast) {
568+
$message .= "\n";
569+
$message .= "\n" . 'This message is coming from Laravel Framework itself.';
570+
$message .= "\n" . 'Larastan boots up your application in order to provide';
571+
$message .= "\n" . 'smarter static analysis of your codebase.';
572+
$message .= "\n";
573+
$message .= "\n" . 'In order to do that, the environment you run PHPStan in';
574+
$message .= "\n" . 'must match the environment you run your application in.';
575+
$message .= "\n";
576+
$message .= "\n" . 'Make sure you\'ve set your environment variables';
577+
$message .= "\n" . 'or the .env file correctly.';
578+
579+
return $message;
580+
}
581+
582+
$bugReportUrl = 'https://github.com/larastan/larastan/issues/new?template=bug-report.md';
583+
} else {
584+
$bugReportUrl = 'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml';
585+
}
586+
if ($internalError->getTraceAsString() !== null) {
587+
if (OutputInterface::VERBOSITY_VERBOSE <= $verbosity) {
588+
$firstTraceItem = $internalError->getTrace()[0] ?? null;
589+
$trace = '';
590+
if ($firstTraceItem !== null && $firstTraceItem['file'] !== null && $firstTraceItem['line'] !== null) {
591+
$trace = sprintf('## %s(%d)%s', $firstTraceItem['file'], $firstTraceItem['line'], "\n");
592+
}
593+
$trace .= $internalError->getTraceAsString();
594+
595+
if ($internalError->shouldReportBug()) {
596+
$message .= sprintf('%sPost the following stack trace to %s: %s%s', "\n", $bugReportUrl, "\n", $trace);
597+
} else {
598+
$message .= sprintf('%s%s', "\n\n", $trace);
599+
}
600+
} else {
601+
if ($internalError->shouldReportBug()) {
602+
$message .= sprintf('%sRun PHPStan with -v option and post the stack trace to:%s%s%s', "\n\n", "\n", $bugReportUrl, "\n");
603+
} else {
604+
$message .= sprintf('%sRun PHPStan with -v option to see the stack trace', "\n");
605+
}
606+
}
607+
}
608+
609+
return $message;
610+
}
611+
558612
private function generateBaseline(string $generateBaselineFile, InceptionResult $inceptionResult, AnalysisResult $analysisResult, OutputInterface $output, bool $allowEmptyBaseline, string $baselineExtension, bool $failWithoutResultCache): int
559613
{
560614
if (!$allowEmptyBaseline && !$analysisResult->hasErrors()) {

0 commit comments

Comments
 (0)