Skip to content

Commit

Permalink
Result cache - do not restore and save when only files are passed as …
Browse files Browse the repository at this point in the history
…analysed paths
ondrejmirtes committed Nov 18, 2020
1 parent d77e326 commit dd11e25
Showing 4 changed files with 22 additions and 8 deletions.
18 changes: 15 additions & 3 deletions src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
@@ -87,14 +87,20 @@ public function __construct(
* @param bool $debug
* @return ResultCache
*/
public function restore(array $allAnalysedFiles, bool $debug, Output $output, ?string $resultCacheName = null): ResultCache
public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, Output $output, ?string $resultCacheName = null): ResultCache
{
if ($debug) {
if ($output->isDebug()) {
$output->writeLineFormatted('Result cache not used because of debug mode.');
}
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
}
if ($onlyFiles) {
if ($output->isDebug()) {
$output->writeLineFormatted('Result cache not used because only files were passed as analysed paths.');
}
return new ResultCache($allAnalysedFiles, true, time(), [], [], []);
}

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

$doSave = function (array $errorsByFile, ?array $dependencies, array $exportedNodes, ?string $resultCacheName) use ($internalErrors, $resultCache, $output): bool {
$doSave = function (array $errorsByFile, ?array $dependencies, array $exportedNodes, ?string $resultCacheName) use ($internalErrors, $resultCache, $output, $onlyFiles): bool {
if ($onlyFiles) {
if ($output->isDebug()) {
$output->writeLineFormatted('Result cache was not saved because only files were passed as analysed paths.');
}
return false;
}
if ($dependencies === null) {
if ($output->isDebug()) {
$output->writeLineFormatted('Result cache was not saved because of error in dependencies.');
4 changes: 2 additions & 2 deletions src/Command/AnalyseApplication.php
Original file line number Diff line number Diff line change
@@ -93,7 +93,7 @@ public function analyse(
$errorOutput->writeLineFormatted('Result cache was not saved because of ignoredErrorHelperResult errors.');
}
} else {
$resultCache = $resultCacheManager->restore($files, $debug, $errorOutput);
$resultCache = $resultCacheManager->restore($files, $debug, $onlyFiles, $errorOutput);
$intermediateAnalyserResult = $this->runAnalyser(
$resultCache->getFilesToAnalyse(),
$files,
@@ -103,7 +103,7 @@ public function analyse(
$errorOutput,
$input
);
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, true);
$resultCacheResult = $resultCacheManager->process($intermediateAnalyserResult, $resultCache, $errorOutput, $onlyFiles, true);
$analyserResult = $resultCacheResult->getAnalyserResult();
$internalErrors = $analyserResult->getInternalErrors();
$errors = $ignoredErrorHelperResult->process($analyserResult->getErrors(), $onlyFiles, $files, count($internalErrors) > 0 || $analyserResult->hasReachedInternalErrorsCountLimit());
5 changes: 3 additions & 2 deletions src/Command/FixerApplication.php
Original file line number Diff line number Diff line change
@@ -470,7 +470,7 @@ private function reanalyseWithTmpFile(
{
$resultCacheManager = $this->resultCacheManagerFactory->create([$insteadOfFile => $tmpFile]);
[$inceptionFiles] = $inceptionResult->getFiles();
$resultCache = $resultCacheManager->restore($inceptionFiles, false, $inceptionResult->getErrorOutput());
$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $inceptionResult->getErrorOutput());
$schedule = $this->scheduler->scheduleWork($this->cpuCoreCounter->getNumberOfCpuCores(), $resultCache->getFilesToAnalyse());

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

$resultCacheManager = $this->resultCacheManagerFactory->create([]);
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();
$resultCache = $resultCacheManager->restore($inceptionFiles, false, $inceptionResult->getErrorOutput(), $fixerSuggestionId);
$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $inceptionResult->getErrorOutput(), $fixerSuggestionId);
if (count($resultCache->getFilesToAnalyse()) === 0) {
$result = $resultCacheManager->process(
new AnalyserResult([], [], [], [], false),
$resultCache,
$inceptionResult->getErrorOutput(),
false,
true
)->getAnalyserResult();
$intermediateErrors = $ignoredErrorHelperResult->process(
3 changes: 2 additions & 1 deletion src/Command/FixerWorkerCommand.php
Original file line number Diff line number Diff line change
@@ -142,7 +142,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
/** @var ResultCacheManager $resultCacheManager */
$resultCacheManager = $container->getByType(ResultCacheManagerFactory::class)->create($fileReplacements);
[$inceptionFiles, $isOnlyFiles] = $inceptionResult->getFiles();
$resultCache = $resultCacheManager->restore($inceptionFiles, false, $inceptionResult->getErrorOutput(), $restoreResultCache);
$resultCache = $resultCacheManager->restore($inceptionFiles, false, false, $inceptionResult->getErrorOutput(), $restoreResultCache);

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

0 comments on commit dd11e25

Please sign in to comment.