2
2
3
3
namespace PHPStan \Command ;
4
4
5
+ use PHPStan \AnalysedCodeException ;
5
6
use PHPStan \Analyser \AnalyserResult ;
7
+ use PHPStan \Analyser \Error ;
6
8
use PHPStan \Analyser \IgnoredErrorHelper ;
7
9
use PHPStan \Analyser \ResultCache \ResultCacheManagerFactory ;
10
+ use PHPStan \Analyser \RuleErrorTransformer ;
11
+ use PHPStan \Analyser \ScopeContext ;
12
+ use PHPStan \Analyser \ScopeFactory ;
13
+ use PHPStan \BetterReflection \NodeCompiler \Exception \UnableToCompileNode ;
14
+ use PHPStan \BetterReflection \Reflection \Exception \NotAClassReflection ;
15
+ use PHPStan \BetterReflection \Reflection \Exception \NotAnInterfaceReflection ;
16
+ use PHPStan \BetterReflection \Reflector \Exception \IdentifierNotFound ;
17
+ use PHPStan \Collectors \CollectedData ;
8
18
use PHPStan \Internal \BytesHelper ;
19
+ use PHPStan \Node \CollectedDataNode ;
9
20
use PHPStan \PhpDoc \StubFilesProvider ;
10
21
use PHPStan \PhpDoc \StubValidator ;
22
+ use PHPStan \Rules \Registry as RuleRegistry ;
11
23
use PHPStan \ShouldNotHappenException ;
12
24
use Symfony \Component \Console \Input \InputInterface ;
13
25
use function array_merge ;
@@ -27,6 +39,9 @@ public function __construct(
27
39
private IgnoredErrorHelper $ ignoredErrorHelper ,
28
40
private int $ internalErrorsCountLimit ,
29
41
private StubFilesProvider $ stubFilesProvider ,
42
+ private RuleRegistry $ ruleRegistry ,
43
+ private ScopeFactory $ scopeFactory ,
44
+ private RuleErrorTransformer $ ruleErrorTransformer ,
30
45
)
31
46
{
32
47
}
@@ -87,7 +102,11 @@ public function analyse(
87
102
$ resultCacheResult = $ resultCacheManager ->process ($ intermediateAnalyserResult , $ resultCache , $ errorOutput , $ onlyFiles , true );
88
103
$ analyserResult = $ resultCacheResult ->getAnalyserResult ();
89
104
$ internalErrors = $ analyserResult ->getInternalErrors ();
90
- $ errors = $ ignoredErrorHelperResult ->process ($ analyserResult ->getErrors (), $ onlyFiles , $ files , count ($ internalErrors ) > 0 || $ analyserResult ->hasReachedInternalErrorsCountLimit ());
105
+ $ errors = $ analyserResult ->getErrors ();
106
+ foreach ($ this ->getCollectedDataErrors ($ analyserResult ->getCollectedData ()) as $ error ) {
107
+ $ errors [] = $ error ;
108
+ }
109
+ $ errors = $ ignoredErrorHelperResult ->process ($ errors , $ onlyFiles , $ files , count ($ internalErrors ) > 0 || $ analyserResult ->hasReachedInternalErrorsCountLimit ());
91
110
$ collectedData = $ analyserResult ->getCollectedData ();
92
111
$ savedResultCache = $ resultCacheResult ->isSaved ();
93
112
if ($ analyserResult ->hasReachedInternalErrorsCountLimit ()) {
@@ -119,6 +138,39 @@ public function analyse(
119
138
);
120
139
}
121
140
141
+ /**
142
+ * @param CollectedData[] $collectedData
143
+ * @return Error[]
144
+ */
145
+ private function getCollectedDataErrors (array $ collectedData ): array
146
+ {
147
+ $ nodeType = CollectedDataNode::class;
148
+ $ node = new CollectedDataNode ($ collectedData );
149
+ $ file = 'N/A ' ;
150
+ $ scope = $ this ->scopeFactory ->create (ScopeContext::create ($ file ));
151
+ $ errors = [];
152
+ foreach ($ this ->ruleRegistry ->getRules ($ nodeType ) as $ rule ) {
153
+ try {
154
+ $ ruleErrors = $ rule ->processNode ($ node , $ scope );
155
+ } catch (AnalysedCodeException $ e ) {
156
+ $ errors [] = new Error ($ e ->getMessage (), $ file , $ node ->getLine (), $ e , null , null , $ e ->getTip ());
157
+ continue ;
158
+ } catch (IdentifierNotFound $ e ) {
159
+ $ errors [] = new Error (sprintf ('Reflection error: %s not found. ' , $ e ->getIdentifier ()->getName ()), $ file , $ node ->getLine (), $ e , null , null , 'Learn more at https://phpstan.org/user-guide/discovering-symbols ' );
160
+ continue ;
161
+ } catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection $ e ) {
162
+ $ errors [] = new Error (sprintf ('Reflection error: %s ' , $ e ->getMessage ()), $ file , $ node ->getLine (), $ e );
163
+ continue ;
164
+ }
165
+
166
+ foreach ($ ruleErrors as $ ruleError ) {
167
+ $ errors [] = $ this ->ruleErrorTransformer ->transform ($ ruleError , $ scope , $ nodeType , $ node ->getLine ());
168
+ }
169
+ }
170
+
171
+ return $ errors ;
172
+ }
173
+
122
174
/**
123
175
* @param string[] $files
124
176
* @param string[] $allAnalysedFiles
0 commit comments