Skip to content

Commit

Permalink
Merge pull request #9932 from ygottschalk/fix/cache-fail-silent
Browse files Browse the repository at this point in the history
Fix/cache fail silent
  • Loading branch information
orklah committed Jun 21, 2023
2 parents 03b9156 + 503c38d commit bb28c5a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 15 deletions.
14 changes: 5 additions & 9 deletions src/Psalm/Internal/Cache.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use Psalm\Config;
use Psalm\Internal\Provider\Providers;
use RuntimeException;

use function file_exists;
use function file_put_contents;
Expand Down Expand Up @@ -68,7 +67,7 @@ public function getItem(string $path)
return null;
}

if ($this->config->use_igbinary) {
if ($this->use_igbinary) {
/** @var object|false $unserialized */
$unserialized = @igbinary_unserialize($inflated);
} else {
Expand Down Expand Up @@ -97,7 +96,7 @@ public function deleteItem(string $path): void
*/
public function saveItem(string $path, $item): void
{
if ($this->config->use_igbinary) {
if ($this->use_igbinary) {
$serialized = igbinary_serialize($item);
} else {
$serialized = serialize($item);
Expand All @@ -115,13 +114,10 @@ public function saveItem(string $path, $item): void
$compressed = $serialized;
}

if ($compressed === false) {
throw new RuntimeException(
'Failed to compress cache data',
);
if ($compressed !== false) {
file_put_contents($path, $compressed, LOCK_EX);
}

file_put_contents($path, $compressed, LOCK_EX);
// TODO: Error handling
}

public function getCacheDirectory(): ?string
Expand Down
4 changes: 3 additions & 1 deletion src/Psalm/Internal/Provider/FileReferenceCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,9 @@ private function getCacheItem(string $type): ?array
}

$cache_item = $this->cache->getItem($cache_location);
if (!is_array($cache_item)) {
if ($cache_item === null) {
return null;
} elseif (!is_array($cache_item)) {
throw new UnexpectedValueException('The reference cache must be an array');
}

Expand Down
18 changes: 13 additions & 5 deletions src/Psalm/Internal/Provider/ParserCacheProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use function is_array;
use function is_dir;
use function is_readable;
use function is_string;
use function is_writable;
use function json_decode;
use function json_encode;
Expand Down Expand Up @@ -89,10 +90,12 @@ public function loadStatementsFromCache(
&& is_readable($cache_location)
&& filemtime($cache_location) > $file_modified_time
) {
/** @var list<Stmt> $stmts */
$stmts = $this->cache->getItem($cache_location);

return $stmts;
if (is_array($stmts)) {
/** @var list<Stmt> $stmts */
return $stmts;
}
}

return null;
Expand All @@ -110,10 +113,12 @@ public function loadExistingStatementsFromCache(string $file_path): ?array
$cache_location = $this->getCacheLocationForPath($file_path, self::PARSER_CACHE_DIRECTORY);

if (is_readable($cache_location)) {
/** @var list<Stmt> $stmts */
$stmts = $this->cache->getItem($cache_location);

return $stmts;
if (is_array($stmts)) {
/** @var list<Stmt> $stmts */
return $stmts;
}
}

return null;
Expand All @@ -127,9 +132,12 @@ public function loadExistingFileContentsFromCache(string $file_path): ?string

$cache_location = $this->getCacheLocationForPath($file_path, self::FILE_CONTENTS_CACHE_DIRECTORY);

/** @var string $cache_item */
$cache_item = $this->cache->getItem($cache_location);

if (!is_string($cache_item)) {
return null;
}

return $cache_item;
}

Expand Down

0 comments on commit bb28c5a

Please sign in to comment.