-
-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathAnalyser.php
More file actions
145 lines (114 loc) · 3.95 KB
/
Analyser.php
File metadata and controls
145 lines (114 loc) · 3.95 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
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
<?php
declare(strict_types=1);
namespace Pest\TypeCoverage;
use Closure;
use Pest\TypeCoverage\Support\Cache;
use PHPStan\Analyser\Error;
use Pokio\Environment;
/**
* @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 $postProcessedFile, Closure $onProcessedFile, ?Cache $cache): void
{
$testCase = new TestCaseForTypeCoverage('dummy');
if (count($files) === 0) {
return;
}
$filesTouched = [];
foreach ($files as $file) {
if ($cache !== null && $cache->has($file)) {
[$file, $errors, $ignored] = $cache->get($file);
$result = Result::fromPHPStanErrors($file, $errors, $ignored);
$postProcessedFile($result);
$onProcessedFile($result);
} else {
$filesTouched[] = $file;
}
}
unset($files);
// next, if we don't have touched files, we can return early
if (count($filesTouched) === 0) {
return;
}
// if not, lets warm up the cache with the first file:
$firstFile = array_shift($filesTouched);
self::analyseChunks(
[[$firstFile]],
$testCase,
$postProcessedFile,
$onProcessedFile,
$cache,
false,
);
$maxProcesses = (Environment::supportsFork() && ! isset($_ENV['__PEST_PLUGIN_ENV']))
? (Environment::maxProcesses() / 3)
: 1;
$maxProcesses = max(1, $maxProcesses);
$chunkOfFiles = array_fill(0, $maxProcesses, []);
foreach (array_values($filesTouched) as $i => $file) {
$chunkOfFiles[$i % $maxProcesses][] = $file;
}
$chunkOfFiles = array_values(
array_filter($chunkOfFiles, static fn (array $chunk) => count($chunk) > 0),
);
self::analyseChunks(
$chunkOfFiles,
$testCase,
$postProcessedFile,
$onProcessedFile,
$cache,
);
}
/**
* Analyse the chunks of files.
*/
private static function analyseChunks(
array $chunks,
TestCaseForTypeCoverage $testCase,
Closure $postProcessedFile,
Closure $onProcessedFile,
?Cache $cache,
bool $useAsync = true,
): void {
$promises = [];
if ($useAsync === false) {
pokio()->useSync();
} else {
if (Environment::supportsFork() && ! isset($_ENV['__PEST_PLUGIN_ENV'])) {
pokio()->useFork();
}
}
foreach ($chunks as $files) {
$promises[] = async(function () use ($cache, $files, $testCase, $onProcessedFile) {
$testCase->resetIgnoredErrors();
$results = [];
$analyserErrors = $testCase->gatherAnalyserErrors($files);
$analyserIgnored = $testCase->getIgnoredErrors();
foreach ($files as $file) {
$errors = array_filter($analyserErrors, static fn (Error $error) => $error->getFile() === $file);
$ignored = array_filter($analyserIgnored, static fn (Error $error) => $error->getFile() === $file);
$errors = array_values($errors);
$ignored = array_values($ignored);
$cache?->persist($file, [$file, $errors, $ignored]);
$result = Result::fromPHPStanErrors($file, $errors, $ignored);
$onProcessedFile($result);
$results[] = $result;
}
return $results;
});
}
foreach (await($promises) as $results) {
foreach ($results as $result) {
$postProcessedFile($result);
}
}
}
}