3
3
namespace PHPStan \Command ;
4
4
5
5
use Nette \Utils \Json ;
6
+ use PHPStan \AnalysedCodeException ;
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 \CircularReference ;
15
+ use PHPStan \BetterReflection \Reflection \Exception \NotAClassReflection ;
16
+ use PHPStan \BetterReflection \Reflection \Exception \NotAnInterfaceReflection ;
17
+ use PHPStan \BetterReflection \Reflector \Exception \IdentifierNotFound ;
18
+ use PHPStan \Collectors \CollectedData ;
19
+ use PHPStan \DependencyInjection \Container ;
20
+ use PHPStan \Node \CollectedDataNode ;
21
+ use PHPStan \Rules \Registry as RuleRegistry ;
8
22
use PHPStan \ShouldNotHappenException ;
9
23
use Symfony \Component \Console \Command \Command ;
10
24
use Symfony \Component \Console \Input \InputArgument ;
15
29
use function is_array ;
16
30
use function is_bool ;
17
31
use function is_string ;
32
+ use function sprintf ;
18
33
19
34
class FixerWorkerCommand extends Command
20
35
{
@@ -124,12 +139,19 @@ protected function execute(InputInterface $input, OutputInterface $output): int
124
139
is_string ($ saveResultCache ) ? $ saveResultCache : $ saveResultCache === null ,
125
140
)->getAnalyserResult ();
126
141
142
+ $ hasInternalErrors = count ($ result ->getInternalErrors ()) > 0 || $ result ->hasReachedInternalErrorsCountLimit ();
127
143
$ intermediateErrors = $ ignoredErrorHelperResult ->process (
128
144
$ result ->getErrors (),
129
145
$ isOnlyFiles ,
130
146
$ inceptionFiles ,
131
- count ( $ result -> getInternalErrors ()) > 0 || $ result -> hasReachedInternalErrorsCountLimit () ,
147
+ $ hasInternalErrors ,
132
148
);
149
+ if (!$ hasInternalErrors ) {
150
+ foreach ($ this ->getCollectedDataErrors ($ container , $ result ->getCollectedData ()) as $ error ) {
151
+ $ intermediateErrors [] = $ error ;
152
+ }
153
+ }
154
+
133
155
$ finalFileSpecificErrors = [];
134
156
$ finalNotFileSpecificErrors = [];
135
157
foreach ($ intermediateErrors as $ intermediateError ) {
@@ -149,4 +171,40 @@ protected function execute(InputInterface $input, OutputInterface $output): int
149
171
return 0 ;
150
172
}
151
173
174
+ /**
175
+ * @param CollectedData[] $collectedData
176
+ * @return Error[]
177
+ */
178
+ private function getCollectedDataErrors (Container $ container , array $ collectedData ): array
179
+ {
180
+ $ nodeType = CollectedDataNode::class;
181
+ $ node = new CollectedDataNode ($ collectedData );
182
+ $ file = 'N/A ' ;
183
+ $ scope = $ container ->getByType (ScopeFactory::class)->create (ScopeContext::create ($ file ));
184
+ $ ruleRegistry = $ container ->getByType (RuleRegistry::class);
185
+ $ ruleErrorTransformer = $ container ->getByType (RuleErrorTransformer::class);
186
+
187
+ $ errors = [];
188
+ foreach ($ ruleRegistry ->getRules ($ nodeType ) as $ rule ) {
189
+ try {
190
+ $ ruleErrors = $ rule ->processNode ($ node , $ scope );
191
+ } catch (AnalysedCodeException $ e ) {
192
+ $ errors [] = new Error ($ e ->getMessage (), $ file , $ node ->getLine (), $ e , null , null , $ e ->getTip ());
193
+ continue ;
194
+ } catch (IdentifierNotFound $ e ) {
195
+ $ 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 ' );
196
+ continue ;
197
+ } catch (UnableToCompileNode | NotAClassReflection | NotAnInterfaceReflection | CircularReference $ e ) {
198
+ $ errors [] = new Error (sprintf ('Reflection error: %s ' , $ e ->getMessage ()), $ file , $ node ->getLine (), $ e );
199
+ continue ;
200
+ }
201
+
202
+ foreach ($ ruleErrors as $ ruleError ) {
203
+ $ errors [] = $ ruleErrorTransformer ->transform ($ ruleError , $ scope , $ nodeType , $ node ->getLine ());
204
+ }
205
+ }
206
+
207
+ return $ errors ;
208
+ }
209
+
152
210
}
0 commit comments