-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
Copy pathErrorHandler.php
268 lines (222 loc) · 7.72 KB
/
ErrorHandler.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?php declare(strict_types=1);
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace PHPUnit\Runner;
use const DEBUG_BACKTRACE_IGNORE_ARGS;
use const E_COMPILE_ERROR;
use const E_COMPILE_WARNING;
use const E_CORE_ERROR;
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;
use function assert;
use function debug_backtrace;
use function error_reporting;
use function restore_error_handler;
use function set_error_handler;
use PHPUnit\Event;
use PHPUnit\Event\Code\IssueTrigger\IssueTrigger;
use PHPUnit\Event\Code\NoTestCaseObjectOnCallStackException;
use PHPUnit\Event\Code\TestMethod;
use PHPUnit\Runner\Baseline\Baseline;
use PHPUnit\Runner\Baseline\Issue;
use PHPUnit\TextUI\Configuration\Registry;
use PHPUnit\TextUI\Configuration\Source;
use PHPUnit\TextUI\Configuration\SourceFilter;
use PHPUnit\Util\ExcludeList;
/**
* @internal This class is not covered by the backward compatibility promise for PHPUnit
*/
final class ErrorHandler
{
private const UNHANDLEABLE_LEVELS = E_ERROR | E_PARSE | E_CORE_ERROR | E_CORE_WARNING | E_COMPILE_ERROR | E_COMPILE_WARNING;
private const INSUPPRESSIBLE_LEVELS = E_ERROR | E_PARSE | E_CORE_ERROR | E_COMPILE_ERROR | E_USER_ERROR | E_RECOVERABLE_ERROR;
private static ?self $instance = null;
private ?Baseline $baseline = null;
private bool $enabled = false;
private ?int $originalErrorReportingLevel = null;
private readonly Source $source;
private readonly SourceFilter $sourceFilter;
public static function instance(): self
{
return self::$instance ?? self::$instance = new self(Registry::get()->source());
}
private function __construct(Source $source)
{
$this->source = $source;
$this->sourceFilter = new SourceFilter;
}
/**
* @throws NoTestCaseObjectOnCallStackException
*/
public function __invoke(int $errorNumber, string $errorString, string $errorFile, int $errorLine): bool
{
$suppressed = (error_reporting() & ~self::INSUPPRESSIBLE_LEVELS) === 0;
if ($suppressed && (new ExcludeList)->isExcluded($errorFile)) {
return false;
}
$test = Event\Code\TestMethodBuilder::fromCallStack();
$ignoredByBaseline = $this->ignoredByBaseline($errorFile, $errorLine, $errorString);
$ignoredByTest = $test->metadata()->isIgnoreDeprecations()->isNotEmpty();
switch ($errorNumber) {
case E_NOTICE:
case E_STRICT:
Event\Facade::emitter()->testTriggeredPhpNotice(
$test,
$errorString,
$errorFile,
$errorLine,
$suppressed,
$ignoredByBaseline,
);
break;
case E_USER_NOTICE:
Event\Facade::emitter()->testTriggeredNotice(
$test,
$errorString,
$errorFile,
$errorLine,
$suppressed,
$ignoredByBaseline,
);
break;
case E_WARNING:
Event\Facade::emitter()->testTriggeredPhpWarning(
$test,
$errorString,
$errorFile,
$errorLine,
$suppressed,
$ignoredByBaseline,
);
break;
case E_USER_WARNING:
Event\Facade::emitter()->testTriggeredWarning(
$test,
$errorString,
$errorFile,
$errorLine,
$suppressed,
$ignoredByBaseline,
);
break;
case E_DEPRECATED:
Event\Facade::emitter()->testTriggeredPhpDeprecation(
$test,
$errorString,
$errorFile,
$errorLine,
$suppressed,
$ignoredByBaseline,
$ignoredByTest,
$this->trigger($test),
);
break;
case E_USER_DEPRECATED:
Event\Facade::emitter()->testTriggeredDeprecation(
$test,
$errorString,
$errorFile,
$errorLine,
$suppressed,
$ignoredByBaseline,
$ignoredByTest,
$this->trigger($test),
);
break;
case E_USER_ERROR:
Event\Facade::emitter()->testTriggeredError(
$test,
$errorString,
$errorFile,
$errorLine,
$suppressed,
);
throw new ErrorException('E_USER_ERROR was triggered');
default:
return false;
}
return false;
}
public function enable(): void
{
if ($this->enabled) {
return;
}
$oldErrorHandler = set_error_handler($this);
if ($oldErrorHandler !== null) {
restore_error_handler();
return;
}
$this->enabled = true;
$this->originalErrorReportingLevel = error_reporting();
error_reporting($this->originalErrorReportingLevel & self::UNHANDLEABLE_LEVELS);
}
public function disable(): void
{
if (!$this->enabled) {
return;
}
restore_error_handler();
error_reporting(error_reporting() | $this->originalErrorReportingLevel);
$this->enabled = false;
$this->originalErrorReportingLevel = null;
}
public function use(Baseline $baseline): void
{
$this->baseline = $baseline;
}
/**
* @psalm-param non-empty-string $file
* @psalm-param positive-int $line
* @psalm-param non-empty-string $description
*/
private function ignoredByBaseline(string $file, int $line, string $description): bool
{
if ($this->baseline === null) {
return false;
}
return $this->baseline->has(Issue::from($file, $line, null, $description));
}
private function trigger(TestMethod $test): IssueTrigger
{
if (!$this->source->notEmpty()) {
return IssueTrigger::unknown();
}
$trace = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 4);
assert(isset($trace[2]['file']));
assert(isset($trace[3]['file']));
$triggeredInFirstPartyCode = false;
$triggerCalledFromFirstPartyCode = false;
if ($trace[2]['file'] === $test->file() ||
$this->sourceFilter->includes($this->source, $trace[2]['file'])) {
$triggeredInFirstPartyCode = true;
}
if ($trace[3]['file'] === $test->file() ||
$this->sourceFilter->includes($this->source, $trace[3]['file'])) {
$triggerCalledFromFirstPartyCode = true;
}
if ($triggerCalledFromFirstPartyCode) {
if ($triggeredInFirstPartyCode) {
return IssueTrigger::self();
}
return IssueTrigger::direct();
}
return IssueTrigger::indirect();
}
}