1010
1111final class ErrorHandler
1212{
13- /**
14- * @var bool
15- */
16- private $ autoExit = true ;
17-
18- /**
19- * @var null|bool
20- */
21- private $ cli ;
22-
23- /**
24- * @var null|int
25- */
26- private $ terminalWidth ;
27-
28- /**
29- * @var null|resource
30- */
31- private $ errorOutputStream ;
32-
33- /**
34- * @var bool
35- */
36- private $ hasColorSupport = false ;
37-
38- /**
39- * @var null|bool
40- */
41- private $ logErrors ;
42-
43- /**
44- * @var bool
45- */
46- private $ logVariables = true ;
47-
48- /**
49- * @var null|bool
50- */
51- private $ displayErrors ;
52-
53- /**
54- * @var callable
55- */
56- private $ emailCallback ;
57-
58- /**
59- * @var callable
60- */
61- private $ errorLogCallback = '\\error_log ' ;
62-
63- /**
64- * @var array<int, bool>
65- */
66- private $ scream = [];
67-
68- /**
69- * @var array<string, string>
70- */
71- private static $ colors = [
13+ private const COLORS = [
7214 '<error> ' => "\033[37;41m " ,
7315 '</error> ' => "\033[0m " ,
7416 ];
7517
76- /**
77- * @var array<int, string>
78- */
79- private static $ errors = [
18+ private const ERRORS = [
8019 \E_COMPILE_ERROR => 'E_COMPILE_ERROR ' ,
8120 \E_COMPILE_WARNING => 'E_COMPILE_WARNING ' ,
8221 \E_CORE_ERROR => 'E_CORE_ERROR ' ,
@@ -94,10 +33,33 @@ final class ErrorHandler
9433 \E_WARNING => 'E_WARNING ' ,
9534 ];
9635
36+ private bool $ autoExit = true ;
37+ private ?bool $ cli = null ;
38+ private ?int $ terminalWidth = null ;
39+ /**
40+ * @var null|resource
41+ */
42+ private $ errorOutputStream ;
43+ private bool $ hasColorSupport = false ;
44+ private ?bool $ logErrors = null ;
45+ private bool $ logVariables = true ;
46+ private ?bool $ displayErrors = null ;
47+ /**
48+ * @var callable
49+ */
50+ private $ emailCallback ;
51+ /**
52+ * @var callable
53+ */
54+ private $ errorLogCallback = '\\error_log ' ;
55+ /**
56+ * @var array<int, bool>
57+ */
58+ private array $ scream = [];
9759 /**
9860 * @var array<int, class-string<Throwable>>
9961 */
100- private $ exceptionsTypesFor404 = [];
62+ private array $ exceptionsTypesFor404 = [];
10163
10264 public function __construct (callable $ emailCallback )
10365 {
@@ -248,17 +210,11 @@ public function register(): void
248210 \set_exception_handler ([$ this , 'exceptionHandler ' ]);
249211 }
250212
251- /**
252- * @param int $errno
253- * @param string $errstr
254- * @param string $errfile
255- * @param int $errline
256- */
257- public function errorHandler ($ errno , $ errstr = '' , $ errfile = '' , $ errline = 0 ): void
213+ public function errorHandler (int $ errno , string $ errstr = '' , string $ errfile = '' , int $ errline = 0 ): bool
258214 {
259215 // Mandatory check for @ operator
260- if (0 === \error_reporting () && ! isset ($ this ->scream [$ errno ])) {
261- return ;
216+ if (0 === ( \error_reporting () & $ errno ) && ! isset ($ this ->scream [$ errno ])) {
217+ return true ;
262218 }
263219
264220 throw new ErrorException ($ errstr , $ errno , $ errno , $ errfile , $ errline );
@@ -371,7 +327,11 @@ public function renderHtmlException(Throwable $exception): string
371327
372328 private function outputError (string $ text ): void
373329 {
374- \fwrite ($ this ->getErrorOutputStream (), \str_replace (\array_keys (self ::$ colors ), $ this ->hasColorSupport ? \array_values (self ::$ colors ) : '' , $ text ) . \PHP_EOL );
330+ \fwrite ($ this ->getErrorOutputStream (), \str_replace (
331+ \array_keys (self ::COLORS ),
332+ $ this ->hasColorSupport ? \array_values (self ::COLORS ) : '' ,
333+ $ text
334+ ) . \PHP_EOL );
375335 }
376336
377337 public function logException (Throwable $ exception ): void
@@ -476,8 +436,8 @@ public function emailException(Throwable $exception): void
476436 private function getExceptionCode (Throwable $ exception ): string
477437 {
478438 $ code = $ exception ->getCode ();
479- if ($ exception instanceof ErrorException && isset (self ::$ errors [$ code ])) {
480- $ code = self ::$ errors [$ code ];
439+ if ($ exception instanceof ErrorException && isset (self ::ERRORS [$ code ])) {
440+ $ code = self ::ERRORS [$ code ];
481441 }
482442
483443 return (string ) $ code ;
0 commit comments