diff --git a/src/Cache/Cache.php b/src/Cache/Cache.php index 83472f05de..3e9b97cf3b 100644 --- a/src/Cache/Cache.php +++ b/src/Cache/Cache.php @@ -17,19 +17,20 @@ public function __construct(CacheStorage $storage) * @param string $key * @return mixed|null */ - public function load(string $key) + public function load(string $key, string $variableKey) { - return $this->storage->load($key); + return $this->storage->load($key, $variableKey); } /** * @param string $key + * @param string $variableKey * @param mixed $data * @return void */ - public function save(string $key, $data): void + public function save(string $key, string $variableKey, $data): void { - $this->storage->save($key, $data); + $this->storage->save($key, $variableKey, $data); } } diff --git a/src/Cache/CacheItem.php b/src/Cache/CacheItem.php new file mode 100644 index 0000000000..10e1ccb0a2 --- /dev/null +++ b/src/Cache/CacheItem.php @@ -0,0 +1,46 @@ +variableKey = $variableKey; + $this->data = $data; + } + + public function isVariableKeyValid(string $variableKey): bool + { + return $this->variableKey === $variableKey; + } + + /** + * @return mixed + */ + public function getData() + { + return $this->data; + } + + /** + * @param mixed[] $properties + * @return self + */ + public static function __set_state(array $properties): self + { + return new self($properties['variableKey'], $properties['data']); + } + +} diff --git a/src/Cache/CacheStorage.php b/src/Cache/CacheStorage.php index e63091b5fc..a9227b0eca 100644 --- a/src/Cache/CacheStorage.php +++ b/src/Cache/CacheStorage.php @@ -7,15 +7,17 @@ interface CacheStorage /** * @param string $key + * @param string $variableKey * @return mixed|null */ - public function load(string $key); + public function load(string $key, string $variableKey); /** * @param string $key + * @param string $variableKey * @param mixed $data * @return void */ - public function save(string $key, $data): void; + public function save(string $key, string $variableKey, $data): void; } diff --git a/src/Cache/FileCacheStorage.php b/src/Cache/FileCacheStorage.php index df7b8f6380..2593d1e213 100644 --- a/src/Cache/FileCacheStorage.php +++ b/src/Cache/FileCacheStorage.php @@ -23,22 +23,36 @@ private function makeDir(string $directory): void /** * @param string $key + * @param string $variableKey * @return mixed|null */ - public function load(string $key) + public function load(string $key, string $variableKey) { - return (function (string $key) { + return (function (string $key, string $variableKey) { $filePath = $this->getFilePath($key); - return is_file($filePath) ? require $filePath : null; - })($key); + if (!is_file($filePath)) { + return null; + } + + $cacheItem = require $filePath; + if (!$cacheItem instanceof CacheItem) { + return null; + } + if (!$cacheItem->isVariableKeyValid($variableKey)) { + return null; + } + + return $cacheItem->getData(); + })($key, $variableKey); } /** * @param string $key + * @param string $variableKey * @param mixed $data * @return void */ - public function save(string $key, $data): void + public function save(string $key, string $variableKey, $data): void { $path = $this->getFilePath($key); $this->makeDir(dirname($path)); @@ -46,7 +60,7 @@ public function save(string $key, $data): void $path, sprintf( " */ private $storage = []; /** * @param string $key + * @param string $variableKey * @return mixed|null */ - public function load(string $key) + public function load(string $key, string $variableKey) { - return $this->storage[$key] ?? null; + if (!isset($this->storage[$key])) { + return null; + } + + $item = $this->storage[$key]; + if (!$item->isVariableKeyValid($variableKey)) { + return null; + } + + return $item->getData(); } /** * @param string $key + * @param string $variableKey * @param mixed $data * @return void */ - public function save(string $key, $data): void + public function save(string $key, string $variableKey, $data): void { - $this->storage[$key] = $data; + $this->storage[$key] = new CacheItem($variableKey, $data); } } diff --git a/src/Reflection/Php/PhpFunctionReflection.php b/src/Reflection/Php/PhpFunctionReflection.php index f07f472d66..42c31d3b96 100644 --- a/src/Reflection/Php/PhpFunctionReflection.php +++ b/src/Reflection/Php/PhpFunctionReflection.php @@ -171,12 +171,13 @@ private function isVariadic(): bool if ($modifiedTime === false) { $modifiedTime = time(); } - $key = sprintf('variadic-function-%s-%s-%d-v1', $functionName, $fileName, $modifiedTime); - $cachedResult = $this->cache->load($key); + $variableCacheKey = sprintf('%d-v1', $modifiedTime); + $key = sprintf('variadic-function-%s-%s', $functionName, $fileName); + $cachedResult = $this->cache->load($key, $variableCacheKey); if ($cachedResult === null) { $nodes = $this->parser->parseFile($fileName); $result = $this->callsFuncGetArgs($nodes); - $this->cache->save($key, $result); + $this->cache->save($key, $variableCacheKey, $result); return $result; } diff --git a/src/Reflection/Php/PhpMethodReflection.php b/src/Reflection/Php/PhpMethodReflection.php index 9a55bb7afa..48da1a8f06 100644 --- a/src/Reflection/Php/PhpMethodReflection.php +++ b/src/Reflection/Php/PhpMethodReflection.php @@ -281,12 +281,13 @@ private function isVariadic(): bool if ($modifiedTime === false) { $modifiedTime = time(); } - $key = sprintf('variadic-method-%s-%s-%s-%d-v2', $declaringClass->getName(), $this->reflection->getName(), $filename, $modifiedTime); - $cachedResult = $this->cache->load($key); + $key = sprintf('variadic-method-%s-%s-%s', $declaringClass->getName(), $this->reflection->getName(), $filename); + $variableCacheKey = sprintf('%d-v2', $modifiedTime); + $cachedResult = $this->cache->load($key, $variableCacheKey); if ($cachedResult === null || !is_bool($cachedResult)) { $nodes = $this->parser->parseFile($filename); $result = $this->callsFuncGetArgs($declaringClass, $nodes); - $this->cache->save($key, $result); + $this->cache->save($key, $variableCacheKey, $result); return $result; } diff --git a/src/Type/FileTypeMapper.php b/src/Type/FileTypeMapper.php index 589848d65e..09816ce3dc 100644 --- a/src/Type/FileTypeMapper.php +++ b/src/Type/FileTypeMapper.php @@ -152,15 +152,15 @@ private function resolvePhpDocStringToDocNode(string $phpDocString): PhpDocNode } catch (\OutOfBoundsException $e) { // skip } - $cacheKey = sprintf('phpdocstring-%s-%s', $phpDocString, $phpDocParserVersion); - $phpDocNodeSerializedString = $this->cache->load($cacheKey); + $cacheKey = sprintf('phpdocstring-%s', $phpDocString); + $phpDocNodeSerializedString = $this->cache->load($cacheKey, $phpDocParserVersion); if ($phpDocNodeSerializedString !== null) { return unserialize($phpDocNodeSerializedString); } $phpDocNode = $this->phpDocStringResolver->resolve($phpDocString); if ($this->shouldPhpDocNodeBeCachedToDisk($phpDocNode)) { - $this->cache->save($cacheKey, serialize($phpDocNode)); + $this->cache->save($cacheKey, $phpDocParserVersion, serialize($phpDocNode)); } return $phpDocNode; @@ -190,12 +190,13 @@ private function getResolvedPhpDocMap(string $fileName): array if ($modifiedTime === false) { $modifiedTime = time(); } - $cacheKey = sprintf('%s-phpdocstring-%d', $fileName, $modifiedTime); - $map = $this->cache->load($cacheKey); + $cacheKey = sprintf('%s-phpdocstring', $fileName); + $modifiedTimeString = sprintf('%d', $modifiedTime); + $map = $this->cache->load($cacheKey, $modifiedTimeString); if ($map === null) { $map = $this->createResolvedPhpDocMap($fileName); - $this->cache->save($cacheKey, $map); + $this->cache->save($cacheKey, $modifiedTimeString, $map); } $this->memoryCache[$fileName] = $map;