Skip to content

Commit

Permalink
Merge pull request #9970 from kkmuffme/missing-composer-lock-should-n…
Browse files Browse the repository at this point in the history
…ot-invalidate-cache-created-with-missing-composer-lock

Missing composer lock should not invalidate cache created with missing composer lock
  • Loading branch information
orklah committed Jul 3, 2023
2 parents 1f92b8d + a83c3fe commit 27798e6
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 23 deletions.
9 changes: 5 additions & 4 deletions src/Psalm/Codebase.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion src/Psalm/Internal/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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');
}
Expand Down
6 changes: 2 additions & 4 deletions src/Psalm/Internal/Provider/ParserCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
Expand Down
23 changes: 11 additions & 12 deletions src/Psalm/Internal/Provider/ProjectCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 27798e6

Please sign in to comment.