-
Notifications
You must be signed in to change notification settings - Fork 582
Expand file tree
/
Copy pathIgnoredErrorHelperResult.php
More file actions
289 lines (261 loc) · 9.74 KB
/
Copy pathIgnoredErrorHelperResult.php
File metadata and controls
289 lines (261 loc) · 9.74 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
<?php declare(strict_types = 1);
namespace PHPStan\Analyser\Ignore;
use PHPStan\Analyser\Error;
use PHPStan\File\FileHelper;
use PHPStan\ShouldNotHappenException;
use function array_fill_keys;
use function array_key_exists;
use function array_values;
use function count;
use function is_array;
use function is_string;
use function sprintf;
/**
* `IgnoredErrorHelper` may collapse several configured ignores into one
* merged entry, so `message`/`rawMessage`/`identifier` are nullable here.
* It also attaches `realPath` once the configured path is resolved. The
* `messages`/`rawMessages`/`identifiers` keys remain in the inferred shape
* even after expansion + unset (PHPStan does not strip optional keys via
* negative isset on sealed shapes), so the type lists them explicitly here
* — they are never read, only tolerated. `paths` is `array<int<0, max>,
* string>` rather than `list<string>` because `process()` unsets matched
* entries by index, breaking list-ness.
*
* @phpstan-type ExpandedIgnoredErrorData = array{
* message?: string|null,
* rawMessage?: string|null,
* identifier?: string|null,
* messages?: list<string>,
* rawMessages?: list<string>,
* identifiers?: list<string>,
* path?: string,
* paths?: array<int<0, max>, string>,
* count?: int,
* reportUnmatched?: bool,
* realPath?: string,
* }
*/
final class IgnoredErrorHelperResult
{
/**
* @param list<string> $errors
* @param array<array{index: int<0, max>, ignoreError: string|ExpandedIgnoredErrorData}> $otherIgnoreErrors
* @param array<string, array<array{index: int<0, max>, ignoreError: string|ExpandedIgnoredErrorData}>> $ignoreErrorsByFile
* @param (string|ExpandedIgnoredErrorData)[] $ignoreErrors
*/
public function __construct(
private FileHelper $fileHelper,
private array $errors,
private array $otherIgnoreErrors,
private array $ignoreErrorsByFile,
private array $ignoreErrors,
private bool $reportUnmatchedIgnoredErrors,
)
{
}
/**
* @return list<string>
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* @param list<Error> $errors
* @param string[] $analysedFiles
*/
public function process(
array $errors,
bool $onlyFiles,
array $analysedFiles,
bool $hasInternalErrors,
): IgnoredErrorHelperProcessedResult
{
$unmatchedIgnoredErrors = $this->ignoreErrors;
$stringErrors = [];
// Per-entry runtime state for `count`-bounded ignores. Tracked in side
// maps keyed by the same index so `$unmatchedIgnoredErrors` keeps the
// `(string|ExpandedIgnoredErrorData)[]` shape across the closure's
// offset writes — otherwise PHPStan widens it to `array<mixed>`.
$realCounts = [];
$matchedAt = [];
$processIgnoreError = function (Error $error, int $i, $ignore) use (&$unmatchedIgnoredErrors, &$stringErrors, &$realCounts, &$matchedAt): bool {
$shouldBeIgnored = false;
if (is_string($ignore)) {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, ignoredErrorPattern: $ignore, ignoredErrorMessage: null, identifier: null, path: null);
if ($shouldBeIgnored) {
unset($unmatchedIgnoredErrors[$i]);
}
} else {
if (isset($ignore['path'])) {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, ignoredErrorPattern: $ignore['message'] ?? null, ignoredErrorMessage: $ignore['rawMessage'] ?? null, identifier: $ignore['identifier'] ?? null, path: $ignore['path']);
if ($shouldBeIgnored) {
if (isset($ignore['count'])) {
$realCount = ($realCounts[$i] ?? 0) + 1;
$realCounts[$i] = $realCount;
if (!isset($matchedAt[$i])) {
$matchedAt[$i] = ['file' => $error->getFile(), 'line' => $error->getLine()];
}
if ($realCount > $ignore['count']) {
$shouldBeIgnored = false;
}
} else {
unset($unmatchedIgnoredErrors[$i]);
}
}
} elseif (isset($ignore['paths'])) {
foreach ($ignore['paths'] as $j => $ignorePath) {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, ignoredErrorPattern: $ignore['message'] ?? null, ignoredErrorMessage: $ignore['rawMessage'] ?? null, identifier: $ignore['identifier'] ?? null, path: $ignorePath);
if (!$shouldBeIgnored) {
continue;
}
if (isset($unmatchedIgnoredErrors[$i])) {
if (!is_array($unmatchedIgnoredErrors[$i])) {
throw new ShouldNotHappenException();
}
unset($unmatchedIgnoredErrors[$i]['paths'][$j]);
if (isset($unmatchedIgnoredErrors[$i]['paths']) && count($unmatchedIgnoredErrors[$i]['paths']) === 0) {
unset($unmatchedIgnoredErrors[$i]);
}
}
break;
}
} else {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, ignoredErrorPattern: $ignore['message'] ?? null, ignoredErrorMessage: $ignore['rawMessage'] ?? null, identifier: $ignore['identifier'] ?? null, path: null);
if ($shouldBeIgnored) {
unset($unmatchedIgnoredErrors[$i]);
}
}
}
if ($shouldBeIgnored) {
if (!$error->canBeIgnored()) {
$stringErrors[] = sprintf(
'Error message "%s" cannot be ignored, use excludePaths instead.',
$error->getMessage(),
);
return true;
}
return false;
}
return true;
};
$ignoredErrors = [];
foreach ($errors as $errorIndex => $error) {
$filePath = $this->fileHelper->normalizePath($error->getFilePath());
if (isset($this->ignoreErrorsByFile[$filePath])) {
foreach ($this->ignoreErrorsByFile[$filePath] as $ignoreError) {
$i = $ignoreError['index'];
$ignore = $ignoreError['ignoreError'];
$result = $processIgnoreError($error, $i, $ignore);
if (!$result) {
unset($errors[$errorIndex]);
$ignoredErrors[] = [$error, $ignore];
continue 2;
}
}
}
$traitFilePath = $error->getTraitFilePath();
if ($traitFilePath !== null) {
$normalizedTraitFilePath = $this->fileHelper->normalizePath($traitFilePath);
if (isset($this->ignoreErrorsByFile[$normalizedTraitFilePath])) {
foreach ($this->ignoreErrorsByFile[$normalizedTraitFilePath] as $ignoreError) {
$i = $ignoreError['index'];
$ignore = $ignoreError['ignoreError'];
$result = $processIgnoreError($error, $i, $ignore);
if (!$result) {
unset($errors[$errorIndex]);
$ignoredErrors[] = [$error, $ignore];
continue 2;
}
}
}
}
foreach ($this->otherIgnoreErrors as $ignoreError) {
$i = $ignoreError['index'];
$ignore = $ignoreError['ignoreError'];
$result = $processIgnoreError($error, $i, $ignore);
if (!$result) {
unset($errors[$errorIndex]);
$ignoredErrors[] = [$error, $ignore];
continue 2;
}
}
}
$errors = array_values($errors);
foreach ($unmatchedIgnoredErrors as $i => $unmatchedIgnoredError) {
if (!is_array($unmatchedIgnoredError) || !isset($unmatchedIgnoredError['count']) || !isset($realCounts[$i])) {
continue;
}
$realCount = $realCounts[$i];
if ($realCount <= $unmatchedIgnoredError['count']) {
continue;
}
$matchedFile = $matchedAt[$i]['file'] ?? null;
$matchedLine = $matchedAt[$i]['line'] ?? null;
$errors[] = (new Error(sprintf(
'%s %s is expected to occur %d %s, but occurred %d %s.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
$unmatchedIgnoredError['count'],
$unmatchedIgnoredError['count'] === 1 ? 'time' : 'times',
$realCount,
$realCount === 1 ? 'time' : 'times',
), $matchedFile ?? '', $matchedLine, false))->withIdentifier('ignore.count');
}
$analysedFilesKeys = array_fill_keys($analysedFiles, true);
if (!$hasInternalErrors) {
foreach ($unmatchedIgnoredErrors as $i => $unmatchedIgnoredError) {
$reportUnmatched = is_array($unmatchedIgnoredError)
? ($unmatchedIgnoredError['reportUnmatched'] ?? $this->reportUnmatchedIgnoredErrors)
: $this->reportUnmatchedIgnoredErrors;
if ($reportUnmatched === false) {
continue;
}
$realCount = $realCounts[$i] ?? null;
if (
isset($unmatchedIgnoredError['count'])
&& $realCount !== null
&& (isset($unmatchedIgnoredError['realPath']) || !$onlyFiles)
) {
if ($realCount < $unmatchedIgnoredError['count']) {
$matchedFile = $matchedAt[$i]['file'] ?? null;
$matchedLine = $matchedAt[$i]['line'] ?? null;
// $realCount is at least 1 (it was incremented in the closure)
// and strictly less than count, so count is always >= 2.
$errors[] = (new Error(sprintf(
'%s %s is expected to occur %d times, but occurred only %d %s.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
$unmatchedIgnoredError['count'],
$realCount,
$realCount === 1 ? 'time' : 'times',
), $matchedFile ?? '', $matchedLine, false))->withIdentifier('ignore.count');
}
} elseif (isset($unmatchedIgnoredError['realPath'])) {
if (!array_key_exists($unmatchedIgnoredError['realPath'], $analysedFilesKeys)) {
continue;
}
if ($onlyFiles) {
continue;
}
$errors[] = (new Error(
sprintf(
'%s %s was not matched in reported errors.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
),
$unmatchedIgnoredError['realPath'],
canBeIgnored: false,
))->withIdentifier('ignore.unmatched');
} elseif (!$onlyFiles) {
$stringErrors[] = sprintf(
'%s %s was not matched in reported errors.',
IgnoredError::getIgnoredErrorLabel($unmatchedIgnoredError),
IgnoredError::stringifyPattern($unmatchedIgnoredError),
);
}
}
}
return new IgnoredErrorHelperProcessedResult($errors, $ignoredErrors, $stringErrors);
}
}