6
6
use Nette \FileNotFoundException ;
7
7
use Nette \InvalidStateException ;
8
8
use OndraM \CiDetector \CiDetector ;
9
- use PHPStan \Analyser \Error ;
10
- use PHPStan \Analyser \Ignore \IgnoredError ;
9
+ use PHPStan \Analyser \Ignore \BaselineIgnoredErrorHelper ;
11
10
use PHPStan \Analyser \InternalError ;
12
11
use PHPStan \Command \ErrorFormatter \BaselineNeonErrorFormatter ;
13
12
use PHPStan \Command \ErrorFormatter \BaselinePhpErrorFormatter ;
@@ -602,10 +601,9 @@ private function getMessageFromInternalError(FileHelper $fileHelper, InternalErr
602
601
private function generateBaseline (string $ generateBaselineFile , InceptionResult $ inceptionResult , AnalysisResult $ analysisResult , OutputInterface $ output , bool $ allowEmptyBaseline , string $ baselineExtension , bool $ failWithoutResultCache , bool $ onlyRemoveErrors , Container $ container ): int
603
602
{
604
603
$ baselineFileDirectory = dirname ($ generateBaselineFile );
605
- $ fileHelper = $ container ->getByType (FileHelper::class);
606
604
$ baselinePathHelper = new ParentDirectoryRelativePathHelper ($ baselineFileDirectory );
607
605
if ($ onlyRemoveErrors ) {
608
- $ analysisResult = $ this ->filterAnalysisResultForExistingErrors ($ analysisResult , $ generateBaselineFile , $ inceptionResult , $ fileHelper , $ baselinePathHelper );
606
+ $ analysisResult = $ this ->filterAnalysisResultForExistingErrors ($ analysisResult , $ generateBaselineFile , $ inceptionResult , $ container , $ baselinePathHelper );
609
607
}
610
608
611
609
if (!$ allowEmptyBaseline && !$ analysisResult ->hasErrors ()) {
@@ -618,7 +616,6 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
618
616
$ streamOutput = $ this ->createStreamOutput ();
619
617
$ errorConsoleStyle = new ErrorsConsoleStyle (new StringInput ('' ), $ streamOutput );
620
618
$ baselineOutput = new SymfonyOutput ($ streamOutput , new SymfonyStyle ($ errorConsoleStyle ));
621
- $ baselinePathHelper = new ParentDirectoryRelativePathHelper ($ baselineFileDirectory );
622
619
623
620
if ($ baselineExtension === 'php ' ) {
624
621
$ baselineErrorFormatter = new BaselinePhpErrorFormatter ($ baselinePathHelper );
@@ -692,37 +689,19 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
692
689
return $ inceptionResult ->handleReturn ($ exitCode , $ analysisResult ->getPeakMemoryUsageBytes (), $ this ->analysisStartTime );
693
690
}
694
691
695
- private function filterAnalysisResultForExistingErrors (AnalysisResult $ analysisResult , string $ generateBaselineFile , InceptionResult $ inceptionResult , FileHelper $ fileHelper , RelativePathHelper $ baselinePathHelper ): AnalysisResult
692
+ private function filterAnalysisResultForExistingErrors (AnalysisResult $ analysisResult , string $ generateBaselineFile , InceptionResult $ inceptionResult , Container $ container , ParentDirectoryRelativePathHelper $ baselinePathHelper ): AnalysisResult
696
693
{
697
- $ fileSpecificErrors = $ analysisResult ->getFileSpecificErrors ();
698
-
699
- $ baselineIgnoreErrors = $ this ->getCurrentBaselineIgnoreErrors ($ generateBaselineFile , $ inceptionResult );
700
- $ ignoreErrorsByFile = $ this ->mapIgnoredErrors ($ baselineIgnoreErrors , $ fileHelper );
701
-
702
- $ ignoreUseCount = [];
703
-
704
- foreach ($ fileSpecificErrors as $ errorIndex => $ error ) {
705
- $ hasMatchingIgnore = $ this ->checkIgnoreErrorByPath ($ error ->getFilePath (), $ ignoreErrorsByFile , $ fileHelper , $ error , $ ignoreUseCount , $ baselinePathHelper );
706
- if ($ hasMatchingIgnore ) {
707
- continue ;
708
- }
694
+ $ currentAnalysisErrors = $ analysisResult ->getFileSpecificErrors ();
709
695
710
- $ traitFilePath = $ error ->getTraitFilePath ();
711
- if ($ traitFilePath !== null ) {
712
- $ hasMatchingIgnore = $ this ->checkIgnoreErrorByPath ($ traitFilePath , $ ignoreErrorsByFile , $ fileHelper , $ error , $ ignoreUseCount , $ baselinePathHelper );
713
- if ($ hasMatchingIgnore ) {
714
- continue ;
715
- }
716
- }
696
+ $ currentBaselinedErrors = $ this ->getCurrentBaselinedErrors ($ generateBaselineFile , $ inceptionResult );
717
697
718
- // the error was not matched in the baseline, making it a new error, new errors should be ignored here
719
- unset($ fileSpecificErrors [$ errorIndex ]);
720
- }
698
+ /** @var BaselineIgnoredErrorHelper $baselineIgnoredErrorsHelper */
699
+ $ baselineIgnoredErrorsHelper = $ container ->getByType (BaselineIgnoredErrorHelper::class);
721
700
722
- $ fileSpecificErrors = array_values ( $ fileSpecificErrors );
701
+ $ nextBaselinedErrors = $ baselineIgnoredErrorsHelper -> removeUnusedIgnoredErrors ( $ currentBaselinedErrors , $ currentAnalysisErrors , $ baselinePathHelper );
723
702
724
703
return new AnalysisResult (
725
- $ fileSpecificErrors ,
704
+ $ nextBaselinedErrors ,
726
705
$ analysisResult ->getNotFileSpecificErrors (),
727
706
$ analysisResult ->getInternalErrorObjects (),
728
707
$ analysisResult ->getWarnings (),
@@ -736,36 +715,6 @@ private function filterAnalysisResultForExistingErrors(AnalysisResult $analysisR
736
715
);
737
716
}
738
717
739
- /**
740
- * @param mixed[][] $ignoreErrorsByFile
741
- * @param int[] $ignoreUseCount map of indexes of ignores and how often they have been "used" to ignore an error
742
- */
743
- private function checkIgnoreErrorByPath (string $ filePath , array $ ignoreErrorsByFile , FileHelper $ fileHelper , Error $ error , array &$ ignoreUseCount , RelativePathHelper $ baselinePathHelper ): bool
744
- {
745
- $ relativePath = $ baselinePathHelper ->getRelativePath ($ filePath );
746
- if (!isset ($ ignoreErrorsByFile [$ relativePath ])) {
747
- return false ;
748
- }
749
-
750
- foreach ($ ignoreErrorsByFile [$ relativePath ] as $ ignoreError ) {
751
- $ ignore = $ ignoreError ['ignoreError ' ];
752
- $ shouldIgnore = IgnoredError::shouldIgnore ($ fileHelper , $ error , $ ignore ['message ' ] ?? null , $ ignore ['identifier ' ] ?? null , null );
753
- if (!$ shouldIgnore ) {
754
- continue ;
755
- }
756
-
757
- $ realCount = $ ignoreUseCount [$ ignoreError ['index ' ]] ?? 0 ;
758
- $ realCount ++;
759
- $ ignoreUseCount [$ ignoreError ['index ' ]] = $ realCount ;
760
-
761
- if ($ realCount <= $ ignore ['count ' ]) {
762
- return true ;
763
- }
764
- }
765
-
766
- return false ;
767
- }
768
-
769
718
/**
770
719
* @param string[] $files
771
720
*/
@@ -808,41 +757,23 @@ private function runDiagnoseExtensions(Container $container, Output $errorOutput
808
757
}
809
758
}
810
759
811
- private function getCurrentBaselineIgnoreErrors (string $ generateBaselineFile , InceptionResult $ inceptionResult ): mixed
760
+ /**
761
+ * @return mixed[][]
762
+ */
763
+ private function getCurrentBaselinedErrors (string $ generateBaselineFile , InceptionResult $ inceptionResult ): array
812
764
{
813
765
$ loader = new Loader ();
814
766
try {
815
767
$ currentBaselineConfig = $ loader ->load ($ generateBaselineFile );
816
- $ baselineIgnoreErrors = $ currentBaselineConfig ['parameters ' ]['ignoreErrors ' ] ?? [];
768
+ $ baselinedErrors = $ currentBaselineConfig ['parameters ' ]['ignoreErrors ' ] ?? [];
817
769
} catch (FileNotFoundException ) {
818
770
// currently no baseline file -> empty config
819
- $ baselineIgnoreErrors = [];
771
+ $ baselinedErrors = [];
820
772
} catch (InvalidStateException $ invalidStateException ) {
821
773
$ inceptionResult ->getErrorOutput ()->writeLineFormatted ($ invalidStateException ->getMessage ());
822
774
throw $ invalidStateException ;
823
775
}
824
- return $ baselineIgnoreErrors ;
825
- }
826
-
827
- /**
828
- * @param mixed[][] $baselineIgnoreErrors
829
- * @return mixed[][]
830
- */
831
- private function mapIgnoredErrors (array $ baselineIgnoreErrors , FileHelper $ fileHelper ): array
832
- {
833
- $ ignoreErrorsByFile = [];
834
-
835
- foreach ($ baselineIgnoreErrors as $ i => $ ignoreError ) {
836
- $ ignoreErrorEntry = [
837
- 'index ' => $ i ,
838
- 'ignoreError ' => $ ignoreError ,
839
- ];
840
-
841
- $ normalizedPath = $ fileHelper ->normalizePath ($ ignoreError ['path ' ]);
842
- $ ignoreErrorsByFile [$ normalizedPath ][] = $ ignoreErrorEntry ;
843
- }
844
-
845
- return $ ignoreErrorsByFile ;
776
+ return $ baselinedErrors ;
846
777
}
847
778
848
779
}
0 commit comments