2018-09-28 22:18:45 +02:00
|
|
|
<?php
|
2021-12-15 04:58:32 +01:00
|
|
|
|
2018-11-12 16:57:05 +01:00
|
|
|
namespace Psalm\Tests\Internal\Provider;
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2019-07-05 22:24:00 +02:00
|
|
|
use PhpParser;
|
2021-12-03 20:11:20 +01:00
|
|
|
use Psalm\Internal\Provider\ParserCacheProvider;
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2021-06-08 04:55:21 +02:00
|
|
|
use function microtime;
|
|
|
|
|
2021-12-03 20:11:20 +01:00
|
|
|
class ParserInstanceCacheProvider extends ParserCacheProvider
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2022-12-16 19:58:47 +01:00
|
|
|
private array $file_contents_cache = [];
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2018-10-26 06:59:14 +02:00
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
2022-12-16 19:58:47 +01:00
|
|
|
private array $file_content_hash = [];
|
2018-10-26 06:59:14 +02:00
|
|
|
|
2018-09-28 22:18:45 +02:00
|
|
|
/**
|
2019-10-09 01:06:16 +02:00
|
|
|
* @var array<string, list<PhpParser\Node\Stmt>>
|
2018-09-28 22:18:45 +02:00
|
|
|
*/
|
2022-12-16 19:58:47 +01:00
|
|
|
private array $statements_cache = [];
|
2018-09-28 22:18:45 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, float>
|
|
|
|
*/
|
2022-12-16 19:58:47 +01:00
|
|
|
private array $statements_cache_time = [];
|
2018-09-28 22:18:45 +02:00
|
|
|
|
2018-10-16 21:59:11 +02:00
|
|
|
public function __construct()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function loadStatementsFromCache(string $file_path, int $file_modified_time, string $file_content_hash): ?array
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
2018-10-16 21:59:11 +02:00
|
|
|
if (isset($this->statements_cache[$file_path])
|
|
|
|
&& $this->statements_cache_time[$file_path] >= $file_modified_time
|
2018-10-26 06:59:14 +02:00
|
|
|
&& $this->file_content_hash[$file_path] === $file_content_hash
|
2018-09-28 22:18:45 +02:00
|
|
|
) {
|
2018-10-16 21:59:11 +02:00
|
|
|
return $this->statements_cache[$file_path];
|
2018-09-28 22:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-09 01:06:16 +02:00
|
|
|
* @return list<PhpParser\Node\Stmt>|null
|
2018-09-28 22:18:45 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function loadExistingStatementsFromCache(string $file_path): ?array
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
2018-10-16 21:59:11 +02:00
|
|
|
if (isset($this->statements_cache[$file_path])) {
|
|
|
|
return $this->statements_cache[$file_path];
|
2018-09-28 22:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2019-10-09 01:06:16 +02:00
|
|
|
* @param list<PhpParser\Node\Stmt> $stmts
|
2018-09-28 22:18:45 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function saveStatementsToCache(string $file_path, string $file_content_hash, array $stmts, bool $touch_only): void
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
2018-10-16 21:59:11 +02:00
|
|
|
$this->statements_cache[$file_path] = $stmts;
|
|
|
|
$this->statements_cache_time[$file_path] = microtime(true);
|
2018-10-26 06:59:14 +02:00
|
|
|
$this->file_content_hash[$file_path] = $file_content_hash;
|
2018-09-28 22:18:45 +02:00
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function loadExistingFileContentsFromCache(string $file_path): ?string
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
2018-10-16 21:59:11 +02:00
|
|
|
if (isset($this->file_contents_cache[$file_path])) {
|
|
|
|
return $this->file_contents_cache[$file_path];
|
2018-09-28 22:18:45 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function cacheFileContents(string $file_path, string $file_contents): void
|
2018-09-28 22:18:45 +02:00
|
|
|
{
|
2018-10-16 21:59:11 +02:00
|
|
|
$this->file_contents_cache[$file_path] = $file_contents;
|
2018-09-28 22:18:45 +02:00
|
|
|
}
|
2020-06-06 22:57:25 +02:00
|
|
|
|
2022-08-16 15:48:49 +02:00
|
|
|
public function deleteOldParserCaches(float $time_before): int
|
|
|
|
{
|
|
|
|
$this->existing_file_content_hashes = null;
|
|
|
|
$this->new_file_content_hashes = [];
|
|
|
|
|
|
|
|
$this->file_contents_cache = [];
|
|
|
|
$this->file_content_hash = [];
|
|
|
|
$this->statements_cache = [];
|
|
|
|
$this->statements_cache_time = [];
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-09-12 17:24:05 +02:00
|
|
|
public function saveFileContentHashes(): void
|
2020-06-06 22:57:25 +02:00
|
|
|
{
|
|
|
|
}
|
2018-09-28 22:18:45 +02:00
|
|
|
}
|