forked from vimeo/psalm
/
IssueHandler.php
183 lines (152 loc) · 5.28 KB
/
IssueHandler.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
<?php
namespace Psalm\Config;
use Psalm\Config;
use Psalm\Exception\ConfigException;
use SimpleXMLElement;
use function array_filter;
use function array_map;
use function dirname;
use function in_array;
use function scandir;
use function strtolower;
use function substr;
use const SCANDIR_SORT_NONE;
/** @internal */
final class IssueHandler
{
/**
* @var string
*/
private $error_level = Config::REPORT_ERROR;
/**
* @var array<ErrorLevelFileFilter>
*/
private $custom_levels = [];
public static function loadFromXMLElement(SimpleXMLElement $e, string $base_dir): IssueHandler
{
$handler = new self();
if (isset($e['errorLevel'])) {
$handler->error_level = (string) $e['errorLevel'];
if (!in_array($handler->error_level, Config::$ERROR_LEVELS, true)) {
throw new ConfigException('Unexpected error level ' . $handler->error_level);
}
}
/** @var SimpleXMLElement $error_level */
foreach ($e->errorLevel as $error_level) {
$handler->custom_levels[] = ErrorLevelFileFilter::loadFromXMLElement($error_level, $base_dir, true);
}
return $handler;
}
public function setCustomLevels(array $customLevels, string $base_dir): void
{
/** @var array $customLevel */
foreach ($customLevels as $customLevel) {
$this->custom_levels[] = ErrorLevelFileFilter::loadFromArray($customLevel, $base_dir, true);
}
}
public function setErrorLevel(string $error_level): void
{
if (!in_array($error_level, Config::$ERROR_LEVELS, true)) {
throw new ConfigException('Unexpected error level ' . $error_level);
}
$this->error_level = $error_level;
}
public function getReportingLevelForFile(string $file_path): string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allows($file_path)) {
return $custom_level->getErrorLevel();
}
}
return $this->error_level;
}
public function getReportingLevelForClass(string $fq_classlike_name): ?string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsClass($fq_classlike_name)) {
return $custom_level->getErrorLevel();
}
}
return null;
}
public function getReportingLevelForMethod(string $method_id): ?string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsMethod(strtolower($method_id))) {
return $custom_level->getErrorLevel();
}
}
return null;
}
public function getReportingLevelForFunction(string $function_id): ?string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsMethod(strtolower($function_id))) {
return $custom_level->getErrorLevel();
}
}
return null;
}
public function getReportingLevelForArgument(string $function_id): ?string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsMethod(strtolower($function_id))) {
return $custom_level->getErrorLevel();
}
}
return null;
}
public function getReportingLevelForProperty(string $property_id): ?string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsProperty($property_id)) {
return $custom_level->getErrorLevel();
}
}
return null;
}
public function getReportingLevelForClassConstant(string $constant_id): ?string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsClassConstant($constant_id)) {
return $custom_level->getErrorLevel();
}
}
return null;
}
public function getReportingLevelForVariable(string $var_name): ?string
{
foreach ($this->custom_levels as $custom_level) {
if ($custom_level->allowsVariable($var_name)) {
return $custom_level->getErrorLevel();
}
}
return null;
}
/**
* @return array<int, string>
*/
public static function getAllIssueTypes(): array
{
return array_filter(
array_map(
fn(string $file_name): string => substr($file_name, 0, -4),
scandir(dirname(__DIR__) . '/Issue', SCANDIR_SORT_NONE)
),
fn(string $issue_name): bool => $issue_name !== ''
&& $issue_name !== 'MethodIssue'
&& $issue_name !== 'PropertyIssue'
&& $issue_name !== 'ClassConstantIssue'
&& $issue_name !== 'FunctionIssue'
&& $issue_name !== 'ArgumentIssue'
&& $issue_name !== 'VariableIssue'
&& $issue_name !== 'ClassIssue'
&& $issue_name !== 'CodeIssue'
&& $issue_name !== 'PsalmInternalError'
&& $issue_name !== 'ParseError'
&& $issue_name !== 'PluginIssue'
&& $issue_name !== 'MixedIssue'
&& $issue_name !== 'MixedIssueTrait'
);
}
}