Skip to content

Commit d2717dc

Browse files
committed
improves cache
1 parent cc88efb commit d2717dc

3 files changed

Lines changed: 39 additions & 19 deletions

File tree

.gitignore

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@ coverage.xml
1111
*.swo
1212
.php
1313
.phpunit.cache
14-
/temp
14+
/.temp

src/Analyser.php

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,27 +29,22 @@ public static function analyse(array $files, Closure $postProcessedFile, Closure
2929
}
3030

3131
$filesTouched = [];
32-
$filesInCache = [];
3332

3433
foreach ($files as $file) {
3534
if ($cache->has($file)) {
36-
$filesInCache[] = $file;
35+
[$file, $errors, $ignored] = $cache->get($file);
36+
37+
$result = Result::fromPHPStanErrors($file, $errors, $ignored);
38+
39+
$postProcessedFile($result);
40+
$onProcessedFile($result);
3741
} else {
3842
$filesTouched[] = $file;
3943
}
4044
}
4145

4246
unset($files);
4347

44-
self::analyseChunks(
45-
[$filesInCache],
46-
$testCase,
47-
$postProcessedFile,
48-
$onProcessedFile,
49-
$cache,
50-
false,
51-
);
52-
5348
// next, if we don't have touched files, we can return early
5449

5550
if (count($filesTouched) === 0) {
@@ -85,7 +80,7 @@ public static function analyse(array $files, Closure $postProcessedFile, Closure
8580
$testCase,
8681
$postProcessedFile,
8782
$onProcessedFile,
88-
$cache
83+
$cache,
8984
);
9085
}
9186

@@ -111,7 +106,7 @@ private static function analyseChunks(
111106
}
112107

113108
foreach ($chunks as $files) {
114-
$promises[] = async(function () use ($files, $testCase, $onProcessedFile) {
109+
$promises[] = async(function () use ($cache, $files, $testCase, $onProcessedFile) {
115110
$testCase->resetIgnoredErrors();
116111
$results = [];
117112

@@ -125,6 +120,8 @@ private static function analyseChunks(
125120
$errors = array_values($errors);
126121
$ignored = array_values($ignored);
127122

123+
$cache->persist($file, [$file, $errors, $ignored]);
124+
128125
$result = Result::fromPHPStanErrors($file, $errors, $ignored);
129126

130127
$onProcessedFile($result);

src/Support/Cache.php

Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
namespace Pest\TypeCoverage\Support;
66

7+
use LogicException;
78
use PHPStan\Analyser\Error;
89

910
/**
@@ -14,7 +15,7 @@ final class Cache
1415
/**
1516
* The cache version.
1617
*/
17-
private const string CACHE_VERSION = 'v2';
18+
private const string CACHE_VERSION = 'v3';
1819

1920
/**
2021
* The cache instance.
@@ -40,6 +41,26 @@ public function has(string $file): bool
4041
return array_key_exists($fileHash, $items);
4142
}
4243

44+
/**
45+
* Gets the cached contents for the given file.
46+
*
47+
* @return array{0: string, 1: array<int, Error>, 2: array<int, Error>}
48+
*
49+
* @throws LogicException
50+
*/
51+
public function get(string $file): array
52+
{
53+
$fileHash = md5_file($file);
54+
55+
if ($fileHash === false) {
56+
throw new LogicException('Failed to compute the hash for the file: '.$file);
57+
}
58+
59+
$items = $this->all();
60+
61+
return $items[$fileHash] ?? throw new LogicException('No cache found for the file: '.$file);
62+
}
63+
4364
/**
4465
* Flushes all the cache contents.
4566
*/
@@ -57,7 +78,7 @@ private function file(): string
5778
{
5879
return dirname(__DIR__, 2)
5980
.DIRECTORY_SEPARATOR
60-
.'temp'
81+
.'.temp'
6182
.DIRECTORY_SEPARATOR
6283
.self::CACHE_VERSION
6384
.'.php';
@@ -82,8 +103,10 @@ private function all(): array
82103
/**
83104
* Persists the cache contents.
84105
*/
85-
public function persist(string $key, array $values): void
106+
public function persist(string $file, array $values): void
86107
{
108+
$fileHash = md5_file($file);
109+
87110
foreach ($values as $value) {
88111
if (is_array($value)) {
89112
foreach ($value as $item) {
@@ -102,7 +125,7 @@ public function persist(string $key, array $values): void
102125
chmod($dirPath, 0755);
103126
}
104127

105-
$this->withinLock(function () use ($key, $values) {
128+
$this->withinLock(function () use ($fileHash, $values) {
106129
$filePath = $this->file();
107130
$cache = [];
108131

@@ -113,7 +136,7 @@ public function persist(string $key, array $values): void
113136
}
114137
}
115138

116-
$cache[$key] = $values;
139+
$cache[$fileHash] = $values;
117140

118141
$content = '<?php return '.var_export($cache, true).';';
119142

0 commit comments

Comments
 (0)