-
Notifications
You must be signed in to change notification settings - Fork 672
/
Copy pathFakeFileProvider.php
94 lines (77 loc) · 2.57 KB
/
FakeFileProvider.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
<?php
namespace Psalm\Internal\Provider;
use function microtime;
use function strpos;
/**
* @internal
*/
final class FakeFileProvider extends FileProvider
{
/**
* @var array<string, string>
*/
public array $fake_files = [];
/**
* @var array<string, int>
*/
public array $fake_file_times = [];
/**
* @var array<string, true>
*/
public array $fake_directories = [];
public function fileExists(string $file_path): bool
{
return isset($this->fake_files[$file_path]) || parent::fileExists($file_path);
}
public function isDirectory(string $file_path): bool
{
return isset($this->fake_directories[$file_path]) || parent::isDirectory($file_path);
}
/** @psalm-external-mutation-free */
public function getContents(string $file_path, bool $go_to_source = false): string
{
if (!$go_to_source && isset($this->temp_files[$file_path])) {
return $this->temp_files[$file_path]['content'];
}
return $this->fake_files[$file_path] ?? parent::getContents($file_path);
}
public function setContents(string $file_path, string $file_contents): void
{
$this->fake_files[$file_path] = $file_contents;
}
public function setOpenContents(string $file_path, ?string $file_contents = null): void
{
if (isset($this->fake_files[$file_path])) {
$this->fake_files[$file_path] = $file_contents ?? $this->getContents($file_path, true);
}
}
public function getModifiedTime(string $file_path): int
{
return $this->fake_file_times[$file_path] ?? parent::getModifiedTime($file_path);
}
public function registerFile(string $file_path, string $file_contents): void
{
$this->fake_files[$file_path] = $file_contents;
$this->fake_file_times[$file_path] = (int)microtime(true);
}
public function deleteFile(string $file_path): void
{
unset($this->fake_files[$file_path]);
unset($this->fake_file_times[$file_path]);
}
/**
* @param array<string> $file_extensions
* @param null|callable(string):bool $filter
* @return list<string>
*/
public function getFilesInDir(string $dir_path, array $file_extensions, ?callable $filter = null): array
{
$file_paths = parent::getFilesInDir($dir_path, $file_extensions, $filter);
foreach ($this->fake_files as $file_path => $_) {
if (strpos($file_path, $dir_path) === 0) {
$file_paths[] = $file_path;
}
}
return $file_paths;
}
}