Skip to content

Commit d86cee3

Browse files
SanderMullerclaude
andcommitted
Save the result cache after the rules on CollectedDataNode ran
ResultCacheManager::process() now returns the save as a callback, and both callers call it after AnalyserResultFinalizer::finalize(). Nothing else changes: the same data is saved. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
1 parent 7d3ede5 commit d86cee3

4 files changed

Lines changed: 37 additions & 21 deletions

File tree

‎src/Analyser/ResultCache/ResultCacheManager.php‎

Lines changed: 19 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1356,20 +1356,22 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
13561356
};
13571357

13581358
if ($resultCache->isFullAnalysis()) {
1359-
$saved = false;
1360-
if ($save !== false) {
1359+
return new ResultCacheProcessResult($analyserResult, function () use ($save, $output, $analyserResult, $projectConfigArray, $doSave, $freshErrorsByFile, $freshLocallyIgnoredErrorsByFile, $freshCollectedDataByFile): bool {
1360+
if ($save === false) {
1361+
if ($output->isVeryVerbose()) {
1362+
$output->writeLineFormatted('Result cache was not saved because it was not requested.');
1363+
}
1364+
1365+
return false;
1366+
}
1367+
13611368
$projectExtensionFiles = [];
13621369
if ($analyserResult->getDependencies() !== null) {
13631370
$projectExtensionFiles = $this->getProjectExtensionFiles($projectConfigArray, $analyserResult->getDependencies());
13641371
}
1365-
$saved = $doSave($freshErrorsByFile, $freshLocallyIgnoredErrorsByFile, $analyserResult->getLinesToIgnore(), $analyserResult->getUnmatchedLineIgnores(), $freshCollectedDataByFile, $analyserResult->getDependencies(), $analyserResult->getUsedTraitDependencies(), $analyserResult->getValueDependencies(), $analyserResult->getPackageDependencies(), $this->addNonAnalysedExportedNodes($analyserResult->getExportedNodes(), $analyserResult->getDependencies(), $analyserResult->getUsedTraitDependencies(), CachedExportedNodes::createEmpty()), CachedExportedNodes::createEmpty(), $projectExtensionFiles);
1366-
} else {
1367-
if ($output->isVeryVerbose()) {
1368-
$output->writeLineFormatted('Result cache was not saved because it was not requested.');
1369-
}
1370-
}
13711372

1372-
return new ResultCacheProcessResult($analyserResult, $saved);
1373+
return $doSave($freshErrorsByFile, $freshLocallyIgnoredErrorsByFile, $analyserResult->getLinesToIgnore(), $analyserResult->getUnmatchedLineIgnores(), $freshCollectedDataByFile, $analyserResult->getDependencies(), $analyserResult->getUsedTraitDependencies(), $analyserResult->getValueDependencies(), $analyserResult->getPackageDependencies(), $this->addNonAnalysedExportedNodes($analyserResult->getExportedNodes(), $analyserResult->getDependencies(), $analyserResult->getUsedTraitDependencies(), CachedExportedNodes::createEmpty()), CachedExportedNodes::createEmpty(), $projectExtensionFiles);
1374+
});
13731375
}
13741376

13751377
$errorsByFile = $this->mergeErrors($resultCache, $freshErrorsByFile);
@@ -1385,8 +1387,11 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
13851387
$linesToIgnore = $this->mergeLinesToIgnore($resultCache, $analyserResult->getLinesToIgnore());
13861388
$unmatchedLineIgnores = $this->mergeUnmatchedLineIgnores($resultCache, $analyserResult->getUnmatchedLineIgnores());
13871389

1388-
$saved = false;
1389-
if ($save !== false) {
1390+
$saveCallback = function () use ($save, $resultCache, $projectConfigArray, $doSave, $errorsByFile, $locallyIgnoredErrorsByFile, $linesToIgnore, $unmatchedLineIgnores, $collectedDataByFile, $dependencies, $usedTraitDependencies, $valueDependencies, $packageDependencies, $exportedNodes, $cachedExportedNodes): bool {
1391+
if ($save === false) {
1392+
return false;
1393+
}
1394+
13901395
$projectExtensionFiles = [];
13911396
foreach ($resultCache->getProjectExtensionFiles() as $file => [$hash, $isAnalysed, $className]) {
13921397
if ($isAnalysed) {
@@ -1407,8 +1412,8 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
14071412
$projectExtensionFiles[$file] = [$hash, true, $className];
14081413
}
14091414
}
1410-
$saved = $doSave($errorsByFile, $locallyIgnoredErrorsByFile, $linesToIgnore, $unmatchedLineIgnores, $collectedDataByFile, $dependencies, $usedTraitDependencies, $valueDependencies, $packageDependencies, $exportedNodes, $cachedExportedNodes, $projectExtensionFiles);
1411-
}
1415+
return $doSave($errorsByFile, $locallyIgnoredErrorsByFile, $linesToIgnore, $unmatchedLineIgnores, $collectedDataByFile, $dependencies, $usedTraitDependencies, $valueDependencies, $packageDependencies, $exportedNodes, $cachedExportedNodes, $projectExtensionFiles);
1416+
};
14121417

14131418
$flatErrors = [];
14141419
foreach ($errorsByFile as $fileErrors) {
@@ -1442,7 +1447,7 @@ public function process(AnalyserResult $analyserResult, ResultCache $resultCache
14421447
peakMemoryUsageBytes: $analyserResult->getPeakMemoryUsageBytes(),
14431448
processedFiles: $analyserResult->getProcessedFiles(),
14441449
workerCount: $analyserResult->getWorkerCount(),
1445-
), $saved);
1450+
), $saveCallback);
14461451
}
14471452

14481453
/**

‎src/Analyser/ResultCache/ResultCacheProcessResult.php‎

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,16 @@
22

33
namespace PHPStan\Analyser\ResultCache;
44

5+
use Closure;
56
use PHPStan\Analyser\AnalyserResult;
67

78
final class ResultCacheProcessResult
89
{
910

10-
public function __construct(private AnalyserResult $analyserResult, private bool $saved)
11+
/**
12+
* @param Closure(): bool $saveCallback
13+
*/
14+
public function __construct(private AnalyserResult $analyserResult, private Closure $saveCallback)
1115
{
1216
}
1317

@@ -16,9 +20,15 @@ public function getAnalyserResult(): AnalyserResult
1620
return $this->analyserResult;
1721
}
1822

19-
public function isSaved(): bool
23+
/**
24+
* Writes the result cache, and says whether it was written.
25+
*
26+
* It is separate from ResultCacheManager::process() so that the caller can run the rules on
27+
* CollectedDataNode on the merged result first.
28+
*/
29+
public function save(): bool
2030
{
21-
return $this->saved;
31+
return ($this->saveCallback)();
2232
}
2333

2434
}

‎src/Command/AnalyseApplication.php‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ public function analyse(
140140
$onlyFiles,
141141
$debug,
142142
)->getAnalyserResult();
143+
$savedResultCache = $resultCacheResult->save();
143144
$internalErrors = $analyserResult->getInternalErrors();
144145
$errors = array_merge(
145146
$analyserResult->getErrors(),
@@ -154,7 +155,6 @@ public function analyse(
154155
$fileSpecificErrors = $ignoredErrorHelperProcessedResult->getNotIgnoredErrors();
155156
$notFileSpecificErrors = $ignoredErrorHelperProcessedResult->getOtherIgnoreMessages();
156157
$collectedData = $analyserResult->getCollectedData();
157-
$savedResultCache = $resultCacheResult->isSaved();
158158
}
159159

160160
return new AnalysisResult(

‎src/Command/FixerWorkerRunner.php‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -190,14 +190,15 @@ function (array $errors, array $locallyIgnoredErrors, array $analysedFiles) use
190190
]);
191191
},
192192
)->then(function (AnalyserResult $intermediateAnalyserResult) use ($resultCacheManager, $resultCache, $errorOutput, $isOnlyFiles, $ignoredErrorHelperResult, $inceptionFiles, $out): void {
193-
$analyserResult = $resultCacheManager->process(
193+
$resultCacheResult = $resultCacheManager->process(
194194
$intermediateAnalyserResult,
195195
$resultCache,
196196
$errorOutput,
197197
false,
198198
true,
199-
)->getAnalyserResult();
200-
$finalizerResult = $this->analyserResultFinalizer->finalize($analyserResult, $isOnlyFiles, false);
199+
);
200+
$finalizerResult = $this->analyserResultFinalizer->finalize($resultCacheResult->getAnalyserResult(), $isOnlyFiles, false);
201+
$resultCacheResult->save();
201202

202203
$internalErrors = [];
203204
foreach ($finalizerResult->getAnalyserResult()->getInternalErrors() as $internalError) {

0 commit comments

Comments
 (0)