Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix/cache fail silent #9932

Merged
merged 2 commits into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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