2017-07-29 21:05:06 +02:00
|
|
|
|
<?php
|
2018-11-06 03:57:36 +01:00
|
|
|
|
namespace Psalm\Internal\Provider;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
|
use function array_merge;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
use Psalm\Storage\ClassLikeStorage;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
use function strtolower;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
2018-12-02 00:37:49 +01:00
|
|
|
|
/**
|
|
|
|
|
* @internal
|
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
|
class ClassLikeStorageProvider
|
|
|
|
|
{
|
2018-01-21 16:53:17 +01:00
|
|
|
|
/**
|
|
|
|
|
* Storing this statically is much faster (at least in PHP 7.2.1)
|
|
|
|
|
*
|
|
|
|
|
* @var array<string, ClassLikeStorage>
|
|
|
|
|
*/
|
2017-07-29 21:05:06 +02:00
|
|
|
|
private static $storage = [];
|
|
|
|
|
|
2018-10-18 16:42:37 +02:00
|
|
|
|
/**
|
|
|
|
|
* @var array<string, ClassLikeStorage>
|
|
|
|
|
*/
|
|
|
|
|
private static $new_storage = [];
|
|
|
|
|
|
2018-02-19 06:27:39 +01:00
|
|
|
|
/**
|
2018-09-28 22:18:45 +02:00
|
|
|
|
* @var ?ClassLikeStorageCacheProvider
|
2018-02-19 06:27:39 +01:00
|
|
|
|
*/
|
|
|
|
|
public $cache;
|
|
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
|
public function __construct(ClassLikeStorageCacheProvider $cache = null)
|
2018-02-19 06:27:39 +01:00
|
|
|
|
{
|
|
|
|
|
$this->cache = $cache;
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param string $fq_classlike_name
|
2019-03-16 23:03:37 +01:00
|
|
|
|
* @throws \InvalidArgumentException when class does not exist
|
2019-07-05 22:24:00 +02:00
|
|
|
|
*
|
|
|
|
|
* @return ClassLikeStorage
|
2017-07-29 21:05:06 +02:00
|
|
|
|
*/
|
|
|
|
|
public function get($fq_classlike_name)
|
|
|
|
|
{
|
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
if (!isset(self::$storage[$fq_classlike_name_lc])) {
|
2020-02-15 02:54:26 +01:00
|
|
|
|
throw new \InvalidArgumentException('Could not get class storage for ' . $fq_classlike_name_lc);
|
2017-07-29 21:05:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return self::$storage[$fq_classlike_name_lc];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2018-02-19 06:27:39 +01:00
|
|
|
|
* @param string $fq_classlike_name
|
2017-07-29 21:05:06 +02:00
|
|
|
|
*
|
|
|
|
|
* @return bool
|
|
|
|
|
*/
|
|
|
|
|
public function has($fq_classlike_name)
|
|
|
|
|
{
|
2018-02-19 06:27:39 +01:00
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
|
|
|
|
return isset(self::$storage[$fq_classlike_name_lc]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
|
* @param string|null $file_path
|
|
|
|
|
* @param string|null $file_contents
|
|
|
|
|
*
|
|
|
|
|
* @return ClassLikeStorage
|
|
|
|
|
*/
|
|
|
|
|
public function exhume($fq_classlike_name, $file_path, $file_contents)
|
|
|
|
|
{
|
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
|
|
|
|
if (isset(self::$storage[$fq_classlike_name_lc])) {
|
2018-02-19 17:53:30 +01:00
|
|
|
|
return self::$storage[$fq_classlike_name_lc];
|
2018-02-19 06:27:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
|
if (!$this->cache) {
|
|
|
|
|
throw new \LogicException('Cannot exhume when there’s no cache');
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 16:42:37 +02:00
|
|
|
|
$cached_value = $this->cache->getLatestFromCache($fq_classlike_name_lc, $file_path, $file_contents);
|
|
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
|
self::$storage[$fq_classlike_name_lc] = $cached_value;
|
|
|
|
|
self::$new_storage[$fq_classlike_name_lc] = $cached_value;
|
2018-02-19 06:27:39 +01:00
|
|
|
|
|
|
|
|
|
return $cached_value;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return array<string, ClassLikeStorage>
|
|
|
|
|
*/
|
|
|
|
|
public function getAll()
|
|
|
|
|
{
|
|
|
|
|
return self::$storage;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-18 16:42:37 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return array<string, ClassLikeStorage>
|
|
|
|
|
*/
|
|
|
|
|
public function getNew()
|
|
|
|
|
{
|
|
|
|
|
return self::$new_storage;
|
|
|
|
|
}
|
|
|
|
|
|
2018-10-11 19:58:39 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param array<string, ClassLikeStorage> $more
|
2019-07-05 22:24:00 +02:00
|
|
|
|
*
|
2018-10-11 19:58:39 +02:00
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function addMore(array $more)
|
|
|
|
|
{
|
2019-01-16 05:24:29 +01:00
|
|
|
|
self::$new_storage = array_merge(self::$new_storage, $more);
|
|
|
|
|
self::$storage = array_merge(self::$storage, $more);
|
2018-10-11 19:58:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-01-10 22:59:44 +01:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function makeNew(string $fq_classlike_name_lc)
|
|
|
|
|
{
|
|
|
|
|
self::$new_storage[$fq_classlike_name_lc] = self::$storage[$fq_classlike_name_lc];
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
|
/**
|
|
|
|
|
* @param string $fq_classlike_name
|
|
|
|
|
*
|
|
|
|
|
* @return ClassLikeStorage
|
|
|
|
|
*/
|
|
|
|
|
public function create($fq_classlike_name)
|
|
|
|
|
{
|
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
2018-10-18 16:42:37 +02:00
|
|
|
|
$storage = new ClassLikeStorage($fq_classlike_name);
|
|
|
|
|
self::$storage[$fq_classlike_name_lc] = $storage;
|
|
|
|
|
self::$new_storage[$fq_classlike_name_lc] = $storage;
|
2017-07-29 21:05:06 +02:00
|
|
|
|
|
|
|
|
|
return $storage;
|
|
|
|
|
}
|
|
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
|
/**
|
2020-02-15 02:54:26 +01:00
|
|
|
|
* @param string $fq_classlike_name
|
2018-09-28 22:18:45 +02:00
|
|
|
|
*
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public function remove($fq_classlike_name)
|
|
|
|
|
{
|
2020-02-15 02:54:26 +01:00
|
|
|
|
$fq_classlike_name_lc = strtolower($fq_classlike_name);
|
|
|
|
|
|
|
|
|
|
unset(self::$storage[$fq_classlike_name_lc]);
|
2018-09-28 22:18:45 +02:00
|
|
|
|
}
|
|
|
|
|
|
2017-07-29 21:05:06 +02:00
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
2018-01-21 18:44:46 +01:00
|
|
|
|
public static function deleteAll()
|
2017-07-29 21:05:06 +02:00
|
|
|
|
{
|
|
|
|
|
self::$storage = [];
|
|
|
|
|
}
|
2018-10-18 16:42:37 +02:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return void
|
|
|
|
|
*/
|
|
|
|
|
public static function populated()
|
|
|
|
|
{
|
|
|
|
|
self::$new_storage = [];
|
|
|
|
|
}
|
2017-07-29 21:05:06 +02:00
|
|
|
|
}
|