13
13
use PHPStan \Diagnose \DiagnoseExtension ;
14
14
use PHPStan \Diagnose \PHPStanDiagnoseExtension ;
15
15
use PHPStan \File \CouldNotWriteFileException ;
16
+ use PHPStan \File \FileHelper ;
16
17
use PHPStan \File \FileReader ;
17
18
use PHPStan \File \FileWriter ;
18
19
use PHPStan \File \ParentDirectoryRelativePathHelper ;
34
35
use function array_key_exists ;
35
36
use function array_keys ;
36
37
use function array_map ;
38
+ use function array_reverse ;
37
39
use function array_unique ;
38
40
use function array_values ;
39
41
use function count ;
50
52
use function pathinfo ;
51
53
use function rewind ;
52
54
use function sprintf ;
55
+ use function str_contains ;
53
56
use function stream_get_contents ;
54
57
use function strlen ;
55
58
use function substr ;
56
59
use const PATHINFO_BASENAME ;
57
60
use const PATHINFO_EXTENSION ;
58
61
62
+ /**
63
+ * @phpstan-import-type Trace from InternalError as InternalErrorTrace
64
+ */
59
65
final class AnalyseCommand extends Command
60
66
{
61
67
@@ -385,7 +391,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
385
391
}
386
392
387
393
$ internalErrorsTuples = array_values ($ internalErrorsTuples );
388
- $ bugReportUrl = 'https://github.com/phpstan/phpstan/issues/new?template=Bug_report.yaml ' ;
394
+
395
+ $ fileHelper = $ container ->getByType (FileHelper::class);
389
396
390
397
/**
391
398
* Variable $internalErrors only contains non-file-specific "internal errors".
@@ -396,32 +403,8 @@ protected function execute(InputInterface $input, OutputInterface $output): int
396
403
continue ;
397
404
}
398
405
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
-
423
406
$ internalErrors [] = new InternalError (
424
- $ message ,
407
+ $ this -> getMessageFromInternalError ( $ fileHelper , $ internalError , $ output -> getVerbosity ()) ,
425
408
$ internalError ->getContextDescription (),
426
409
$ internalError ->getTrace (),
427
410
$ internalError ->getTraceAsString (),
@@ -555,6 +538,77 @@ private function createStreamOutput(): StreamOutput
555
538
return new StreamOutput ($ resource );
556
539
}
557
540
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
+
558
612
private function generateBaseline (string $ generateBaselineFile , InceptionResult $ inceptionResult , AnalysisResult $ analysisResult , OutputInterface $ output , bool $ allowEmptyBaseline , string $ baselineExtension , bool $ failWithoutResultCache ): int
559
613
{
560
614
if (!$ allowEmptyBaseline && !$ analysisResult ->hasErrors ()) {
0 commit comments