-
Notifications
You must be signed in to change notification settings - Fork 664
/
FileStorageProvider.php
126 lines (99 loc) · 2.8 KB
/
FileStorageProvider.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php
declare(strict_types=1);
namespace Psalm\Internal\Provider;
use InvalidArgumentException;
use Psalm\Storage\FileStorage;
use function strtolower;
/**
* @internal
*/
final class FileStorageProvider
{
/**
* A list of data useful to analyse files
* Storing this statically is much faster (at least in PHP 7.2.1)
*
* @var array<lowercase-string, FileStorage>
*/
private static array $storage = [];
/**
* A list of data useful to analyse new files
* Storing this statically is much faster (at least in PHP 7.2.1)
*
* @var array<string, FileStorage>
*/
private static array $new_storage = [];
public function __construct(public ?FileStorageCacheProvider $cache = null)
{
}
public function get(string $file_path): FileStorage
{
$file_path = strtolower($file_path);
if (!isset(self::$storage[$file_path])) {
throw new InvalidArgumentException('Could not get file storage for ' . $file_path);
}
return self::$storage[$file_path];
}
public function remove(string $file_path): void
{
unset(self::$storage[strtolower($file_path)]);
}
public function has(string $file_path, ?string $file_contents = null): bool
{
$file_path = strtolower($file_path);
if (isset(self::$storage[$file_path])) {
return true;
}
if ($file_contents === null) {
return false;
}
if (!$this->cache) {
return false;
}
$cached_value = $this->cache->getLatestFromCache($file_path, $file_contents);
if (!$cached_value) {
return false;
}
self::$storage[$file_path] = $cached_value;
self::$new_storage[$file_path] = $cached_value;
return true;
}
/**
* @return array<lowercase-string, FileStorage>
*/
public function getAll(): array
{
return self::$storage;
}
/**
* @return array<string, FileStorage>
*/
public function getNew(): array
{
return self::$new_storage;
}
/**
* @param array<lowercase-string, FileStorage> $more
*/
public function addMore(array $more): void
{
self::$new_storage = [...self::$new_storage, ...$more];
self::$storage = [...self::$storage, ...$more];
}
public function create(string $file_path): FileStorage
{
$file_path_lc = strtolower($file_path);
$storage = new FileStorage($file_path);
self::$storage[$file_path_lc] = $storage;
self::$new_storage[$file_path_lc] = $storage;
return $storage;
}
public static function deleteAll(): void
{
self::$storage = [];
}
public static function populated(): void
{
self::$new_storage = [];
}
}