5
5
namespace Slam \ErrorHandler ;
6
6
7
7
use Doctrine \Common \Util \Debug as DoctrineDebug ;
8
+ use ErrorException ;
9
+ use Throwable ;
8
10
9
11
final class ErrorHandler
10
12
{
13
+ /**
14
+ * @var bool
15
+ */
11
16
private $ autoExit = true ;
17
+
18
+ /**
19
+ * @var null|bool
20
+ */
12
21
private $ cli ;
22
+
23
+ /**
24
+ * @var null|int
25
+ */
13
26
private $ terminalWidth ;
27
+
28
+ /**
29
+ * @var null|resource
30
+ */
14
31
private $ errorOutputStream ;
32
+
33
+ /**
34
+ * @var bool
35
+ */
15
36
private $ hasColorSupport = false ;
37
+
38
+ /**
39
+ * @var null|bool
40
+ */
16
41
private $ logErrors ;
42
+
43
+ /**
44
+ * @var bool
45
+ */
17
46
private $ logVariables = true ;
47
+
48
+ /**
49
+ * @var callable
50
+ */
18
51
private $ emailCallback ;
52
+
53
+ /**
54
+ * @var array<int, bool>
55
+ */
19
56
private $ scream = [];
20
57
58
+ /**
59
+ * @var array<string, string>
60
+ */
21
61
private static $ colors = [
22
62
'<error> ' => "\033[37;41m " ,
23
63
'</error> ' => "\033[0m " ,
24
64
];
25
65
66
+ /**
67
+ * @var array<int, string>
68
+ */
26
69
private static $ errors = [
27
70
\E_COMPILE_ERROR => 'E_COMPILE_ERROR ' ,
28
71
\E_COMPILE_WARNING => 'E_COMPILE_WARNING ' ,
@@ -65,6 +108,7 @@ public function isCli(): bool
65
108
{
66
109
if (null === $ this ->cli ) {
67
110
$ this ->setCli (\PHP_SAPI === 'cli ' );
111
+ \assert (null !== $ this ->cli );
68
112
}
69
113
70
114
return $ this ->cli ;
@@ -85,11 +129,15 @@ public function getTerminalWidth(): int
85
129
}
86
130
87
131
$ this ->setTerminalWidth ((int ) $ width ?: 80 );
132
+ \assert (null !== $ this ->terminalWidth );
88
133
}
89
134
90
135
return $ this ->terminalWidth ;
91
136
}
92
137
138
+ /**
139
+ * @param mixed $errorOutputStream
140
+ */
93
141
public function setErrorOutputStream ($ errorOutputStream ): void
94
142
{
95
143
if (! \is_resource ($ errorOutputStream )) {
@@ -100,10 +148,14 @@ public function setErrorOutputStream($errorOutputStream): void
100
148
$ this ->hasColorSupport = (\function_exists ('posix_isatty ' ) && @\posix_isatty ($ errorOutputStream ));
101
149
}
102
150
151
+ /**
152
+ * @return resource
153
+ */
103
154
public function getErrorOutputStream ()
104
155
{
105
156
if (null === $ this ->errorOutputStream ) {
106
157
$ this ->setErrorOutputStream (\STDERR );
158
+ \assert (null !== $ this ->errorOutputStream );
107
159
}
108
160
109
161
return $ this ->errorOutputStream ;
@@ -118,6 +170,7 @@ public function logErrors(): bool
118
170
{
119
171
if (null === $ this ->logErrors ) {
120
172
$ this ->setLogErrors (! \interface_exists (\PHPUnit \Framework \Test::class));
173
+ \assert (null !== $ this ->logErrors );
121
174
}
122
175
123
176
return $ this ->logErrors ;
@@ -133,11 +186,17 @@ public function logVariables(): bool
133
186
return $ this ->logVariables ;
134
187
}
135
188
189
+ /**
190
+ * @param array<int, bool> $scream
191
+ */
136
192
public function setScreamSilencedErrors (array $ scream ): void
137
193
{
138
194
$ this ->scream = $ scream ;
139
195
}
140
196
197
+ /**
198
+ * @return array<int, bool>
199
+ */
141
200
public function getScreamSilencedErrors (): array
142
201
{
143
202
return $ this ->scream ;
@@ -149,17 +208,23 @@ public function register(): void
149
208
\set_exception_handler ([$ this , 'exceptionHandler ' ]);
150
209
}
151
210
211
+ /**
212
+ * @param int $errno
213
+ * @param string $errstr
214
+ * @param string $errfile
215
+ * @param int $errline
216
+ */
152
217
public function errorHandler ($ errno , $ errstr = '' , $ errfile = '' , $ errline = 0 ): void
153
218
{
154
219
// Mandatory check for @ operator
155
220
if (0 === \error_reporting () && ! isset ($ this ->scream [$ errno ])) {
156
221
return ;
157
222
}
158
223
159
- throw new \ ErrorException ($ errstr , $ errno , $ errno , $ errfile , $ errline );
224
+ throw new ErrorException ($ errstr , $ errno , $ errno , $ errfile , $ errline );
160
225
}
161
226
162
- public function exceptionHandler (\ Throwable $ exception ): void
227
+ public function exceptionHandler (Throwable $ exception ): void
163
228
{
164
229
$ this ->logException ($ exception );
165
230
$ this ->emailException ($ exception );
@@ -256,7 +321,7 @@ private function outputError(string $text): void
256
321
\fwrite ($ this ->getErrorOutputStream (), \str_replace (\array_keys (self ::$ colors ), $ this ->hasColorSupport ? \array_values (self ::$ colors ) : '' , $ text ) . \PHP_EOL );
257
322
}
258
323
259
- public function logException (\ Throwable $ exception ): void
324
+ public function logException (Throwable $ exception ): void
260
325
{
261
326
if (! $ this ->logErrors ()) {
262
327
return ;
@@ -280,7 +345,7 @@ public function logException(\Throwable $exception): void
280
345
} while ($ exception = $ exception ->getPrevious ());
281
346
}
282
347
283
- public function emailException (\ Throwable $ exception ): void
348
+ public function emailException (Throwable $ exception ): void
284
349
{
285
350
if (! $ this ->logErrors ()) {
286
351
return ;
@@ -348,16 +413,16 @@ public function emailException(\Throwable $exception): void
348
413
349
414
try {
350
415
$ callback ($ subject , $ bodyText );
351
- } catch (\ Throwable $ e ) {
416
+ } catch (Throwable $ e ) {
352
417
$ this ->logException ($ e );
353
418
}
354
419
}
355
420
356
- private function getExceptionCode (\ Throwable $ exception ): string
421
+ private function getExceptionCode (Throwable $ exception ): string
357
422
{
358
423
$ code = $ exception ->getCode ();
359
- if ($ exception instanceof \ ErrorException && isset (static ::$ errors [$ code ])) {
360
- $ code = static ::$ errors [$ code ];
424
+ if ($ exception instanceof ErrorException && isset (self ::$ errors [$ code ])) {
425
+ $ code = self ::$ errors [$ code ];
361
426
}
362
427
363
428
return (string ) $ code ;
0 commit comments