Skip to content

Commit a8f412a

Browse files
committed
Merge remote-tracking branch 'origin/1.12.x' into 2.1.x
2 parents fed47b1 + bd6fc4e commit a8f412a

15 files changed

+314
-38
lines changed

src/Analyser/Analyser.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function analyse(
6868
$internalErrorsCount = 0;
6969
$reachedInternalErrorsCountLimit = false;
7070
$dependencies = [];
71+
$usedTraitDependencies = [];
7172
$exportedNodes = [];
7273
foreach ($files as $file) {
7374
if ($preFileCallback !== null) {
@@ -91,6 +92,7 @@ public function analyse(
9192
$unmatchedLineIgnores[$file] = $fileAnalyserResult->getUnmatchedLineIgnores();
9293
$collectedData = array_merge($collectedData, $fileAnalyserResult->getCollectedData());
9394
$dependencies[$file] = $fileAnalyserResult->getDependencies();
95+
$usedTraitDependencies[$file] = $fileAnalyserResult->getUsedTraitDependencies();
9496

9597
$fileExportedNodes = $fileAnalyserResult->getExportedNodes();
9698
if (count($fileExportedNodes) > 0) {
@@ -130,6 +132,7 @@ public function analyse(
130132
[],
131133
$collectedData,
132134
$internalErrorsCount === 0 ? $dependencies : null,
135+
$internalErrorsCount === 0 ? $usedTraitDependencies : null,
133136
$exportedNodes,
134137
$reachedInternalErrorsCountLimit,
135138
memory_get_peak_usage(true),

src/Analyser/AnalyserResult.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ final class AnalyserResult
2626
* @param CollectorData $collectedData
2727
* @param list<InternalError> $internalErrors
2828
* @param array<string, array<string>>|null $dependencies
29+
* @param array<string, array<string>>|null $usedTraitDependencies
2930
* @param array<string, array<RootExportedNode>> $exportedNodes
3031
*/
3132
public function __construct(
@@ -38,6 +39,7 @@ public function __construct(
3839
private array $internalErrors,
3940
private array $collectedData,
4041
private ?array $dependencies,
42+
private ?array $usedTraitDependencies,
4143
private array $exportedNodes,
4244
private bool $reachedInternalErrorsCountLimit,
4345
private int $peakMemoryUsageBytes,
@@ -141,6 +143,14 @@ public function getDependencies(): ?array
141143
return $this->dependencies;
142144
}
143145

146+
/**
147+
* @return array<string, array<string>>|null
148+
*/
149+
public function getUsedTraitDependencies(): ?array
150+
{
151+
return $this->usedTraitDependencies;
152+
}
153+
144154
/**
145155
* @return array<string, array<RootExportedNode>>
146156
*/

src/Analyser/AnalyserResultFinalizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles, bool $
140140
$internalErrors,
141141
$analyserResult->getCollectedData(),
142142
$analyserResult->getDependencies(),
143+
$analyserResult->getUsedTraitDependencies(),
143144
$analyserResult->getExportedNodes(),
144145
$analyserResult->hasReachedInternalErrorsCountLimit(),
145146
$analyserResult->getPeakMemoryUsageBytes(),
@@ -158,6 +159,7 @@ private function mergeFilteredPhpErrors(AnalyserResult $analyserResult): Analyse
158159
$analyserResult->getInternalErrors(),
159160
$analyserResult->getCollectedData(),
160161
$analyserResult->getDependencies(),
162+
$analyserResult->getUsedTraitDependencies(),
161163
$analyserResult->getExportedNodes(),
162164
$analyserResult->hasReachedInternalErrorsCountLimit(),
163165
$analyserResult->getPeakMemoryUsageBytes(),
@@ -221,6 +223,7 @@ private function addUnmatchedIgnoredErrors(
221223
$analyserResult->getInternalErrors(),
222224
$analyserResult->getCollectedData(),
223225
$analyserResult->getDependencies(),
226+
$analyserResult->getUsedTraitDependencies(),
224227
$analyserResult->getExportedNodes(),
225228
$analyserResult->hasReachedInternalErrorsCountLimit(),
226229
$analyserResult->getPeakMemoryUsageBytes(),

src/Analyser/FileAnalyser.php

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use PHPStan\Collectors\Registry as CollectorRegistry;
1212
use PHPStan\Dependency\DependencyResolver;
1313
use PHPStan\Node\FileNode;
14+
use PHPStan\Node\InClassNode;
1415
use PHPStan\Node\InTraitNode;
1516
use PHPStan\Parser\Parser;
1617
use 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,

src/Analyser/FileAnalyserResult.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ final class FileAnalyserResult
1919
* @param list<Error> $locallyIgnoredErrors
2020
* @param CollectorData $collectedData
2121
* @param list<string> $dependencies
22+
* @param list<string> $usedTraitDependencies
2223
* @param list<RootExportedNode> $exportedNodes
2324
* @param LinesToIgnore $linesToIgnore
2425
* @param LinesToIgnore $unmatchedLineIgnores
@@ -30,6 +31,7 @@ public function __construct(
3031
private array $locallyIgnoredErrors,
3132
private array $collectedData,
3233
private array $dependencies,
34+
private array $usedTraitDependencies,
3335
private array $exportedNodes,
3436
private array $linesToIgnore,
3537
private array $unmatchedLineIgnores,
@@ -85,6 +87,14 @@ public function getDependencies(): array
8587
return $this->dependencies;
8688
}
8789

90+
/**
91+
* @return list<string>
92+
*/
93+
public function getUsedTraitDependencies(): array
94+
{
95+
return $this->usedTraitDependencies;
96+
}
97+
8898
/**
8999
* @return list<RootExportedNode>
90100
*/

src/Analyser/ResultCache/ResultCache.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ final class ResultCache
2323
* @param array<string, LinesToIgnore> $unmatchedLineIgnores
2424
* @param CollectorData $collectedData
2525
* @param array<string, array<string>> $dependencies
26+
* @param array<string, array<string>> $usedTraitDependencies
2627
* @param array<string, array<RootExportedNode>> $exportedNodes
2728
* @param array<string, array{string, bool, string}> $projectExtensionFiles
2829
* @param array<string, string> $currentFileHashes
@@ -38,6 +39,7 @@ public function __construct(
3839
private array $unmatchedLineIgnores,
3940
private array $collectedData,
4041
private array $dependencies,
42+
private array $usedTraitDependencies,
4143
private array $exportedNodes,
4244
private array $projectExtensionFiles,
4345
private array $currentFileHashes,
@@ -119,6 +121,14 @@ public function getDependencies(): array
119121
return $this->dependencies;
120122
}
121123

124+
/**
125+
* @return array<string, array<string>>
126+
*/
127+
public function getUsedTraitDependencies(): array
128+
{
129+
return $this->usedTraitDependencies;
130+
}
131+
122132
/**
123133
* @return array<string, array<RootExportedNode>>
124134
*/

0 commit comments

Comments
 (0)