diff --git a/src/Psalm/Codebase.php b/src/Psalm/Codebase.php index 8ef58c00c0f..56a9aab5d34 100644 --- a/src/Psalm/Codebase.php +++ b/src/Psalm/Codebase.php @@ -533,11 +533,12 @@ public function createClassLikeStorage(string $fq_classlike_name): ClassLikeStor public function cacheClassLikeStorage(ClassLikeStorage $classlike_storage, string $file_path): void { - $file_contents = $this->file_provider->getContents($file_path); - - if ($this->classlike_storage_provider->cache) { - $this->classlike_storage_provider->cache->writeToCache($classlike_storage, $file_path, $file_contents); + if (!$this->classlike_storage_provider->cache) { + return; } + + $file_contents = $this->file_provider->getContents($file_path); + $this->classlike_storage_provider->cache->writeToCache($classlike_storage, $file_path, $file_contents); } public function exhumeClassLikeStorage(string $fq_classlike_name, string $file_path): void diff --git a/src/Psalm/Internal/Cache.php b/src/Psalm/Internal/Cache.php index 3b8edb21038..88e2f5704d7 100644 --- a/src/Psalm/Internal/Cache.php +++ b/src/Psalm/Internal/Cache.php @@ -11,6 +11,7 @@ use function gzinflate; use function igbinary_serialize; use function igbinary_unserialize; +use function is_writable; use function lz4_compress; use function lz4_uncompress; use function serialize; @@ -86,7 +87,7 @@ public function getItem(string $path) public function deleteItem(string $path): void { - if (file_exists($path)) { + if (@is_writable($path)) { @unlink($path); } } diff --git a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php index 8c16dc8b6b8..82eb578fecb 100644 --- a/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php +++ b/src/Psalm/Internal/Provider/ClassLikeStorageCacheProvider.php @@ -18,7 +18,6 @@ use function is_null; use function mkdir; use function strtolower; -use function unlink; use const DIRECTORY_SEPARATOR; use const PHP_VERSION_ID; @@ -95,7 +94,7 @@ public function getLatestFromCache( if (@get_class($cached_value) === '__PHP_Incomplete_Class' || $cache_hash !== $cached_value->hash ) { - unlink($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path)); + $this->cache->deleteItem($this->getCacheLocationForClass($fq_classlike_name_lc, $file_path)); throw new UnexpectedValueException($fq_classlike_name_lc . ' should not be outdated'); } diff --git a/src/Psalm/Internal/Provider/ParserCacheProvider.php b/src/Psalm/Internal/Provider/ParserCacheProvider.php index 7c08d74a459..55aa13e03f7 100644 --- a/src/Psalm/Internal/Provider/ParserCacheProvider.php +++ b/src/Psalm/Internal/Provider/ParserCacheProvider.php @@ -20,13 +20,11 @@ use function is_dir; use function is_readable; use function is_string; -use function is_writable; use function json_decode; use function json_encode; use function mkdir; use function scandir; use function touch; -use function unlink; use const DIRECTORY_SEPARATOR; use const JSON_THROW_ON_ERROR; @@ -319,8 +317,8 @@ public function deleteOldParserCaches(float $time_before): int continue; } - if (filemtime($full_path) < $time_before && is_writable($full_path)) { - unlink($full_path); + if (filemtime($full_path) < $time_before) { + $this->cache->deleteItem($full_path); ++$removed_count; } } diff --git a/src/Psalm/Internal/Provider/ProjectCacheProvider.php b/src/Psalm/Internal/Provider/ProjectCacheProvider.php index b97b0c31cac..4948acc5f24 100644 --- a/src/Psalm/Internal/Provider/ProjectCacheProvider.php +++ b/src/Psalm/Internal/Provider/ProjectCacheProvider.php @@ -78,20 +78,19 @@ public function getLastRun(string $psalm_version): int public function hasLockfileChanged(): bool { - if (!file_exists($this->composer_lock_location)) { - return true; - } - - $lockfile_contents = Providers::safeFileGetContents($this->composer_lock_location); - - if (!$lockfile_contents) { - return true; - } + if (file_exists($this->composer_lock_location)) { + $lockfile_contents = Providers::safeFileGetContents($this->composer_lock_location); + if (!$lockfile_contents) { + return true; + } - if (PHP_VERSION_ID >= 8_01_00) { - $hash = hash('xxh128', $lockfile_contents); + if (PHP_VERSION_ID >= 8_01_00) { + $hash = hash('xxh128', $lockfile_contents); + } else { + $hash = hash('md4', $lockfile_contents); + } } else { - $hash = hash('md4', $lockfile_contents); + $hash = ''; } $changed = $hash !== $this->getComposerLockHash();