1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-11 08:49:52 +01:00
psalm/tests/Internal/Provider/ClassLikeStorageInstanceCacheProvider.php

48 lines
1.3 KiB
PHP
Raw Normal View History

2018-10-12 05:00:32 +02:00
<?php
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
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<lowercase-string, ClassLikeStorage> */
2022-12-16 19:58:47 +01:00
private array $cache = [];
2018-10-12 05:00:32 +02:00
public function __construct()
{
}
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;
}
/**
* @param lowercase-string $fq_classlike_name_lc
*/
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;
}
/**
* @param lowercase-string $fq_classlike_name_lc
*/
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;
}
}