Skip to content

Commit dd11e25

Browse files
committed
Result cache - do not restore and save when only files are passed as analysed paths
1 parent d77e326 commit dd11e25

File tree

4 files changed

+22
-8
lines changed

4 files changed

+22
-8
lines changed

src/Analyser/ResultCache/ResultCacheManager.php

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,14 +87,20 @@ public function __construct(
8787
* @param bool $debug
8888
* @return ResultCache
8989
*/
90-
public function restore(array $allAnalysedFiles, bool $debug, Output $output, ?string $resultCacheName = null): ResultCache
90+
public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, Output $output, ?string $resultCacheName = null): ResultCache
9191
{
9292
if ($debug) {
9393
if ($output->isDebug()) {
9494
$output->writeLineFormatted('Result cache not used because of debug mode.');
9595
}
9696
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
9797
}
98+
if ($onlyFiles) {
99+
if ($output->isDebug()) {
100+
$output->writeLineFormatted('Result cache not used because only files were passed as analysed paths.');
101+
}
102+
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
103+
}
98104

99105
$cacheFilePath = $this->cacheFilePath;
100106
if ($resultCacheName !== null) {
@@ -255,15 +261,21 @@ private function exportedNodesChanged(string $analysedFile, array $cachedFileExp
255261
* @return ResultCacheProcessResult
256262
* @throws \PHPStan\ShouldNotHappenException
257263
*/
258-
public function process(AnalyserResult $analyserResult, ResultCache $resultCache, Output $output, $save): ResultCacheProcessResult
264+
public function process(AnalyserResult $analyserResult, ResultCache $resultCache, Output $output, bool $onlyFiles, $save): ResultCacheProcessResult
259265
{
260266
$internalErrors = $analyserResult->getInternalErrors();
261267
$freshErrorsByFile = [];
262268
foreach ($analyserResult->getErrors() as $error) {
263269
$freshErrorsByFile[$error->getFilePath()][] = $error;
264270
}
265271

266-
$doSave = function (array $errorsByFile, ?array $dependencies, array $exportedNodes, ?string $resultCacheName) use ($internalErrors, $resultCache, $output): bool {
272+
$doSave = function (array $errorsByFile, ?array $dependencies, array $exportedNodes, ?string $resultCacheName) use ($internalErrors, $resultCache, $output, $onlyFiles): bool {
273+
if ($onlyFiles) {
274+
if ($output->isDebug()) {
275+
$output->writeLineFormatted('Result cache was not saved because only files were passed as analysed paths.');
276+
}
277+
return false;
278+
}
267279
if ($dependencies === null) {
268280
if ($output->isDebug()) {
269281
$output->writeLineFormatted('Result cache was not saved because of error in dependencies.');

src/Command/AnalyseApplication.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public function analyse(
9393
$errorOutput->writeLineFormatted('Result cache was not saved because of ignoredErrorHelperResult errors.');
9494
}
9595
} else {
96-
$resultCache = $resultCacheManager->restore($files, $debug, $errorOutput);
96+
$resultCache = $resultCacheManager->restore($files, $debug, $onlyFiles, $errorOutput);
9797
$intermediateAnalyserResult = $this->runAnalyser(
9898
$resultCache->getFilesToAnalyse(),
9999
$files,
@@ -103,7 +103,7 @@ public function analyse(
103103
$errorOutput,
104104
$input
105105
);
106-
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, true);
106+
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, $onlyFiles, true);
107107
$analyserResult = $resultCacheResult->getAnalyserResult();
108108
$internalErrors = $analyserResult->getInternalErrors();
109109
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $files, count($internalErrors) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit());

src/Command/FixerApplication.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,7 @@ private function reanalyseWithTmpFile(
470470
{
471471
$resultCacheManager = $this->resultCacheManagerFactory->create([$insteadOfFile => $tmpFile]);
472472
[$inceptionFiles] = $inceptionResult->getFiles();
473-
$resultCache = $resultCacheManager->restore($inceptionFiles, false, $inceptionResult->getErrorOutput());
473+
$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $inceptionResult->getErrorOutput());
474474
$schedule = $this->scheduler->scheduleWork($this->cpuCoreCounter->getNumberOfCpuCores(), $resultCache->getFilesToAnalyse());
475475

476476
$process = new ProcessPromise($loop, $fixerSuggestionId, ProcessHelper::getWorkerCommand(
@@ -508,12 +508,13 @@ private function reanalyseAfterFileChanges(
508508

509509
$resultCacheManager = $this->resultCacheManagerFactory->create([]);
510510
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();
511-
$resultCache = $resultCacheManager->restore($inceptionFiles, false, $inceptionResult->getErrorOutput(), $fixerSuggestionId);
511+
$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $inceptionResult->getErrorOutput(), $fixerSuggestionId);
512512
if (count($resultCache->getFilesToAnalyse()) === 0) {
513513
$result = $resultCacheManager->process(
514514
new AnalyserResult([], [], [], [], false),
515515
$resultCache,
516516
$inceptionResult->getErrorOutput(),
517+
false,
517518
true
518519
)->getAnalyserResult();
519520
$intermediateErrors = $ignoredErrorHelperResult->process(

src/Command/FixerWorkerCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
142142
/** @var ResultCacheManager $resultCacheManager */
143143
$resultCacheManager = $container->getByType(ResultCacheManagerFactory::class)->create($fileReplacements);
144144
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();
145-
$resultCache = $resultCacheManager->restore($inceptionFiles, false, $inceptionResult->getErrorOutput(), $restoreResultCache);
145+
$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $inceptionResult->getErrorOutput(), $restoreResultCache);
146146

147147
$intermediateAnalyserResult = $analyserRunner->runAnalyser(
148148
$resultCache->getFilesToAnalyse(),
@@ -160,6 +160,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
160160
$this->switchTmpFileInAnalyserResult($intermediateAnalyserResult, $tmpFile, $insteadOfFile),
161161
$resultCache,
162162
$inceptionResult->getErrorOutput(),
163+
false,
163164
is_string($saveResultCache) ? $saveResultCache : $saveResultCache === null
164165
)->getAnalyserResult();
165166

0 commit comments

Comments
 (0)