55namespace Slam \ErrorHandler ;
66
77use Doctrine \Common \Util \Debug as DoctrineDebug ;
8+ use ErrorException ;
9+ use Throwable ;
810
911final class ErrorHandler
1012{
13+ /**
14+ * @var bool
15+ */
1116 private $ autoExit = true ;
17+
18+ /**
19+ * @var null|bool
20+ */
1221 private $ cli ;
22+
23+ /**
24+ * @var null|int
25+ */
1326 private $ terminalWidth ;
27+
28+ /**
29+ * @var null|resource
30+ */
1431 private $ errorOutputStream ;
32+
33+ /**
34+ * @var bool
35+ */
1536 private $ hasColorSupport = false ;
37+
38+ /**
39+ * @var null|bool
40+ */
1641 private $ logErrors ;
42+
43+ /**
44+ * @var bool
45+ */
1746 private $ logVariables = true ;
47+
48+ /**
49+ * @var callable
50+ */
1851 private $ emailCallback ;
52+
53+ /**
54+ * @var array<int, bool>
55+ */
1956 private $ scream = [];
2057
58+ /**
59+ * @var array<string, string>
60+ */
2161 private static $ colors = [
2262 '<error> ' => "\033[37;41m " ,
2363 '</error> ' => "\033[0m " ,
2464 ];
2565
66+ /**
67+ * @var array<int, string>
68+ */
2669 private static $ errors = [
2770 \E_COMPILE_ERROR => 'E_COMPILE_ERROR ' ,
2871 \E_COMPILE_WARNING => 'E_COMPILE_WARNING ' ,
@@ -65,6 +108,7 @@ public function isCli(): bool
65108 {
66109 if (null === $ this ->cli ) {
67110 $ this ->setCli (\PHP_SAPI === 'cli ' );
111+ \assert (null !== $ this ->cli );
68112 }
69113
70114 return $ this ->cli ;
@@ -85,11 +129,15 @@ public function getTerminalWidth(): int
85129 }
86130
87131 $ this ->setTerminalWidth ((int ) $ width ?: 80 );
132+ \assert (null !== $ this ->terminalWidth );
88133 }
89134
90135 return $ this ->terminalWidth ;
91136 }
92137
138+ /**
139+ * @param mixed $errorOutputStream
140+ */
93141 public function setErrorOutputStream ($ errorOutputStream ): void
94142 {
95143 if (! \is_resource ($ errorOutputStream )) {
@@ -100,10 +148,14 @@ public function setErrorOutputStream($errorOutputStream): void
100148 $ this ->hasColorSupport = (\function_exists ('posix_isatty ' ) && @\posix_isatty ($ errorOutputStream ));
101149 }
102150
151+ /**
152+ * @return resource
153+ */
103154 public function getErrorOutputStream ()
104155 {
105156 if (null === $ this ->errorOutputStream ) {
106157 $ this ->setErrorOutputStream (\STDERR );
158+ \assert (null !== $ this ->errorOutputStream );
107159 }
108160
109161 return $ this ->errorOutputStream ;
@@ -118,6 +170,7 @@ public function logErrors(): bool
118170 {
119171 if (null === $ this ->logErrors ) {
120172 $ this ->setLogErrors (! \interface_exists (\PHPUnit \Framework \Test::class));
173+ \assert (null !== $ this ->logErrors );
121174 }
122175
123176 return $ this ->logErrors ;
@@ -133,11 +186,17 @@ public function logVariables(): bool
133186 return $ this ->logVariables ;
134187 }
135188
189+ /**
190+ * @param array<int, bool> $scream
191+ */
136192 public function setScreamSilencedErrors (array $ scream ): void
137193 {
138194 $ this ->scream = $ scream ;
139195 }
140196
197+ /**
198+ * @return array<int, bool>
199+ */
141200 public function getScreamSilencedErrors (): array
142201 {
143202 return $ this ->scream ;
@@ -149,17 +208,23 @@ public function register(): void
149208 \set_exception_handler ([$ this , 'exceptionHandler ' ]);
150209 }
151210
211+ /**
212+ * @param int $errno
213+ * @param string $errstr
214+ * @param string $errfile
215+ * @param int $errline
216+ */
152217 public function errorHandler ($ errno , $ errstr = '' , $ errfile = '' , $ errline = 0 ): void
153218 {
154219 // Mandatory check for @ operator
155220 if (0 === \error_reporting () && ! isset ($ this ->scream [$ errno ])) {
156221 return ;
157222 }
158223
159- throw new \ ErrorException ($ errstr , $ errno , $ errno , $ errfile , $ errline );
224+ throw new ErrorException ($ errstr , $ errno , $ errno , $ errfile , $ errline );
160225 }
161226
162- public function exceptionHandler (\ Throwable $ exception ): void
227+ public function exceptionHandler (Throwable $ exception ): void
163228 {
164229 $ this ->logException ($ exception );
165230 $ this ->emailException ($ exception );
@@ -256,7 +321,7 @@ private function outputError(string $text): void
256321 \fwrite ($ this ->getErrorOutputStream (), \str_replace (\array_keys (self ::$ colors ), $ this ->hasColorSupport ? \array_values (self ::$ colors ) : '' , $ text ) . \PHP_EOL );
257322 }
258323
259- public function logException (\ Throwable $ exception ): void
324+ public function logException (Throwable $ exception ): void
260325 {
261326 if (! $ this ->logErrors ()) {
262327 return ;
@@ -280,7 +345,7 @@ public function logException(\Throwable $exception): void
280345 } while ($ exception = $ exception ->getPrevious ());
281346 }
282347
283- public function emailException (\ Throwable $ exception ): void
348+ public function emailException (Throwable $ exception ): void
284349 {
285350 if (! $ this ->logErrors ()) {
286351 return ;
@@ -348,16 +413,16 @@ public function emailException(\Throwable $exception): void
348413
349414 try {
350415 $ callback ($ subject , $ bodyText );
351- } catch (\ Throwable $ e ) {
416+ } catch (Throwable $ e ) {
352417 $ this ->logException ($ e );
353418 }
354419 }
355420
356- private function getExceptionCode (\ Throwable $ exception ): string
421+ private function getExceptionCode (Throwable $ exception ): string
357422 {
358423 $ 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 ];
361426 }
362427
363428 return (string ) $ code ;
0 commit comments