@@ -36,6 +36,7 @@ final class ErrorHandler
3636 private bool $ autoExit = true ;
3737 private ?bool $ cli = null ;
3838 private ?int $ terminalWidth = null ;
39+
3940 /**
4041 * @var null|resource
4142 */
@@ -44,23 +45,29 @@ final class ErrorHandler
4445 private ?bool $ logErrors = null ;
4546 private bool $ logVariables = true ;
4647 private ?bool $ displayErrors = null ;
48+
4749 /**
4850 * @var callable
4951 */
5052 private $ emailCallback ;
53+
5154 /**
5255 * @var callable
5356 */
5457 private $ errorLogCallback = '\\error_log ' ;
58+
5559 /**
5660 * @var array<int, bool>
5761 */
5862 private array $ scream = [];
63+
5964 /**
6065 * @var array<int, class-string<Throwable>>
6166 */
6267 private array $ exceptionsTypesFor404 = [];
6368
69+ private bool $ shouldEmail404Exceptions = true ;
70+
6471 public function __construct (callable $ emailCallback )
6572 {
6673 $ this ->emailCallback = $ emailCallback ;
@@ -109,13 +116,15 @@ public function setTerminalWidth(int $terminalWidth): void
109116 public function getTerminalWidth (): int
110117 {
111118 if (null === $ this ->terminalWidth ) {
112- $ width = \getenv ('COLUMNS ' );
113-
114- if (false === $ width && 1 === \preg_match ('{rows.(\d+);.columns.(\d+);}i ' , (string ) \exec ('stty -a 2> /dev/null | grep columns ' ), $ match )) {
115- $ width = $ match [2 ]; // @codeCoverageIgnore
119+ $ width = (int ) \getenv ('COLUMNS ' );
120+ if (0 === $ width && 1 === \preg_match ('{rows.(\d+);.columns.(\d+);}i ' , (string ) \exec ('stty -a 2> /dev/null | grep columns ' ), $ match )) {
121+ $ width = (int ) $ match [2 ]; // @codeCoverageIgnore
122+ }
123+ if (0 === $ width ) {
124+ $ width = 80 ; // @codeCoverageIgnore
116125 }
117126
118- $ this ->setTerminalWidth (( int ) $ width ?: 80 );
127+ $ this ->setTerminalWidth ($ width );
119128 \assert (null !== $ this ->terminalWidth );
120129 }
121130
@@ -228,11 +237,11 @@ public function exceptionHandler(Throwable $exception): void
228237 if ($ this ->isCli ()) {
229238 $ currentEx = $ exception ;
230239 do {
231- $ width = $ this ->getTerminalWidth () ? $ this ->getTerminalWidth () - 3 : 120 ;
240+ $ width = 0 !== $ this ->getTerminalWidth () ? $ this ->getTerminalWidth () - 3 : 120 ;
232241 $ lines = [
233242 'Message: ' . $ currentEx ->getMessage (),
234243 '' ,
235- 'Class: ' . \get_class ( $ currentEx) ,
244+ 'Class: ' . $ currentEx::class ,
236245 'Code: ' . $ this ->getExceptionCode ($ currentEx ),
237246 'File: ' . $ currentEx ->getFile () . ': ' . $ currentEx ->getLine (),
238247 ];
@@ -273,7 +282,7 @@ public function exceptionHandler(Throwable $exception): void
273282 // @codeCoverageIgnoreStart
274283 if (! \headers_sent ()) {
275284 $ header = 'HTTP/1.1 500 Internal Server Error ' ;
276- if (\in_array (\get_class ( $ exception) , $ this ->exceptionsTypesFor404 , true )) {
285+ if (\in_array ($ exception::class , $ this ->exceptionsTypesFor404 , true )) {
277286 $ header = 'HTTP/1.1 404 Not Found ' ;
278287 }
279288 \header ($ header );
@@ -288,7 +297,7 @@ public function renderHtmlException(Throwable $exception): string
288297 $ ajax = (isset ($ _SERVER ['X_REQUESTED_WITH ' ]) && 'XMLHttpRequest ' === $ _SERVER ['X_REQUESTED_WITH ' ]);
289298 $ output = '' ;
290299 $ errorType = '500: Internal Server Error ' ;
291- if (\in_array (\get_class ( $ exception) , $ this ->exceptionsTypesFor404 , true )) {
300+ if (\in_array ($ exception::class , $ this ->exceptionsTypesFor404 , true )) {
292301 $ errorType = '404: Not Found ' ;
293302 }
294303 if (! $ ajax ) {
@@ -309,7 +318,7 @@ public function renderHtmlException(Throwable $exception): string
309318 . '<b>Stack trace:</b><pre>%s</pre> '
310319 . '</div>%s ' ,
311320 \htmlspecialchars ($ currentEx ->getMessage ()),
312- \get_class ( $ currentEx) ,
321+ $ currentEx::class ,
313322 $ this ->getExceptionCode ($ currentEx ),
314323 $ currentEx ->getFile (),
315324 $ currentEx ->getLine (),
@@ -347,7 +356,7 @@ public function logException(Throwable $exception): void
347356 $ output = \sprintf (
348357 '%s%s: %s in %s:%s%s%s ' ,
349358 ($ i > 0 ? '{PR ' . $ i . '} ' : '' ),
350- \get_class ( $ exception) ,
359+ $ exception::class ,
351360 $ exception ->getMessage (),
352361 $ exception ->getFile (),
353362 $ exception ->getLine (),
@@ -363,7 +372,13 @@ public function logException(Throwable $exception): void
363372
364373 public function emailException (Throwable $ exception ): void
365374 {
366- if (! $ this ->logErrors ()) {
375+ if (
376+ ! $ this ->logErrors ()
377+ || (
378+ ! $ this ->shouldEmail404Exceptions
379+ && \in_array ($ exception ::class, $ this ->exceptionsTypesFor404 , true )
380+ )
381+ ) {
367382 return ;
368383 }
369384
@@ -389,7 +404,7 @@ public function emailException(Throwable $exception): void
389404 $ currentEx = $ exception ;
390405 do {
391406 $ bodyArray = [
392- 'Class ' => \get_class ( $ currentEx) ,
407+ 'Class ' => $ currentEx::class ,
393408 'Code ' => $ this ->getExceptionCode ($ currentEx ),
394409 'Message ' => $ currentEx ->getMessage (),
395410 'File ' => $ currentEx ->getFile () . ': ' . $ currentEx ->getLine (),
@@ -405,10 +420,10 @@ public function emailException(Throwable $exception): void
405420 $ username = null ;
406421
407422 if ($ this ->logVariables ()) {
408- if (! empty ( $ _POST ) ) {
423+ if ([] !== $ _POST ) {
409424 $ bodyText .= '$_POST = ' . \print_r ($ _POST , true ) . \PHP_EOL ;
410425 }
411- if (isset ($ _SESSION ) && ! empty ( $ _SESSION ) ) {
426+ if (isset ($ _SESSION ) && [] !== $ _SESSION ) {
412427 $ sessionText = \print_r (\class_exists (DoctrineDebug::class) ? DoctrineDebug::export ($ _SESSION , 4 ) : $ _SESSION , true );
413428 $ bodyText .= '$_SESSION = ' . $ sessionText . \PHP_EOL ;
414429
@@ -422,7 +437,7 @@ public function emailException(Throwable $exception): void
422437
423438 $ subject = \sprintf (
424439 'Error%s: %s ' ,
425- $ username ? \sprintf (' [%s] ' , $ username ) : '' ,
440+ null !== $ username ? \sprintf (' [%s] ' , $ username ) : '' ,
426441 $ exception ->getMessage ()
427442 );
428443
@@ -465,4 +480,14 @@ public function get404ExceptionTypes(): array
465480 {
466481 return $ this ->exceptionsTypesFor404 ;
467482 }
483+
484+ public function setShouldEmail404Exceptions (bool $ shouldEmail404Exceptions ): void
485+ {
486+ $ this ->shouldEmail404Exceptions = $ shouldEmail404Exceptions ;
487+ }
488+
489+ public function shouldEmail404Exceptions (): bool
490+ {
491+ return $ this ->shouldEmail404Exceptions ;
492+ }
468493}
0 commit comments