6
6
use Nette \FileNotFoundException ;
7
7
use Nette \InvalidStateException ;
8
8
use OndraM \CiDetector \CiDetector ;
9
+ use PHPStan \Analyser \Error ;
9
10
use PHPStan \Analyser \Ignore \IgnoredError ;
10
11
use PHPStan \Analyser \InternalError ;
11
12
use PHPStan \Command \ErrorFormatter \BaselineNeonErrorFormatter ;
@@ -603,7 +604,9 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
603
604
$ baselineFileDirectory = dirname ($ generateBaselineFile );
604
605
$ fileHelper = $ container ->getByType (FileHelper::class);
605
606
$ baselinePathHelper = new ParentDirectoryRelativePathHelper ($ baselineFileDirectory );
606
- $ analysisResult = $ this ->processFileSpecificErrorsFromAnalysisResult ($ analysisResult , $ ignoreNewErrors , $ generateBaselineFile , $ inceptionResult , $ fileHelper , $ baselinePathHelper );
607
+ if ($ ignoreNewErrors ) {
608
+ $ analysisResult = $ this ->filterAnalysisResultForExistingErrors ($ analysisResult , $ generateBaselineFile , $ inceptionResult , $ fileHelper , $ baselinePathHelper );
609
+ }
607
610
608
611
if (!$ allowEmptyBaseline && !$ analysisResult ->hasErrors ()) {
609
612
$ inceptionResult ->getStdOutput ()->getStyle ()->error ('No errors were found during the analysis. Baseline could not be generated. ' );
@@ -689,39 +692,26 @@ private function generateBaseline(string $generateBaselineFile, InceptionResult
689
692
return $ inceptionResult ->handleReturn ($ exitCode , $ analysisResult ->getPeakMemoryUsageBytes (), $ this ->analysisStartTime );
690
693
}
691
694
692
- private function processFileSpecificErrorsFromAnalysisResult (AnalysisResult $ analysisResult, bool $ ignoreNewErrors , string $ generateBaselineFile , InceptionResult $ inceptionResult , FileHelper $ fileHelper , RelativePathHelper $ baselinePathHelper ): AnalysisResult
695
+ private function filterAnalysisResultForExistingErrors (AnalysisResult $ analysisResult , string $ generateBaselineFile , InceptionResult $ inceptionResult , FileHelper $ fileHelper , RelativePathHelper $ baselinePathHelper ): AnalysisResult
693
696
{
694
697
$ fileSpecificErrors = $ analysisResult ->getFileSpecificErrors ();
695
- if (!$ ignoreNewErrors ) {
696
- return $ analysisResult ;
697
- }
698
698
699
699
$ baselineIgnoreErrors = $ this ->getCurrentBaselineIgnoreErrors ($ generateBaselineFile , $ inceptionResult );
700
700
$ ignoreErrorsByFile = $ this ->mapIgnoredErrors ($ baselineIgnoreErrors , $ fileHelper );
701
701
702
+ $ ignoreUseCount = [];
703
+
702
704
foreach ($ fileSpecificErrors as $ errorIndex => $ error ) {
703
- $ filePath = $ baselinePathHelper ->getRelativePath ($ error ->getFilePath ());
704
- if (isset ($ ignoreErrorsByFile [$ filePath ])) {
705
- foreach ($ ignoreErrorsByFile [$ filePath ] as $ ignoreError ) {
706
- $ ignore = $ ignoreError ['ignoreError ' ];
707
- $ shouldIgnore = IgnoredError::shouldIgnore ($ fileHelper , $ error , $ ignore ['message ' ] ?? null , $ ignore ['identifier ' ] ?? null , null );
708
- if ($ shouldIgnore ) {
709
- continue 2 ;
710
- }
711
- }
705
+ $ hasMatchingIgnore = $ this ->checkIgnoreErrorByPath ($ error ->getFilePath (), $ ignoreErrorsByFile , $ fileHelper , $ error , $ ignoreUseCount , $ baselinePathHelper );
706
+ if ($ hasMatchingIgnore ) {
707
+ continue ;
712
708
}
713
709
714
710
$ traitFilePath = $ error ->getTraitFilePath ();
715
711
if ($ traitFilePath !== null ) {
716
- $ normalizedTraitFilePath = $ baselinePathHelper ->getRelativePath ($ traitFilePath );
717
- if (isset ($ ignoreErrorsByFile [$ normalizedTraitFilePath ])) {
718
- foreach ($ ignoreErrorsByFile [$ normalizedTraitFilePath ] as $ ignoreError ) {
719
- $ ignore = $ ignoreError ['ignoreError ' ];
720
- $ shouldIgnore = IgnoredError::shouldIgnore ($ fileHelper , $ error , $ ignore ['message ' ] ?? null , $ ignore ['identifier ' ] ?? null , null );
721
- if ($ shouldIgnore ) {
722
- continue 2 ;
723
- }
724
- }
712
+ $ hasMatchingIgnore = $ this ->checkIgnoreErrorByPath ($ traitFilePath , $ ignoreErrorsByFile , $ fileHelper , $ error , $ ignoreUseCount , $ baselinePathHelper );
713
+ if ($ hasMatchingIgnore ) {
714
+ continue ;
725
715
}
726
716
}
727
717
@@ -746,6 +736,36 @@ private function processFileSpecificErrorsFromAnalysisResult(AnalysisResult $ana
746
736
);
747
737
}
748
738
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
+
749
769
/**
750
770
* @param string[] $files
751
771
*/
@@ -805,89 +825,21 @@ private function getCurrentBaselineIgnoreErrors(string $generateBaselineFile, In
805
825
}
806
826
807
827
/**
808
- * @param (string| mixed[]) [] $baselineIgnoreErrors
828
+ * @param mixed[][] $baselineIgnoreErrors
809
829
* @return mixed[][]
810
- * @throws ShouldNotHappenException
811
830
*/
812
831
private function mapIgnoredErrors (array $ baselineIgnoreErrors , FileHelper $ fileHelper ): array
813
832
{
814
833
$ ignoreErrorsByFile = [];
815
834
816
- $ expandedIgnoreErrors = [];
817
- foreach ($ baselineIgnoreErrors as $ ignoreError ) {
818
- if (!is_array ($ ignoreError )) {
819
- throw new ShouldNotHappenException ('Baseline should not have ignore error strings ' );
820
- }
821
-
822
- if (!isset ($ ignoreError ['message ' ]) && !isset ($ ignoreError ['messages ' ]) && !isset ($ ignoreError ['identifier ' ])) {
823
- continue ;
824
- }
825
- if (isset ($ ignoreError ['messages ' ])) {
826
- foreach ($ ignoreError ['messages ' ] as $ message ) {
827
- $ expandedIgnoreError = $ ignoreError ;
828
- unset($ expandedIgnoreError ['messages ' ]);
829
- $ expandedIgnoreError ['message ' ] = $ message ;
830
- $ expandedIgnoreErrors [] = $ expandedIgnoreError ;
831
- }
832
- } else {
833
- $ expandedIgnoreErrors [] = $ ignoreError ;
834
- }
835
- }
836
- $ uniquedExpandedIgnoreErrors = [];
837
- foreach ($ expandedIgnoreErrors as $ ignoreError ) {
838
- if (!isset ($ ignoreError ['message ' ]) && !isset ($ ignoreError ['identifier ' ])) {
839
- $ uniquedExpandedIgnoreErrors [] = $ ignoreError ;
840
- continue ;
841
- }
842
- if (!isset ($ ignoreError ['path ' ])) {
843
- $ uniquedExpandedIgnoreErrors [] = $ ignoreError ;
844
- continue ;
845
- }
846
-
847
- $ key = $ ignoreError ['path ' ];
848
- if (isset ($ ignoreError ['message ' ])) {
849
- $ key = sprintf ("%s \n%s " , $ key , $ ignoreError ['message ' ]);
850
- }
851
- if (isset ($ ignoreError ['identifier ' ])) {
852
- $ key = sprintf ("%s \n%s " , $ key , $ ignoreError ['identifier ' ]);
853
- }
854
- if ($ key === '' ) {
855
- throw new ShouldNotHappenException ();
856
- }
857
-
858
- if (!array_key_exists ($ key , $ uniquedExpandedIgnoreErrors )) {
859
- $ uniquedExpandedIgnoreErrors [$ key ] = $ ignoreError ;
860
- continue ;
861
- }
862
-
863
- $ uniquedExpandedIgnoreErrors [$ key ] = [
864
- 'message ' => $ ignoreError ['message ' ] ?? null ,
865
- 'path ' => $ ignoreError ['path ' ],
866
- 'identifier ' => $ ignoreError ['identifier ' ] ?? null ,
867
- 'count ' => ($ uniquedExpandedIgnoreErrors [$ key ]['count ' ] ?? 1 ) + ($ ignoreError ['count ' ] ?? 1 ),
868
- 'reportUnmatched ' => false ,
869
- ];
870
- }
871
- $ expandedIgnoreErrors = array_values ($ uniquedExpandedIgnoreErrors );
872
-
873
- foreach ($ expandedIgnoreErrors as $ i => $ ignoreError ) {
835
+ foreach ($ baselineIgnoreErrors as $ i => $ ignoreError ) {
874
836
$ ignoreErrorEntry = [
875
837
'index ' => $ i ,
876
838
'ignoreError ' => $ ignoreError ,
877
839
];
878
840
879
- if (!isset ($ ignoreError ['message ' ]) && !isset ($ ignoreError ['identifier ' ])) {
880
- continue ;
881
- }
882
- if (!isset ($ ignoreError ['path ' ])) {
883
- throw new ShouldNotHappenException ('Baseline should not have ignore errors without path ' );
884
- }
885
-
886
841
$ normalizedPath = $ fileHelper ->normalizePath ($ ignoreError ['path ' ]);
887
- $ ignoreError ['path ' ] = $ normalizedPath ;
888
842
$ ignoreErrorsByFile [$ normalizedPath ][] = $ ignoreErrorEntry ;
889
- $ ignoreError ['realPath ' ] = $ normalizedPath ;
890
- $ expandedIgnoreErrors [$ i ] = $ ignoreError ;
891
843
}
892
844
893
845
return $ ignoreErrorsByFile ;
0 commit comments