@@ -36,6 +36,7 @@ final class ErrorHandler
36
36
private bool $ autoExit = true ;
37
37
private ?bool $ cli = null ;
38
38
private ?int $ terminalWidth = null ;
39
+
39
40
/**
40
41
* @var null|resource
41
42
*/
@@ -44,23 +45,29 @@ final class ErrorHandler
44
45
private ?bool $ logErrors = null ;
45
46
private bool $ logVariables = true ;
46
47
private ?bool $ displayErrors = null ;
48
+
47
49
/**
48
50
* @var callable
49
51
*/
50
52
private $ emailCallback ;
53
+
51
54
/**
52
55
* @var callable
53
56
*/
54
57
private $ errorLogCallback = '\\error_log ' ;
58
+
55
59
/**
56
60
* @var array<int, bool>
57
61
*/
58
62
private array $ scream = [];
63
+
59
64
/**
60
65
* @var array<int, class-string<Throwable>>
61
66
*/
62
67
private array $ exceptionsTypesFor404 = [];
63
68
69
+ private bool $ shouldEmail404Exceptions = true ;
70
+
64
71
public function __construct (callable $ emailCallback )
65
72
{
66
73
$ this ->emailCallback = $ emailCallback ;
@@ -109,13 +116,15 @@ public function setTerminalWidth(int $terminalWidth): void
109
116
public function getTerminalWidth (): int
110
117
{
111
118
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
116
125
}
117
126
118
- $ this ->setTerminalWidth (( int ) $ width ?: 80 );
127
+ $ this ->setTerminalWidth ($ width );
119
128
\assert (null !== $ this ->terminalWidth );
120
129
}
121
130
@@ -228,11 +237,11 @@ public function exceptionHandler(Throwable $exception): void
228
237
if ($ this ->isCli ()) {
229
238
$ currentEx = $ exception ;
230
239
do {
231
- $ width = $ this ->getTerminalWidth () ? $ this ->getTerminalWidth () - 3 : 120 ;
240
+ $ width = 0 !== $ this ->getTerminalWidth () ? $ this ->getTerminalWidth () - 3 : 120 ;
232
241
$ lines = [
233
242
'Message: ' . $ currentEx ->getMessage (),
234
243
'' ,
235
- 'Class: ' . \get_class ( $ currentEx) ,
244
+ 'Class: ' . $ currentEx::class ,
236
245
'Code: ' . $ this ->getExceptionCode ($ currentEx ),
237
246
'File: ' . $ currentEx ->getFile () . ': ' . $ currentEx ->getLine (),
238
247
];
@@ -273,7 +282,7 @@ public function exceptionHandler(Throwable $exception): void
273
282
// @codeCoverageIgnoreStart
274
283
if (! \headers_sent ()) {
275
284
$ 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 )) {
277
286
$ header = 'HTTP/1.1 404 Not Found ' ;
278
287
}
279
288
\header ($ header );
@@ -288,7 +297,7 @@ public function renderHtmlException(Throwable $exception): string
288
297
$ ajax = (isset ($ _SERVER ['X_REQUESTED_WITH ' ]) && 'XMLHttpRequest ' === $ _SERVER ['X_REQUESTED_WITH ' ]);
289
298
$ output = '' ;
290
299
$ errorType = '500: Internal Server Error ' ;
291
- if (\in_array (\get_class ( $ exception) , $ this ->exceptionsTypesFor404 , true )) {
300
+ if (\in_array ($ exception::class , $ this ->exceptionsTypesFor404 , true )) {
292
301
$ errorType = '404: Not Found ' ;
293
302
}
294
303
if (! $ ajax ) {
@@ -309,7 +318,7 @@ public function renderHtmlException(Throwable $exception): string
309
318
. '<b>Stack trace:</b><pre>%s</pre> '
310
319
. '</div>%s ' ,
311
320
\htmlspecialchars ($ currentEx ->getMessage ()),
312
- \get_class ( $ currentEx) ,
321
+ $ currentEx::class ,
313
322
$ this ->getExceptionCode ($ currentEx ),
314
323
$ currentEx ->getFile (),
315
324
$ currentEx ->getLine (),
@@ -347,7 +356,7 @@ public function logException(Throwable $exception): void
347
356
$ output = \sprintf (
348
357
'%s%s: %s in %s:%s%s%s ' ,
349
358
($ i > 0 ? '{PR ' . $ i . '} ' : '' ),
350
- \get_class ( $ exception) ,
359
+ $ exception::class ,
351
360
$ exception ->getMessage (),
352
361
$ exception ->getFile (),
353
362
$ exception ->getLine (),
@@ -363,7 +372,13 @@ public function logException(Throwable $exception): void
363
372
364
373
public function emailException (Throwable $ exception ): void
365
374
{
366
- if (! $ this ->logErrors ()) {
375
+ if (
376
+ ! $ this ->logErrors ()
377
+ || (
378
+ ! $ this ->shouldEmail404Exceptions
379
+ && \in_array ($ exception ::class, $ this ->exceptionsTypesFor404 , true )
380
+ )
381
+ ) {
367
382
return ;
368
383
}
369
384
@@ -389,7 +404,7 @@ public function emailException(Throwable $exception): void
389
404
$ currentEx = $ exception ;
390
405
do {
391
406
$ bodyArray = [
392
- 'Class ' => \get_class ( $ currentEx) ,
407
+ 'Class ' => $ currentEx::class ,
393
408
'Code ' => $ this ->getExceptionCode ($ currentEx ),
394
409
'Message ' => $ currentEx ->getMessage (),
395
410
'File ' => $ currentEx ->getFile () . ': ' . $ currentEx ->getLine (),
@@ -405,10 +420,10 @@ public function emailException(Throwable $exception): void
405
420
$ username = null ;
406
421
407
422
if ($ this ->logVariables ()) {
408
- if (! empty ( $ _POST ) ) {
423
+ if ([] !== $ _POST ) {
409
424
$ bodyText .= '$_POST = ' . \print_r ($ _POST , true ) . \PHP_EOL ;
410
425
}
411
- if (isset ($ _SESSION ) && ! empty ( $ _SESSION ) ) {
426
+ if (isset ($ _SESSION ) && [] !== $ _SESSION ) {
412
427
$ sessionText = \print_r (\class_exists (DoctrineDebug::class) ? DoctrineDebug::export ($ _SESSION , 4 ) : $ _SESSION , true );
413
428
$ bodyText .= '$_SESSION = ' . $ sessionText . \PHP_EOL ;
414
429
@@ -422,7 +437,7 @@ public function emailException(Throwable $exception): void
422
437
423
438
$ subject = \sprintf (
424
439
'Error%s: %s ' ,
425
- $ username ? \sprintf (' [%s] ' , $ username ) : '' ,
440
+ null !== $ username ? \sprintf (' [%s] ' , $ username ) : '' ,
426
441
$ exception ->getMessage ()
427
442
);
428
443
@@ -465,4 +480,14 @@ public function get404ExceptionTypes(): array
465
480
{
466
481
return $ this ->exceptionsTypesFor404 ;
467
482
}
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
+ }
468
493
}
0 commit comments