Skip to content

Commit 50723e6

Browse files
phpstan-botstaabmclaude
authored
Performance - list the most-often-analysed trait files in -vvv
Co-authored-by: staabm <120441+staabm@users.noreply.github.com> Co-authored-by: Markus Staab <maggus.staab@googlemail.com> Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
1 parent ae20323 commit 50723e6

15 files changed

+111
-19
lines changed

conf/services.neon

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ services:
105105
composerAutoloaderProjectPaths: %composerAutoloaderProjectPaths%
106106
allConfigFiles: %allConfigFiles%
107107
configPhpVersion: %phpVersion%
108+
simpleRelativePathHelper: @simpleRelativePathHelper
108109
autowired: false
109110

110111
# not registered using attributes because there is 2+ instances

phpstan-baseline.neon

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -234,12 +234,6 @@ parameters:
234234
count: 1
235235
path: src/DependencyInjection/NeonAdapter.php
236236

237-
-
238-
rawMessage: Creating new ReflectionClass is a runtime reflection concept that might not work in PHPStan because it uses fully static reflection engine. Use objects retrieved from ReflectionProvider instead.
239-
identifier: phpstanApi.runtimeReflection
240-
count: 1
241-
path: src/Diagnose/PHPStanDiagnoseExtension.php
242-
243237
-
244238
rawMessage: 'Parameter #1 $path of function dirname expects string, string|false given.'
245239
identifier: argument.type

src/Analyser/Analyser.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ public function __construct(
3636
/**
3737
* @param string[] $files
3838
* @param Closure(string $file): void|null $preFileCallback
39-
* @param Closure(int, list<string>): void|null $postFileCallback
39+
* @param Closure(int, list<string>=): void|null $postFileCallback
4040
* @param string[]|null $allAnalysedFiles
4141
*/
4242
public function analyse(
@@ -75,6 +75,7 @@ public function analyse(
7575
$dependencies = [];
7676
$usedTraitDependencies = [];
7777
$exportedNodes = [];
78+
$allProcessedFiles = [];
7879
foreach ($files as $file) {
7980
if ($preFileCallback !== null) {
8081
$preFileCallback($file);
@@ -92,6 +93,7 @@ public function analyse(
9293
$filteredPhpErrors = array_merge($filteredPhpErrors, $fileAnalyserResult->getFilteredPhpErrors());
9394
$allPhpErrors = array_merge($allPhpErrors, $fileAnalyserResult->getAllPhpErrors());
9495
$processedFiles = $fileAnalyserResult->getProcessedFiles();
96+
$allProcessedFiles = array_merge($allProcessedFiles, $processedFiles);
9597

9698
$locallyIgnoredErrors = array_merge($locallyIgnoredErrors, $fileAnalyserResult->getLocallyIgnoredErrors());
9799
$linesToIgnore[$file] = $fileAnalyserResult->getLinesToIgnore();
@@ -143,6 +145,7 @@ public function analyse(
143145
exportedNodes: $exportedNodes,
144146
reachedInternalErrorsCountLimit: $reachedInternalErrorsCountLimit,
145147
peakMemoryUsageBytes: memory_get_peak_usage(true),
148+
processedFiles: $allProcessedFiles,
146149
);
147150
}
148151

src/Analyser/AnalyserResult.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ final class AnalyserResult
2828
* @param array<string, array<string>>|null $dependencies
2929
* @param array<string, array<string>>|null $usedTraitDependencies
3030
* @param array<string, array<RootExportedNode>> $exportedNodes
31+
* @param list<string> $processedFiles
3132
*/
3233
public function __construct(
3334
private array $unorderedErrors,
@@ -43,6 +44,7 @@ public function __construct(
4344
private array $exportedNodes,
4445
private bool $reachedInternalErrorsCountLimit,
4546
private int $peakMemoryUsageBytes,
47+
private array $processedFiles,
4648
)
4749
{
4850
}
@@ -169,4 +171,12 @@ public function getPeakMemoryUsageBytes(): int
169171
return $this->peakMemoryUsageBytes;
170172
}
171173

174+
/**
175+
* @return list<string>
176+
*/
177+
public function getProcessedFiles(): array
178+
{
179+
return $this->processedFiles;
180+
}
181+
172182
}

src/Analyser/AnalyserResultFinalizer.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,7 @@ public function finalize(AnalyserResult $analyserResult, bool $onlyFiles, bool $
148148
exportedNodes: $analyserResult->getExportedNodes(),
149149
reachedInternalErrorsCountLimit: $analyserResult->hasReachedInternalErrorsCountLimit(),
150150
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
151+
processedFiles: $analyserResult->getProcessedFiles(),
151152
), $collectorErrors, $locallyIgnoredCollectorErrors);
152153
}
153154

@@ -167,6 +168,7 @@ private function mergeFilteredPhpErrors(AnalyserResult $analyserResult): Analyse
167168
exportedNodes: $analyserResult->getExportedNodes(),
168169
reachedInternalErrorsCountLimit: $analyserResult->hasReachedInternalErrorsCountLimit(),
169170
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
171+
processedFiles: $analyserResult->getProcessedFiles(),
170172
);
171173
}
172174

@@ -231,6 +233,7 @@ private function addUnmatchedIgnoredErrors(
231233
exportedNodes: $analyserResult->getExportedNodes(),
232234
reachedInternalErrorsCountLimit: $analyserResult->hasReachedInternalErrorsCountLimit(),
233235
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
236+
processedFiles: $analyserResult->getProcessedFiles(),
234237
),
235238
$collectorErrors,
236239
$locallyIgnoredCollectorErrors,

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -760,6 +760,7 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
760760
exportedNodes: $exportedNodes,
761761
reachedInternalErrorsCountLimit: $analyserResult->hasReachedInternalErrorsCountLimit(),
762762
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
763+
processedFiles: $analyserResult->getProcessedFiles(),
763764
), $saved);
764765
}
765766

src/Command/AnalyseApplication.php

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ public function analyse(
8080
$collectedData = [];
8181
$savedResultCache = false;
8282
$memoryUsageBytes = memory_get_peak_usage(true);
83+
$processedFiles = [];
8384
if ($errorOutput->isVeryVerbose()) {
8485
$errorOutput->writeLineFormatted('Result cache was not saved because of ignoredErrorHelperResult errors.');
8586
}
@@ -121,9 +122,12 @@ public function analyse(
121122
exportedNodes: $intermediateAnalyserResult->getExportedNodes(),
122123
reachedInternalErrorsCountLimit: $intermediateAnalyserResult->hasReachedInternalErrorsCountLimit(),
123124
peakMemoryUsageBytes: $intermediateAnalyserResult->getPeakMemoryUsageBytes(),
125+
processedFiles: $intermediateAnalyserResult->getProcessedFiles(),
124126
);
125127
}
126128

129+
$processedFiles = $intermediateAnalyserResult->getProcessedFiles();
130+
127131
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, $onlyFiles, true);
128132
$analyserResult = $this->analyserResultFinalizer->finalize(
129133
$this->switchTmpFileInAnalyserResult($resultCacheResult->getAnalyserResult(), $insteadOfFile, $tmpFile),
@@ -184,6 +188,7 @@ public function analyse(
184188
$memoryUsageBytes,
185189
$isResultCacheUsed,
186190
$changedProjectExtensionFilesOutsideOfAnalysedPaths,
191+
$processedFiles,
187192
);
188193
}
189194

@@ -239,6 +244,7 @@ private function runAnalyser(
239244
exportedNodes: [],
240245
reachedInternalErrorsCountLimit: false,
241246
peakMemoryUsageBytes: memory_get_peak_usage(true),
247+
processedFiles: [],
242248
);
243249
}
244250

@@ -346,6 +352,7 @@ private function switchTmpFileInAnalyserResult(
346352
exportedNodes: $exportedNodes,
347353
reachedInternalErrorsCountLimit: $analyserResult->hasReachedInternalErrorsCountLimit(),
348354
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
355+
processedFiles: $analyserResult->getProcessedFiles(),
349356
);
350357
}
351358

src/Command/AnalyseCommand.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -459,7 +459,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
459459
}
460460

461461
if ($generateBaselineFile !== null) {
462-
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
462+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput(), $analysisResult->getProcessedFiles());
463463
if (count($internalErrorsTuples) > 0) {
464464
foreach ($internalErrorsTuples as [$internalError]) {
465465
$inceptionResult->getStdOutput()->writeLineFormatted($internalError->getMessage());
@@ -493,11 +493,12 @@ protected function execute(InputInterface $input, OutputInterface $output): int
493493
$analysisResult->getPeakMemoryUsageBytes(),
494494
$analysisResult->isResultCacheUsed(),
495495
$analysisResult->getChangedProjectExtensionFilesOutsideOfAnalysedPaths(),
496+
$analysisResult->getProcessedFiles(),
496497
);
497498

498499
$exitCode = $errorFormatter->formatErrors($analysisResult, $inceptionResult->getStdOutput());
499500

500-
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
501+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput(), $analysisResult->getProcessedFiles());
501502

502503
$errorOutput->writeLineFormatted('⚠️ Result is incomplete because of severe errors. ⚠️');
503504
$errorOutput->writeLineFormatted(' Fix these errors first and then re-run PHPStan');
@@ -649,7 +650,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
649650
}
650651
}
651652

652-
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput());
653+
$this->runDiagnoseExtensions($container, $inceptionResult->getErrorOutput(), $analysisResult->getProcessedFiles());
653654

654655
return $inceptionResult->handleReturn(
655656
$exitCode,
@@ -847,7 +848,10 @@ private function runFixer(InceptionResult $inceptionResult, Container $container
847848
);
848849
}
849850

850-
private function runDiagnoseExtensions(Container $container, Output $errorOutput): void
851+
/**
852+
* @param list<string> $processedFiles
853+
*/
854+
private function runDiagnoseExtensions(Container $container, Output $errorOutput, array $processedFiles = []): void
851855
{
852856
if (!$errorOutput->isDebug()) {
853857
return;
@@ -857,7 +861,7 @@ private function runDiagnoseExtensions(Container $container, Output $errorOutput
857861
$phpstanDiagnoseExtension = $container->getService('phpstanDiagnoseExtension');
858862

859863
// not using tag for this extension to make sure it's always first
860-
$phpstanDiagnoseExtension->print($errorOutput);
864+
$phpstanDiagnoseExtension->print($errorOutput, $processedFiles);
861865

862866
/** @var DiagnoseExtension $extension */
863867
foreach ($container->getServicesByTag(DiagnoseExtension::EXTENSION_TAG) as $extension) {

src/Command/AnalyserRunner.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ public function runAnalyser(
6868
exportedNodes: [],
6969
reachedInternalErrorsCountLimit: false,
7070
peakMemoryUsageBytes: memory_get_peak_usage(true),
71+
processedFiles: [],
7172
);
7273
}
7374

src/Command/AnalysisResult.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ final class AnalysisResult
2424
* @param list<string> $warnings
2525
* @param list<CollectedData> $collectedData
2626
* @param array<string, string> $changedProjectExtensionFilesOutsideOfAnalysedPaths
27+
* @param list<string> $processedFiles
2728
*/
2829
public function __construct(
2930
array $fileSpecificErrors,
@@ -37,6 +38,7 @@ public function __construct(
3738
private int $peakMemoryUsageBytes,
3839
private bool $isResultCacheUsed,
3940
private array $changedProjectExtensionFilesOutsideOfAnalysedPaths,
41+
private array $processedFiles = [],
4042
)
4143
{
4244
usort(
@@ -148,6 +150,14 @@ public function getChangedProjectExtensionFilesOutsideOfAnalysedPaths(): array
148150
return $this->changedProjectExtensionFilesOutsideOfAnalysedPaths;
149151
}
150152

153+
/**
154+
* @return list<string>
155+
*/
156+
public function getProcessedFiles(): array
157+
{
158+
return $this->processedFiles;
159+
}
160+
151161
/**
152162
* @api
153163
* @param list<Error> $fileSpecificErrors
@@ -166,6 +176,7 @@ public function withFileSpecificErrors(array $fileSpecificErrors): self
166176
$this->peakMemoryUsageBytes,
167177
$this->isResultCacheUsed,
168178
$this->changedProjectExtensionFilesOutsideOfAnalysedPaths,
179+
$this->processedFiles,
169180
);
170181
}
171182

0 commit comments

Comments
 (0)