-
Notifications
You must be signed in to change notification settings - Fork 0
/
ReportingLevel.php
222 lines (194 loc) · 6.39 KB
/
ReportingLevel.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
<?php
declare(strict_types=1);
namespace Bakame\Aide\Error;
use ValueError;
use function array_filter;
use function array_key_exists;
use function array_map;
use function array_reduce;
use function array_search;
use function array_values;
use function error_reporting;
use function is_string;
use const ARRAY_FILTER_USE_KEY;
use const E_ALL;
use const E_COMPILE_ERROR;
use const E_COMPILE_WARNING;
use const E_CORE_WARNING;
use const E_DEPRECATED;
use const E_ERROR;
use const E_NOTICE;
use const E_PARSE;
use const E_RECOVERABLE_ERROR;
use const E_STRICT;
use const E_USER_DEPRECATED;
use const E_USER_ERROR;
use const E_USER_NOTICE;
use const E_USER_WARNING;
use const E_WARNING;
/**
* @method static ReportingLevel all()
* @method static ReportingLevel error()
* @method static ReportingLevel warning()
* @method static ReportingLevel parse()
* @method static ReportingLevel notice()
* @method static ReportingLevel coreError()
* @method static ReportingLevel coreWarning()
* @method static ReportingLevel compileError()
* @method static ReportingLevel compileWarning()
* @method static ReportingLevel userError()
* @method static ReportingLevel userWarning()
* @method static ReportingLevel userNotice()
* @method static ReportingLevel strict()
* @method static ReportingLevel recoverableError()
* @method static ReportingLevel deprecated()
* @method static ReportingLevel userDeprecated()
*/
class ReportingLevel
{
protected const LEVELS = [
E_ALL => 'E_ALL',
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',
];
protected function __construct(protected readonly int $value)
{
if ($this->value < -1 || $this->value > E_ALL) {
throw new ValueError('The value `'.$this->value.'` is invalid as an error reporting level in PHP.');
}
}
/**
* Returns a new instance by using the error reporting level value.
*/
public static function fromValue(int $value): self
{
return new self($value);
}
/**
* Returns a new instance by using the error reporting level constant name.
*/
public static function fromName(string $name): self
{
/** @var int|false $errorLevel */
$errorLevel = array_search($name, self::LEVELS, true);
if (false === $errorLevel) {
throw new ValueError('The name `'.$name.'` is invalid or an unknown error reporting level name.');
}
return new self($errorLevel);
}
public static function fromEnv(): self
{
return new self(error_reporting());
}
/**
* Returns a new instance by excluded error levels from E_ALL.
*/
public static function fromExclusion(self|string|int ...$levels): self
{
return new self(array_reduce($levels, function (int $carry, self|string|int $level) {
$level = match (true) {
$level instanceof ReportingLevel => $level->value,
is_string($level) => ReportingLevel::fromName($level)->value,
default => ReportingLevel::fromValue($level)->value,
};
if (!array_key_exists($level, self::LEVELS)) {
throw new ValueError('The value `'.$level.'` is invalid as a error reporting level value in PHP.');
}
return $carry & ~$level;
}, E_ALL));
}
/**
* Returns a new instance by adding error levels from the initial no error reporting level.
*/
public static function fromInclusion(self|string|int ...$levels): self
{
return new self(array_reduce($levels, function (int $carry, self|string|int $level) {
$level = match (true) {
$level instanceof ReportingLevel => $level->value,
is_string($level) => ReportingLevel::fromName($level)->value,
default => ReportingLevel::fromValue($level)->value,
};
if (!array_key_exists($level, self::LEVELS)) {
throw new ValueError('The value `'.$level.'` is invalid as a error reporting level value in PHP.');
}
return $carry | $level;
}, 0));
}
/**
* @param array<mixed> $arguments
*/
public static function __callStatic(string $name, array $arguments = []): self
{
return self::fromName(
'E_'.strtoupper(
(string) preg_replace('/(.)(?=[A-Z])/u', '$1_', $name)
)
);
}
public function value(): int
{
return $this->value;
}
public function doesNotContain(self|string|int ...$levels): bool
{
return !$this->contains(...$levels);
}
public function contains(self|string|int ...$levels): bool
{
if ([] === $levels) {
return false;
}
$levels = array_map(fn (self|string|int $level): int => match (true) {
$level instanceof ReportingLevel => $level->value,
is_string($level) => ReportingLevel::fromName($level)->value,
default => ReportingLevel::fromValue($level)->value,
}, $levels);
if (-1 === $this->value) {
return true;
}
if (0 === $this->value) {
return false;
}
foreach ($levels as $level) {
if (1 > $level || $level !== ($this->value & $level)) {
return false;
}
}
return true;
}
/**
* @return array<string>
*/
public function included(): array
{
return array_values(array_filter(
self::LEVELS,
fn (int $error): bool => 0 !== ($error & $this->value),
ARRAY_FILTER_USE_KEY
));
}
/**
* @return array<string>
*/
public function excluded(): array
{
return array_values(array_filter(
self::LEVELS,
fn (int $error): bool => 0 === ($error & $this->value),
ARRAY_FILTER_USE_KEY
));
}
}