2017-07-25 22:11:02 +02:00
|
|
|
<?php
|
2018-11-12 16:57:05 +01:00
|
|
|
namespace Psalm\Tests\Internal\Provider;
|
2017-07-25 22:11:02 +02:00
|
|
|
|
2019-06-26 22:52:29 +02:00
|
|
|
use function microtime;
|
|
|
|
use function strpos;
|
2019-07-05 22:24:00 +02:00
|
|
|
use function strtolower;
|
2019-06-26 22:52:29 +02:00
|
|
|
|
2018-11-06 03:57:36 +01:00
|
|
|
class FakeFileProvider extends \Psalm\Internal\Provider\FileProvider
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var array<string, string>
|
|
|
|
*/
|
|
|
|
public $fake_files = [];
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var array<string, int>
|
|
|
|
*/
|
|
|
|
public $fake_file_times = [];
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function fileExists(string $file_path): bool
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
return isset($this->fake_files[$file_path]) || parent::fileExists($file_path);
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getContents(string $file_path, bool $go_to_source = false): string
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
2018-10-26 22:17:15 +02:00
|
|
|
if (!$go_to_source && isset($this->temp_files[strtolower($file_path)])) {
|
|
|
|
return $this->temp_files[strtolower($file_path)];
|
|
|
|
}
|
|
|
|
|
2017-07-25 22:11:02 +02:00
|
|
|
if (isset($this->fake_files[$file_path])) {
|
|
|
|
return $this->fake_files[$file_path];
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getContents($file_path);
|
|
|
|
}
|
|
|
|
|
2017-09-16 18:45:11 +02:00
|
|
|
/**
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-09-07 01:36:47 +02:00
|
|
|
public function setContents(string $file_path, string $file_contents)
|
2017-09-16 18:45:11 +02:00
|
|
|
{
|
|
|
|
$this->fake_files[$file_path] = $file_contents;
|
|
|
|
}
|
|
|
|
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getModifiedTime(string $file_path): int
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
if (isset($this->fake_file_times[$file_path])) {
|
|
|
|
return $this->fake_file_times[$file_path];
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent::getModifiedTime($file_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2018-01-10 16:56:43 +01:00
|
|
|
* @psalm-suppress InvalidPropertyAssignmentValue because microtime is needed for cache busting
|
2017-07-25 22:11:02 +02:00
|
|
|
*/
|
2020-09-12 17:24:05 +02:00
|
|
|
public function registerFile(string $file_path, string $file_contents): void
|
2017-07-25 22:11:02 +02:00
|
|
|
{
|
|
|
|
$this->fake_files[$file_path] = $file_contents;
|
2018-10-10 22:05:06 +02:00
|
|
|
$this->fake_file_times[$file_path] = microtime(true);
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|
2018-10-17 17:03:32 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @param array<string> $file_extensions
|
|
|
|
*
|
|
|
|
* @return array<int, string>
|
|
|
|
*/
|
2020-09-07 01:36:47 +02:00
|
|
|
public function getFilesInDir(string $dir_path, array $file_extensions): array
|
2018-10-17 17:03:32 +02:00
|
|
|
{
|
|
|
|
$file_paths = parent::getFilesInDir($dir_path, $file_extensions);
|
|
|
|
|
|
|
|
foreach ($this->fake_files as $file_path => $_) {
|
|
|
|
if (strpos(strtolower($file_path), strtolower($dir_path)) === 0) {
|
|
|
|
$file_paths[] = $file_path;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $file_paths;
|
|
|
|
}
|
2017-07-25 22:11:02 +02:00
|
|
|
}
|