Skip to content

Commit

Permalink
use ALLOWED_CLASSES as Versioning hash
Browse files Browse the repository at this point in the history
  • Loading branch information
gennadigennadigennadi committed Jun 17, 2024
1 parent 72caeb5 commit 961ef60
Showing 1 changed file with 27 additions and 21 deletions.
48 changes: 27 additions & 21 deletions src/Core/Ast/Parser/Cache/AstFileReferenceFileCache.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,24 @@ class AstFileReferenceFileCache implements AstFileReferenceDeferredCacheInterfac
/** @var array<string, bool> */
private array $parsedFiles = [];

public function __construct(private readonly string $cacheFile, private readonly string $cacheVersion) {}
private const ALLOWED_CLASSES = [
FileReference::class,
ClassLikeReference::class,
FunctionReference::class,
VariableReference::class,
AstInherit::class,
DependencyToken::class,
DependencyType::class,
FileToken::class,
ClassLikeToken::class,
ClassLikeType::class,
FunctionToken::class,
SuperGlobalToken::class,
FileOccurrence::class,
DependencyContext::class,
];

public function __construct(private readonly string $cacheFile) {}

public function get(string $filepath): ?FileReference
{
Expand Down Expand Up @@ -98,7 +115,7 @@ public function load(): void

$this->loaded = true;

if (null === $cache || $this->cacheVersion !== $cache['version']) {
if (null === $cache || $this->generateVersionHash() !== $cache['version']) {
return;
}

Expand All @@ -107,25 +124,9 @@ public function load(): void
static function (array $data): array {
$reference = unserialize(
$data['reference'],
[
'allowed_classes' => [
FileReference::class,
ClassLikeReference::class,
FunctionReference::class,
VariableReference::class,
AstInherit::class,
DependencyToken::class,
DependencyType::class,
FileToken::class,
ClassLikeToken::class,
ClassLikeType::class,
FunctionToken::class,
SuperGlobalToken::class,
FileOccurrence::class,
DependencyContext::class,
],
]
['allowed_classes' => self::ALLOWED_CLASSES]
);

assert($reference instanceof FileReference);

return [
Expand Down Expand Up @@ -162,13 +163,18 @@ static function (array $data): array {
$this->cacheFile,
json_encode(
[
'version' => $this->cacheVersion,
'version' => $this->generateVersionHash(),
'payload' => $payload,
]
)
);
}

private function generateVersionHash(): string
{
return hash('sha1', implode(', ', self::ALLOWED_CLASSES));
}

/**
* @throws FileNotExistsException
*/
Expand Down

0 comments on commit 961ef60

Please sign in to comment.