/
IgnoredErrorHelperResult.php
239 lines (213 loc) · 7.11 KB
/
IgnoredErrorHelperResult.php
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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use PHPStan\File\FileHelper;
use PHPStan\ShouldNotHappenException;
use function array_fill_keys;
use function array_filter;
use function array_key_exists;
use function array_merge;
use function array_values;
use function count;
use function is_array;
use function is_string;
use function sprintf;
class IgnoredErrorHelperResult
{
/**
* @param string[] $errors
* @param array<array<mixed>> $otherIgnoreErrors
* @param array<string, array<array<mixed>>> $ignoreErrorsByFile
* @param (string|mixed[])[] $ignoreErrors
*/
public function __construct(
private FileHelper $fileHelper,
private array $errors,
private array $otherIgnoreErrors,
private array $ignoreErrorsByFile,
private array $ignoreErrors,
private bool $reportUnmatchedIgnoredErrors,
)
{
}
/**
* @return string[]
*/
public function getErrors(): array
{
return $this->errors;
}
/**
* @param Error[] $errors
* @param string[] $analysedFiles
* @return string[]|Error[]
*/
public function process(
array $errors,
bool $onlyFiles,
array $analysedFiles,
bool $hasInternalErrors,
): array
{
$unmatchedIgnoredErrors = $this->ignoreErrors;
$addErrors = [];
$processIgnoreError = function (Error $error, int $i, $ignore) use (&$unmatchedIgnoredErrors, &$addErrors): bool {
$shouldBeIgnored = false;
if (is_string($ignore)) {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore, null);
if ($shouldBeIgnored) {
unset($unmatchedIgnoredErrors[$i]);
}
} else {
if (isset($ignore['path'])) {
$shouldBeIgnored = IgnoredError::shouldIgnore($this->fileHelper, $error, $ignore['message'], $ignore['path']);
if ($shouldBeIgnored) {
if (isset($ignore['count'])) {
$realCount = $unmatchedIgnoredErrors[$i]['realCount'] ?? 0;
$realCount++;
$unmatchedIgnoredErrors[$i]['realCount'] = $realCount;
if (!isset($unmatchedIgnoredErrors[$i]['file'])) {
$unmatchedIgnoredErrors[$i]['file'] = $error->getFile();
$unmatchedIgnoredErrors[$i]['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, $ignore['message'], $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, $ignore['message'], null);
if ($shouldBeIgnored) {
unset($unmatchedIgnoredErrors[$i]);
}
}
}
if ($shouldBeIgnored) {
if (!$error->canBeIgnored()) {
$addErrors[] = sprintf(
'Error message "%s" cannot be ignored, use excludePaths instead.',
$error->getMessage(),
);
return true;
}
return false;
}
return true;
};
$errors = array_values(array_filter($errors, function (Error $error) use ($processIgnoreError): bool {
$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) {
return false;
}
}
}
$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) {
return false;
}
}
}
}
foreach ($this->otherIgnoreErrors as $ignoreError) {
$i = $ignoreError['index'];
$ignore = $ignoreError['ignoreError'];
$result = $processIgnoreError($error, $i, $ignore);
if (!$result) {
return false;
}
}
return true;
}));
foreach ($unmatchedIgnoredErrors as $unmatchedIgnoredError) {
if (!isset($unmatchedIgnoredError['count']) || !isset($unmatchedIgnoredError['realCount'])) {
continue;
}
if ($unmatchedIgnoredError['realCount'] <= $unmatchedIgnoredError['count']) {
continue;
}
$addErrors[] = new Error(sprintf(
'Ignored error pattern %s is expected to occur %d %s, but occurred %d %s.',
IgnoredError::stringifyPattern($unmatchedIgnoredError),
$unmatchedIgnoredError['count'],
$unmatchedIgnoredError['count'] === 1 ? 'time' : 'times',
$unmatchedIgnoredError['realCount'],
$unmatchedIgnoredError['realCount'] === 1 ? 'time' : 'times',
), $unmatchedIgnoredError['file'], $unmatchedIgnoredError['line'], false);
}
$errors = array_merge($errors, $addErrors);
$analysedFilesKeys = array_fill_keys($analysedFiles, true);
if (!$hasInternalErrors) {
foreach ($unmatchedIgnoredErrors as $unmatchedIgnoredError) {
$reportUnmatched = $unmatchedIgnoredError['reportUnmatched'] ?? $this->reportUnmatchedIgnoredErrors;
if ($reportUnmatched === false) {
continue;
}
if (
isset($unmatchedIgnoredError['count'])
&& isset($unmatchedIgnoredError['realCount'])
&& (isset($unmatchedIgnoredError['realPath']) || !$onlyFiles)
) {
if ($unmatchedIgnoredError['realCount'] < $unmatchedIgnoredError['count']) {
$errors[] = new Error(sprintf(
'Ignored error pattern %s is expected to occur %d %s, but occurred only %d %s.',
IgnoredError::stringifyPattern($unmatchedIgnoredError),
$unmatchedIgnoredError['count'],
$unmatchedIgnoredError['count'] === 1 ? 'time' : 'times',
$unmatchedIgnoredError['realCount'],
$unmatchedIgnoredError['realCount'] === 1 ? 'time' : 'times',
), $unmatchedIgnoredError['file'], $unmatchedIgnoredError['line'], false);
}
} elseif (isset($unmatchedIgnoredError['realPath'])) {
if (!array_key_exists($unmatchedIgnoredError['realPath'], $analysedFilesKeys)) {
continue;
}
$errors[] = new Error(
sprintf(
'Ignored error pattern %s was not matched in reported errors.',
IgnoredError::stringifyPattern($unmatchedIgnoredError),
),
$unmatchedIgnoredError['realPath'],
null,
false,
);
} elseif (!$onlyFiles) {
$errors[] = sprintf(
'Ignored error pattern %s was not matched in reported errors.',
IgnoredError::stringifyPattern($unmatchedIgnoredError),
);
}
}
}
return $errors;
}
}