-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathAnalyser.php
More file actions
82 lines (70 loc) · 2.15 KB
/
Analyser.php
File metadata and controls
82 lines (70 loc) · 2.15 KB
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
<?php
declare(strict_types=1);
namespace Pest\TypeCoverage;
use Closure;
/**
* @internal
*/
final class Analyser
{
/**
* Analyse the code's type coverage.
*
* @param array<int, string> $files
* @param Closure(Result): void $callback
*/
public static function analyse(array $files, Closure $callback): void
{
$testCase = new TestCaseForTypeCoverage;
$cache = self::getCache();
$cacheIsModified = false;
foreach ($files as $file) {
$fileHash = sha1_file($file);
if (array_key_exists($file, $cache) && $cache[$file]['hash'] === $fileHash) {
$errors = $cache[$file]['errors'];
$ignored = $cache[$file]['ignored'];
} else {
$errors = $testCase->gatherAnalyserErrors([$file]);
$ignored = $testCase->getIgnoredErrors();
$cache[$file] = [
'hash' => $fileHash,
'errors' => $errors,
'ignored' => $ignored,
];
$cacheIsModified = true;
}
$testCase->resetIgnoredErrors();
$callback(Result::fromPHPStanErrors($file, $errors, $ignored));
}
$cache = self::clearRemovedFilesFromCache($cache);
if ($cacheIsModified) {
self::saveCache($cache);
}
}
private static function getCache(): array
{
return is_file(self::getCacheFilepath())
? include self::getCacheFilepath()
: [];
}
private static function saveCache(array $cache): void
{
file_put_contents(
self::getCacheFilepath(),
"<?php\nreturn ".var_export($cache, true).';',
);
}
private static function getCacheFilepath(): string
{
return sys_get_temp_dir().'/pest_plugin_type_coverage_cache_'.md5(__DIR__).'.php';
}
private static function clearRemovedFilesFromCache(array $cache): array
{
foreach (array_keys($cache) as $file) {
if (! is_file($file)) {
unset($cache[$file]);
}
}
return $cache;
}
}