-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSeverityLevelPool.php
executable file
·193 lines (169 loc) · 4.93 KB
/
SeverityLevelPool.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
<?php
/**
* @Package: MaplePHP - Severity level pool
* @Author: Daniel Ronkainen
* @Licence: Apache-2.0 license, Copyright © Daniel Ronkainen
Don't delete this comment, its part of the license.
*/
namespace MaplePHP\Blunder;
use InvalidArgumentException;
class SeverityLevelPool
{
// List all supported error types
protected const SEVERITY_TYPES = [
E_ERROR => 'E_ERROR',
E_WARNING => 'E_WARNING',
E_PARSE => 'E_PARSE',
E_NOTICE => 'E_NOTICE',
E_CORE_ERROR => 'E_CORE_ERROR',
E_CORE_WARNING => 'E_CORE_WARNING',
E_COMPILE_ERROR => 'E_COMPILE_ERROR',
E_COMPILE_WARNING => 'E_COMPILE_WARNING',
E_USER_ERROR => 'E_USER_ERROR',
E_USER_WARNING => 'E_USER_WARNING',
E_USER_NOTICE => 'E_USER_NOTICE',
E_STRICT => 'E_STRICT',
E_RECOVERABLE_ERROR => 'E_RECOVERABLE_ERROR',
E_DEPRECATED => 'E_DEPRECATED',
E_USER_DEPRECATED => 'E_USER_DEPRECATED',
E_ALL => 'E_ALL'
];
private array $allowedSeverityTypes = [];
public function __construct(?array $allowedSeverityTypes = null)
{
if(is_array($allowedSeverityTypes)) {
$this->setSeverityLevels($allowedSeverityTypes);
} else {
$this->allowedSeverityTypes = array_keys(self::SEVERITY_TYPES);
}
}
/**
* Get severity level as a title
* @param int $level Expected level code (e.g. E_WARNING)
* @param string|null $fallback
* @return string|null
*/
public static function getSeverityLevel(int $level, ?string $fallback = null): ?string
{
return (self::SEVERITY_TYPES[$level] ?? $fallback);
}
/**
* List all severities that can be used
* @return array
*/
public static function listAll(): array
{
return self::SEVERITY_TYPES;
}
/**
* Overwrite the default severity list and set a new one
* @param array $allowedSeverityTypes
* @return void
*/
public function setSeverityLevels(array $allowedSeverityTypes): void
{
$this->validate($allowedSeverityTypes);
$this->allowedSeverityTypes = $allowedSeverityTypes;
}
/**
* Exclude severity types from list expected severity list
* When excluding the E_ALL flag will also be removed automatically
* @param array $exclude
* @return void
*/
public function excludeSeverityLevels(array $exclude): void
{
$this->validate($exclude);
$this->deleteSeverityLevel(E_ALL);
foreach($exclude as $severityLevel) {
$this->deleteSeverityLevel((int)$severityLevel);
}
}
/**
* Delete a severity level
* @param int $flag
* @return bool
*/
public function deleteSeverityLevel(int $flag): bool
{
if(($key = $this->has($flag)) !== false) {
unset($this->allowedSeverityTypes[$key]);
return true;
}
return false;
}
/**
* Check if error type is supported
* @param int $flag Expected level code (e.g. E_WARNING)
* @return false|int|string
*/
public function has(int $flag): false|int|string
{
return array_search($flag, $this->allowedSeverityTypes);
}
/**
* Get severity mask
* @return int
*/
public function getSeverityLevelMask(): int
{
if($this->has(E_ALL) !== false) {
return E_ALL;
}
$error_mask = 0;
foreach ($this->allowedSeverityTypes as $warning) {
$error_mask |= (int)$warning;
}
return $error_mask;
}
/**
* List all severities that can be used
* @return array
*/
public function listAllSupported(): array
{
return $this->allowedSeverityTypes;
}
/**
* Check if is a fatal error
* @param int $level
* @return bool
*/
final public function isLevelFatal(int $level): bool
{
$errors = E_ERROR;
$errors |= E_PARSE;
$errors |= E_CORE_ERROR;
$errors |= E_CORE_WARNING;
$errors |= E_COMPILE_ERROR;
$errors |= E_COMPILE_WARNING;
return ($level & $errors) > 0;
}
/**
* Validate severity and see if is allowed
* @param int|array $level Expected level code:/s (e.g. E_WARNING)
* @return bool
*/
final protected function validate(int|array $level): bool
{
if(!is_array($level)) {
$level = [$level];
}
return $this->validateMultiple($level);
}
/**
* Validate severity and see if is allowed
* @param array $levels
* @return bool
*/
private function validateMultiple(array $levels): bool
{
foreach($levels as $level) {
$level = (int)$level;
if($this->has($level) === false) {
throw new InvalidArgumentException("The severity level '$level' does not exist.");
}
}
return true;
}
}