/
IgnoredErrorHelper.php
93 lines (84 loc) · 2.46 KB
/
IgnoredErrorHelper.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
<?php declare(strict_types = 1);
namespace PHPStan\Analyser;
use Nette\Utils\Json;
use Nette\Utils\JsonException;
use PHPStan\File\FileHelper;
use function is_array;
use function is_file;
use function sprintf;
class IgnoredErrorHelper
{
/**
* @param (string|mixed[])[] $ignoreErrors
*/
public function __construct(
private FileHelper $fileHelper,
private array $ignoreErrors,
private bool $reportUnmatchedIgnoredErrors,
)
{
}
public function initialize(): IgnoredErrorHelperResult
{
$otherIgnoreErrors = [];
$ignoreErrorsByFile = [];
$errors = [];
$expandedIgnoreErrors = [];
foreach ($this->ignoreErrors as $ignoreError) {
if (is_array($ignoreError)) {
if (!isset($ignoreError['message']) && !isset($ignoreError['messages'])) {
$errors[] = sprintf(
'Ignored error %s is missing a message.',
Json::encode($ignoreError),
);
continue;
}
if (isset($ignoreError['messages'])) {
foreach ($ignoreError['messages'] as $message) {
$expandedIgnoreError = $ignoreError;
unset($expandedIgnoreError['messages']);
$expandedIgnoreError['message'] = $message;
$expandedIgnoreErrors[] = $expandedIgnoreError;
}
} else {
$expandedIgnoreErrors[] = $ignoreError;
}
} else {
$expandedIgnoreErrors[] = $ignoreError;
}
}
foreach ($expandedIgnoreErrors as $i => $ignoreError) {
$ignoreErrorEntry = [
'index' => $i,
'ignoreError' => $ignoreError,
];
try {
if (is_array($ignoreError)) {
if (!isset($ignoreError['message'])) {
$errors[] = sprintf(
'Ignored error %s is missing a message.',
Json::encode($ignoreError),
);
continue;
}
if (!isset($ignoreError['path'])) {
$otherIgnoreErrors[] = $ignoreErrorEntry;
} elseif (@is_file($ignoreError['path'])) {
$normalizedPath = $this->fileHelper->normalizePath($ignoreError['path']);
$ignoreError['path'] = $normalizedPath;
$ignoreErrorsByFile[$normalizedPath][] = $ignoreErrorEntry;
$ignoreError['realPath'] = $normalizedPath;
$expandedIgnoreErrors[$i] = $ignoreError;
} else {
$otherIgnoreErrors[] = $ignoreErrorEntry;
}
} else {
$otherIgnoreErrors[] = $ignoreErrorEntry;
}
} catch (JsonException $e) {
$errors[] = $e->getMessage();
}
}
return new IgnoredErrorHelperResult($this->fileHelper, $errors, $otherIgnoreErrors, $ignoreErrorsByFile, $expandedIgnoreErrors, $this->reportUnmatchedIgnoredErrors);
}
}