2018-10-12 05:00:32 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2023-10-19 13:12:06 +02:00
|
|
|
declare(strict_types=1);
|
|
|
|
|
2018-11-12 16:57:05 +01:00
|
|
|
namespace Psalm\Tests\Internal\Provider;
|
2018-10-12 05:00:32 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\ClassLikeStorageCacheProvider;
|
2018-10-12 05:00:32 +02:00
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2021-12-03 21:40:18 +01:00
|
|
|
use UnexpectedValueException;
|
2021-06-08 04:55:21 +02:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function strtolower;
|
2018-10-12 05:00:32 +02:00
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class ClassLikeStorageInstanceCacheProvider extends ClassLikeStorageCacheProvider
|
2018-10-12 05:00:32 +02:00
|
|
|
{
|
|
|
|
/** @var array<string, ClassLikeStorage> */
|
2022-12-16 19:58:47 +01:00
|
|
|
private array $cache = [];
|
2018-10-12 05:00:32 +02:00
|
|
|
|
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-10-12 21:02:52 +02:00
|
|
|
public function writeToCache(ClassLikeStorage $storage, ?string $file_path, ?string $file_contents): void
|
2018-10-12 05:00:32 +02:00
|
|
|
{
|
|
|
|
$fq_classlike_name_lc = strtolower($storage->name);
|
|
|
|
$this->cache[$fq_classlike_name_lc] = $storage;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function getLatestFromCache(string $fq_classlike_name_lc, ?string $file_path, ?string $file_contents): ClassLikeStorage
|
2018-10-12 05:00:32 +02:00
|
|
|
{
|
|
|
|
$cached_value = $this->loadFromCache($fq_classlike_name_lc);
|
|
|
|
|
|
|
|
if (!$cached_value) {
|
2021-12-03 21:40:18 +01:00
|
|
|
throw new UnexpectedValueException('Should be in cache');
|
2018-10-12 05:00:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return $cached_value;
|
|
|
|
}
|
|
|
|
|
2022-12-15 03:26:17 +01:00
|
|
|
private function loadFromCache(string $fq_classlike_name_lc): ?ClassLikeStorage
|
2018-10-12 05:00:32 +02:00
|
|
|
{
|
|
|
|
return $this->cache[$fq_classlike_name_lc] ?? null;
|
|
|
|
}
|
|
|
|
}
|