1
0
mirror of https://github.com/danog/psalm.git synced 2025-01-09 14:38:37 +01:00
psalm/src/Psalm/Internal/Provider/ProjectCacheProvider.php

144 lines
3.8 KiB
PHP
Raw Normal View History

<?php
namespace Psalm\Internal\Provider;
2021-06-08 04:55:21 +02:00
use Psalm\Config;
use function file_exists;
use function file_get_contents;
use function file_put_contents;
2021-12-03 21:07:25 +01:00
use function filemtime;
use function hash;
2021-12-03 21:07:25 +01:00
use function mkdir;
use function touch;
2021-06-08 04:55:21 +02:00
use const DIRECTORY_SEPARATOR;
use const PHP_VERSION_ID;
/**
* Used to determine which files reference other files, necessary for using the --diff
* option from the command line.
2022-01-03 07:55:32 +01:00
*
* @internal
*/
class ProjectCacheProvider
{
2020-09-20 18:54:46 +02:00
private const GOOD_RUN_NAME = 'good_run';
private const COMPOSER_LOCK_HASH = 'composer_lock_hash';
/**
* @var int|null
*/
2021-09-26 23:24:07 +02:00
private $last_run;
/**
* @var string|null
*/
2021-09-26 23:24:07 +02:00
private $composer_lock_hash;
private $composer_lock_location;
public function __construct(string $composer_lock_location)
{
$this->composer_lock_location = $composer_lock_location;
}
public function canDiffFiles(): bool
{
$cache_directory = Config::getInstance()->getCacheDirectory();
return $cache_directory && file_exists($cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME);
}
public function processSuccessfulRun(float $start_time, string $psalm_version): void
{
$cache_directory = Config::getInstance()->getCacheDirectory();
if (!$cache_directory) {
return;
}
$run_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME;
file_put_contents($run_cache_location, $psalm_version);
2021-12-03 21:07:25 +01:00
touch($run_cache_location, (int)$start_time);
}
public function getLastRun(string $psalm_version): int
{
if ($this->last_run === null) {
$cache_directory = Config::getInstance()->getCacheDirectory();
$run_cache_location = $cache_directory . DIRECTORY_SEPARATOR . self::GOOD_RUN_NAME;
if (file_exists($run_cache_location) && file_get_contents($run_cache_location) === $psalm_version) {
2021-12-03 21:07:25 +01:00
$this->last_run = filemtime($run_cache_location);
} else {
$this->last_run = 0;
}
}
return $this->last_run;
}
public function hasLockfileChanged(): bool
{
if (!file_exists($this->composer_lock_location)) {
return true;
}
$lockfile_contents = file_get_contents($this->composer_lock_location);
if (!$lockfile_contents) {
return true;
}
2022-05-28 22:26:08 +02:00
if (PHP_VERSION_ID >= 8_01_00) {
$hash = hash('xxh128', $lockfile_contents);
} else {
$hash = hash('md4', $lockfile_contents);
}
$changed = $hash !== $this->getComposerLockHash();
$this->composer_lock_hash = $hash;
return $changed;
}
public function updateComposerLockHash(): void
{
$cache_directory = Config::getInstance()->getCacheDirectory();
if (!$cache_directory || !$this->composer_lock_hash) {
return;
}
if (!file_exists($cache_directory)) {
2021-12-03 21:07:25 +01:00
mkdir($cache_directory, 0777, true);
}
$lock_hash_location = $cache_directory . DIRECTORY_SEPARATOR . self::COMPOSER_LOCK_HASH;
file_put_contents($lock_hash_location, $this->composer_lock_hash);
}
protected function getComposerLockHash(): string
{
if ($this->composer_lock_hash === null) {
$cache_directory = Config::getInstance()->getCacheDirectory();
$lock_hash_location = $cache_directory . DIRECTORY_SEPARATOR . self::COMPOSER_LOCK_HASH;
if (file_exists($lock_hash_location)) {
$this->composer_lock_hash = file_get_contents($lock_hash_location) ?: '';
} else {
$this->composer_lock_hash = '';
}
}
return $this->composer_lock_hash;
}
}