1
0
mirror of https://github.com/danog/psalm.git synced 2024-12-02 17:52:45 +01:00
psalm/tests/Internal/Provider/ParserInstanceCacheProvider.php

132 lines
3.0 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Tests\Internal\Provider;
use function microtime;
2019-07-05 22:24:00 +02:00
use PhpParser;
2018-11-06 03:57:36 +01:00
class ParserInstanceCacheProvider extends \Psalm\Internal\Provider\ParserCacheProvider
{
/**
* @var array<string, string>
*/
private $file_contents_cache = [];
/**
* @var array<string, string>
*/
private $file_content_hash = [];
/**
2019-10-09 01:06:16 +02:00
* @var array<string, list<PhpParser\Node\Stmt>>
*/
private $statements_cache = [];
/**
* @var array<string, float>
*/
private $statements_cache_time = [];
/**
* @var int
*/
private $last_good_run = 0;
public function __construct()
{
}
public function loadStatementsFromCache($file_path, $file_modified_time, $file_content_hash)
{
if (isset($this->statements_cache[$file_path])
&& $this->statements_cache_time[$file_path] >= $file_modified_time
&& $this->file_content_hash[$file_path] === $file_content_hash
) {
return $this->statements_cache[$file_path];
}
return null;
}
/**
* @param string $file_content_hash
* @param string $file_path
* @param mixed $file_modified_time
*
2019-10-09 01:06:16 +02:00
* @return list<PhpParser\Node\Stmt>|null
*/
public function loadExistingStatementsFromCache($file_path)
{
if (isset($this->statements_cache[$file_path])) {
return $this->statements_cache[$file_path];
}
return null;
}
/**
* @param string $file_path
* @param string $file_content_hash
2019-10-09 01:06:16 +02:00
* @param list<PhpParser\Node\Stmt> $stmts
* @param bool $touch_only
*
* @return void
*/
public function saveStatementsToCache($file_path, $file_content_hash, array $stmts, $touch_only)
{
$this->statements_cache[$file_path] = $stmts;
$this->statements_cache_time[$file_path] = microtime(true);
$this->file_content_hash[$file_path] = $file_content_hash;
}
/**
* @param string $file_path
*
* @return string|null
*/
public function loadExistingFileContentsFromCache($file_path)
{
if (isset($this->file_contents_cache[$file_path])) {
return $this->file_contents_cache[$file_path];
}
return null;
}
/**
* @param string $file_path
* @param string $file_contents
*
* @return void
*/
public function cacheFileContents($file_path, $file_contents)
{
$this->file_contents_cache[$file_path] = $file_contents;
}
/**
* @return int
*/
public function getLastGoodRun()
{
return $this->last_good_run;
}
/**
* @param float $start_time
*
* @return void
*/
public function processSuccessfulRun($start_time)
{
$this->last_good_run = (int) $start_time;
}
/**
* @return bool
*/
public function canDiffFiles()
{
return $this->last_good_run > 0;
}
}