1111use PHPStan \Collectors \Registry as CollectorRegistry ;
1212use PHPStan \Dependency \DependencyResolver ;
1313use PHPStan \Node \FileNode ;
14+ use PHPStan \Node \InClassNode ;
1415use PHPStan \Node \InTraitNode ;
1516use PHPStan \Parser \Parser ;
1617use PHPStan \Parser \ParserErrorsException ;
@@ -83,6 +84,7 @@ public function analyseFile(
8384 $ fileCollectedData = [];
8485
8586 $ fileDependencies = [];
87+ $ usedTraitFileDependencies = [];
8688 $ exportedNodes = [];
8789 $ linesToIgnore = [];
8890 $ unmatchedLineIgnores = [];
@@ -92,7 +94,7 @@ public function analyseFile(
9294 $ parserNodes = $ this ->parser ->parseFile ($ file );
9395 $ linesToIgnore = $ unmatchedLineIgnores = [$ file => $ this ->getLinesToIgnoreFromTokens ($ parserNodes )];
9496 $ temporaryFileErrors = [];
95- $ nodeCallback = function (Node $ node , Scope $ scope ) use (&$ fileErrors , &$ fileCollectedData , &$ fileDependencies , &$ exportedNodes , $ file , $ ruleRegistry , $ collectorRegistry , $ outerNodeCallback , $ analysedFiles , &$ linesToIgnore , &$ unmatchedLineIgnores , &$ temporaryFileErrors ): void {
97+ $ nodeCallback = function (Node $ node , Scope $ scope ) use (&$ fileErrors , &$ fileCollectedData , &$ fileDependencies , &$ usedTraitFileDependencies , & $ exportedNodes , $ file , $ ruleRegistry , $ collectorRegistry , $ outerNodeCallback , $ analysedFiles , &$ linesToIgnore , &$ unmatchedLineIgnores , &$ temporaryFileErrors ): void {
9698 if ($ node instanceof Node \Stmt \Trait_) {
9799 foreach (array_keys ($ linesToIgnore [$ file ] ?? []) as $ lineToIgnore ) {
98100 if ($ lineToIgnore < $ node ->getStartLine () || $ lineToIgnore > $ node ->getEndLine ()) {
@@ -216,6 +218,15 @@ public function analyseFile(
216218 } catch (UnableToCompileNode ) {
217219 // pass
218220 }
221+
222+ if (!$ node instanceof InClassNode) {
223+ return ;
224+ }
225+
226+ $ usedTraitDependencies = $ this ->dependencyResolver ->resolveUsedTraitDependencies ($ node );
227+ foreach ($ usedTraitDependencies ->getFileDependencies ($ scope ->getFile (), $ analysedFiles ) as $ dependentFile ) {
228+ $ usedTraitFileDependencies [] = $ dependentFile ;
229+ }
219230 };
220231
221232 $ scope = $ this ->scopeFactory ->create (ScopeContext::create ($ file ));
@@ -298,6 +309,7 @@ public function analyseFile(
298309 $ locallyIgnoredErrors ,
299310 $ fileCollectedData ,
300311 array_values (array_unique ($ fileDependencies )),
312+ array_values (array_unique ($ usedTraitFileDependencies )),
301313 $ exportedNodes ,
302314 $ linesToIgnore ,
303315 $ unmatchedLineIgnores ,
0 commit comments