Skip to content

Commit 26a4d3b

Browse files
committed
Cleanup
1 parent 495065b commit 26a4d3b

14 files changed

+103
-584
lines changed

psalm.xml.dist

-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
name="Psalm for Psalm"
66
errorLevel="1"
77
noCache="true"
8-
forceJit="true"
98
throwExceptionOnError="0"
109
findUnusedCode="true"
1110
ensureArrayStringOffsetsExist="true"

src/Psalm/Codebase.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ public function reloadFiles(ProjectAnalyzer $project_analyzer, array $candidate_
352352
$parser_cache_provider = $this->statements_provider->parser_cache_provider;
353353

354354
foreach ($candidate_files as $candidate_file_path) {
355-
if ($parser_cache_provider->loadExistingFileContentsFromCache($candidate_file_path)
355+
if ($parser_cache_provider->loadFileContentsFromCache($candidate_file_path)
356356
!== $this->file_provider->getContents($candidate_file_path)
357357
) {
358358
$diff_files[] = $candidate_file_path;

src/Psalm/Internal/Analyzer/ProjectAnalyzer.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -928,7 +928,7 @@ private function getDiffFiles(): array
928928

929929
foreach ($this->project_files as $file_path) {
930930
if ($this->file_provider->getModifiedTime($file_path) >= $last_run
931-
&& $this->parser_cache_provider->loadExistingFileContentsFromCache($file_path)
931+
&& $this->parser_cache_provider->loadFileContentsFromCache($file_path)
932932
!== $this->file_provider->getContents($file_path)
933933
) {
934934
$diff_files[] = $file_path;

src/Psalm/Internal/Cache.php

+27-27
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,30 @@
44

55
namespace Psalm\Internal;
66

7-
use Amp\Serialization\SerializationException;
87
use Amp\Serialization\Serializer;
9-
use Closure;
108
use Psalm\Config;
119
use Psalm\Internal\Provider\Providers;
1210
use RuntimeException;
1311

12+
use function fclose;
1413
use function file_exists;
1514
use function file_put_contents;
16-
use function filemtime;
17-
use function gzdeflate;
18-
use function gzinflate;
19-
use function igbinary_serialize;
20-
use function igbinary_unserialize;
15+
use function flock;
16+
use function fopen;
17+
use function hash;
2118
use function is_dir;
2219
use function is_readable;
23-
use function is_writable;
24-
use function lz4_compress;
25-
use function lz4_uncompress;
20+
use function json_decode;
2621
use function mkdir;
27-
use function serialize;
22+
use function stream_get_contents;
2823
use function unlink;
29-
use function unserialize;
3024

3125
use const DIRECTORY_SEPARATOR;
3226
use const LOCK_EX;
27+
use const LOCK_UN;
3328

3429
/**
3530
* @internal
36-
*
3731
* @template T as array|object|string
3832
*/
3933
final class Cache
@@ -43,12 +37,12 @@ final class Cache
4337

4438
/** @var array<string, string> */
4539
private array $idx = [];
46-
/** @var array<string, string> */
40+
/** @var array<string, ?string> */
4741
private array $newIdx = [];
4842
/** @var array<string, T> */
4943
private array $cache = [];
5044

51-
public function __construct(Config $config, string $subdir, mixed $dependencies = null)
45+
public function __construct(Config $config, string $subdir, array $dependencies = [])
5246
{
5347
$this->serializer = $config->getCacheSerializer();
5448

@@ -71,6 +65,7 @@ public function __construct(Config $config, string $subdir, mixed $dependencies
7165
}
7266
}
7367

68+
$dependencies []= $config->computeHash();
7469
$dependencies = $this->serializer->serialize($dependencies);
7570

7671
$idx = fopen($this->dir.'idx', 'r');
@@ -82,7 +77,8 @@ public function __construct(Config $config, string $subdir, mixed $dependencies
8277
if ($deps === $dependencies) {
8378
$this->idx = $idx;
8479
}
85-
} catch (RuntimeException) {}
80+
} catch (RuntimeException) {
81+
}
8682
flock($idx, LOCK_UN);
8783
fclose($idx);
8884
}
@@ -91,9 +87,7 @@ public function __construct(Config $config, string $subdir, mixed $dependencies
9187
public function getItem(string $key, string $hash = ''): array|object|string|null
9288
{
9389
if (isset($this->idx[$key]) && $this->idx[$key] !== $hash) {
94-
unset($this->idx[$key]);
95-
unset($this->newIdx[$key]);
96-
unset($this->cache[$key]);
90+
$this->deleteItem($key);
9791
return null;
9892
} elseif (!isset($this->idx[$key])) {
9993
return null;
@@ -105,7 +99,7 @@ public function getItem(string $key, string $hash = ''): array|object|string|nul
10599

106100
$path = $this->dir . DIRECTORY_SEPARATOR . hash('xxh128', $key);
107101

108-
if (!file_exists($path)
102+
if (!file_exists($path)
109103
|| !is_readable($path)
110104
) {
111105
return null;
@@ -125,25 +119,31 @@ public function getItem(string $key, string $hash = ''): array|object|string|nul
125119

126120
public function deleteItem(string $key): void
127121
{
128-
$path = $this->dir . DIRECTORY_SEPARATOR . hash('xxh128', $key);
129-
@unlink($path);
130-
unset($this->idx[$key]);
131-
unset($this->newIdx[$key]);
132-
unset($this->cache[$key]);
122+
if (isset($this->idx[$key])) {
123+
$path = $this->dir . DIRECTORY_SEPARATOR . hash('xxh128', $key);
124+
@unlink($path);
125+
unset($this->idx[$key]);
126+
unset($this->cache[$key]);
127+
$this->newIdx[$key] = null;
128+
}
133129
}
134130

135131
/** @param T $item */
136132
public function saveItem(string $key, array|object|string $item, string $hash = ''): void
137133
{
134+
if (isset($this->idx[$key]) && $this->idx[$key] === $hash) {
135+
return;
136+
}
138137
$path = $this->dir . DIRECTORY_SEPARATOR . hash('xxh128', $key);
139138
file_put_contents($path, $this->serializer->serialize($item), LOCK_EX);
140139
$this->cache[$key] = $item;
141140
$this->idx[$key] = $hash;
142141
$this->newIdx[$key] = $hash;
143142
}
144143

145-
/** @return array<string, string> */
146-
public function getNewIdx(): array {
144+
/** @return array<string, ?string> */
145+
public function getNewIdx(): array
146+
{
147147
return $this->newIdx;
148148
}
149149
}

src/Psalm/Internal/LanguageServer/Provider/InMemoryClassLikeStorageCacheProvider.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public function writeToCache(ClassLikeStorage $storage, ?string $file_path, ?str
3737
public function getLatestFromCache(
3838
string $fq_classlike_name_lc,
3939
?string $file_path,
40-
?string $file_contents,
40+
string $file_contents,
4141
): ClassLikeStorage {
4242
$cached_value = $this->loadFromCache($fq_classlike_name_lc);
4343

src/Psalm/Internal/LanguageServer/Provider/InMemoryParserCacheProvider.php

+5-42
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
use PhpParser;
99
use Psalm\Internal\Provider\ParserCacheProvider as InternalParserCacheProvider;
1010

11-
use function microtime;
12-
1311
/**
1412
* @internal
1513
*/
@@ -20,20 +18,16 @@ final class InMemoryParserCacheProvider extends InternalParserCacheProvider
2018
*/
2119
private array $file_contents_cache = [];
2220

23-
/**
24-
* @var array<string, string>
25-
*/
26-
private array $file_content_hash = [];
2721

2822
/**
2923
* @var array<string, list<PhpParser\Node\Stmt>>
3024
*/
3125
private array $statements_cache = [];
3226

3327
/**
34-
* @var array<string, float>
28+
* @var array<string, string>
3529
*/
36-
private array $statements_cache_time = [];
30+
private array $statements_hash = [];
3731

3832
public function __construct()
3933
{
@@ -42,28 +36,17 @@ public function __construct()
4236
#[Override]
4337
public function loadStatementsFromCache(
4438
string $file_path,
45-
int $file_modified_time,
4639
string $file_content_hash,
4740
): ?array {
4841
if (isset($this->statements_cache[$file_path])
49-
&& $this->statements_cache_time[$file_path] >= $file_modified_time
50-
&& $this->file_content_hash[$file_path] === $file_content_hash
42+
&& $this->statements_hash[$file_path] === $file_content_hash
5143
) {
5244
return $this->statements_cache[$file_path];
5345
}
5446

5547
return null;
5648
}
5749

58-
/**
59-
* @return list<PhpParser\Node\Stmt>|null
60-
*/
61-
#[Override]
62-
public function loadExistingStatementsFromCache(string $file_path): ?array
63-
{
64-
return $this->statements_cache[$file_path] ?? null;
65-
}
66-
6750
/**
6851
* @param list<PhpParser\Node\Stmt> $stmts
6952
*/
@@ -72,15 +55,13 @@ public function saveStatementsToCache(
7255
string $file_path,
7356
string $file_content_hash,
7457
array $stmts,
75-
bool $touch_only,
7658
): void {
7759
$this->statements_cache[$file_path] = $stmts;
78-
$this->statements_cache_time[$file_path] = microtime(true);
79-
$this->file_content_hash[$file_path] = $file_content_hash;
60+
$this->statements_hash[$file_path] = $file_content_hash;
8061
}
8162

8263
#[Override]
83-
public function loadExistingFileContentsFromCache(string $file_path): ?string
64+
public function loadFileContentsFromCache(string $file_path): ?string
8465
{
8566
return $this->file_contents_cache[$file_path] ?? null;
8667
}
@@ -90,22 +71,4 @@ public function cacheFileContents(string $file_path, string $file_contents): voi
9071
{
9172
$this->file_contents_cache[$file_path] = $file_contents;
9273
}
93-
94-
#[Override]
95-
public function deleteOldParserCaches(float $time_before): int
96-
{
97-
$this->existing_file_content_hashes = null;
98-
$this->new_file_content_hashes = [];
99-
100-
$this->file_contents_cache = [];
101-
$this->file_content_hash = [];
102-
$this->statements_cache = [];
103-
$this->statements_cache_time = [];
104-
return 0;
105-
}
106-
107-
#[Override]
108-
public function saveFileContentHashes(): void
109-
{
110-
}
11174
}

0 commit comments

Comments
 (0)