Skip to content

Commit

Permalink
Result-Cache: Indicate key differences in cache metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
staabm committed Sep 18, 2023
1 parent e299da5 commit 11adf72
Showing 1 changed file with 34 additions and 1 deletion.
35 changes: 34 additions & 1 deletion src/Analyser/ResultCache/ResultCacheManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
use function array_values;
use function count;
use function get_loaded_extensions;
use function implode;
use function is_array;
use function is_file;
use function is_string;
Expand Down Expand Up @@ -128,7 +129,8 @@ public function restore(array $allAnalysedFiles, bool $debug, bool $onlyFiles, ?
$meta = $this->getMeta($allAnalysedFiles, $projectConfigArray);
if ($this->isMetaDifferent($data['meta'], $meta)) {
if ($output->isDebug()) {
$output->writeLineFormatted('Result cache not used because the metadata do not match.');
$diffs = $this->getMetaKeyDifferences($data['meta'], $meta);
$output->writeLineFormatted('Result cache not used because the metadata do not match: ' . implode(', ', $diffs));
}
return new ResultCache($allAnalysedFiles, true, time(), $meta, [], [], [], []);
}
Expand Down Expand Up @@ -272,6 +274,37 @@ private function isMetaDifferent(array $cachedMeta, array $currentMeta): bool
return $cachedMeta !== $currentMeta;
}

/**
* @param mixed[] $cachedMeta
* @param mixed[] $currentMeta
*
* @return string[]
*/
private function getMetaKeyDifferences(array $cachedMeta, array $currentMeta): array
{
$diffs = [];
foreach ($cachedMeta as $key => $value) {
if (!array_key_exists($key, $currentMeta)) {
$diffs[] = $key;
continue;
}

if ($value === $currentMeta[$key]) {
continue;
}

$diffs[] = $key;
}

if ($diffs === []) {
// when none of the keys is different,
// the order of the keys is the problem
$diffs[] = 'keyOrder';
}

return $diffs;
}

/**
* @param array<int, RootExportedNode> $cachedFileExportedNodes
* @return bool|null null means nothing changed, true means new root symbol appeared, false means nested node changed
Expand Down

0 comments on commit 11adf72

Please sign in to comment.