From 1b1915eaf51f19f7944d2b53d3bdaa9ce2d79989 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tinjo=20Sch=C3=B6ni?= <32767367+tscni@users.noreply.github.com> Date: Sat, 24 Jun 2023 04:35:26 +0200 Subject: [PATCH] Make file storage cache handling consistent --- .../Provider/FileStorageCacheProvider.php | 24 +++++++------------ .../Provider/FileStorageCacheProvider.php | 15 ++++++++++-- .../FileStorageInstanceCacheProvider.php | 24 +++++++------------ 3 files changed, 31 insertions(+), 32 deletions(-) diff --git a/src/Psalm/Internal/LanguageServer/Provider/FileStorageCacheProvider.php b/src/Psalm/Internal/LanguageServer/Provider/FileStorageCacheProvider.php index c91ffcc14..8841cf407 100644 --- a/src/Psalm/Internal/LanguageServer/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Internal/LanguageServer/Provider/FileStorageCacheProvider.php @@ -19,30 +19,24 @@ class FileStorageCacheProvider extends InternalFileStorageCacheProvider { } - public function writeToCache(FileStorage $storage, string $file_contents): void + /** + * @param lowercase-string $file_path + */ + protected function storeInCache(string $file_path, FileStorage $storage): void { - $file_path = strtolower($storage->file_path); $this->cache[$file_path] = $storage; } - public function getLatestFromCache(string $file_path, string $file_contents): ?FileStorage - { - $cached_value = $this->loadFromCache(strtolower($file_path)); - - if (!$cached_value) { - return null; - } - - return $cached_value; - } - public function removeCacheForFile(string $file_path): void { unset($this->cache[strtolower($file_path)]); } - private function loadFromCache(string $file_path): ?FileStorage + /** + * @param lowercase-string $file_path + */ + protected function loadFromCache(string $file_path): ?FileStorage { - return $this->cache[strtolower($file_path)] ?? null; + return $this->cache[$file_path] ?? null; } } diff --git a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php index 6117d9634..9fcb7d9d3 100644 --- a/src/Psalm/Internal/Provider/FileStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/FileStorageCacheProvider.php @@ -64,9 +64,17 @@ class FileStorageCacheProvider public function writeToCache(FileStorage $storage, string $file_contents): void { $file_path = strtolower($storage->file_path); - $cache_location = $this->getCacheLocationForPath($file_path, true); $storage->hash = $this->getCacheHash($file_path, $file_contents); + $this->storeInCache($file_path, $storage); + } + + /** + * @param lowercase-string $file_path + */ + protected function storeInCache(string $file_path, FileStorage $storage): void + { + $cache_location = $this->getCacheLocationForPath($file_path, true); $this->cache->saveItem($cache_location, $storage); } @@ -107,7 +115,10 @@ class FileStorageCacheProvider return PHP_VERSION_ID >= 8_01_00 ? hash('xxh128', $data) : hash('md4', $data); } - private function loadFromCache(string $file_path): ?FileStorage + /** + * @param lowercase-string $file_path + */ + protected function loadFromCache(string $file_path): ?FileStorage { $storage = $this->cache->getItem($this->getCacheLocationForPath($file_path)); if ($storage instanceof FileStorage) { diff --git a/tests/Internal/Provider/FileStorageInstanceCacheProvider.php b/tests/Internal/Provider/FileStorageInstanceCacheProvider.php index 7889520c3..b6a732ae1 100644 --- a/tests/Internal/Provider/FileStorageInstanceCacheProvider.php +++ b/tests/Internal/Provider/FileStorageInstanceCacheProvider.php @@ -16,30 +16,24 @@ class FileStorageInstanceCacheProvider extends FileStorageCacheProvider { } - public function writeToCache(FileStorage $storage, string $file_contents): void + /** + * @param lowercase-string $file_path + */ + protected function storeInCache(string $file_path, FileStorage $storage): void { - $file_path = strtolower($storage->file_path); $this->cache[$file_path] = $storage; } - public function getLatestFromCache(string $file_path, string $file_contents): ?FileStorage - { - $cached_value = $this->loadFromCache(strtolower($file_path)); - - if (!$cached_value) { - return null; - } - - return $cached_value; - } - public function removeCacheForFile(string $file_path): void { unset($this->cache[strtolower($file_path)]); } - private function loadFromCache(string $file_path): ?FileStorage + /** + * @param lowercase-string $file_path + */ + protected function loadFromCache(string $file_path): ?FileStorage { - return $this->cache[strtolower($file_path)] ?? null; + return $this->cache[$file_path] ?? null; } }