diff --git a/lib/ErrorHandler.php b/lib/ErrorHandler.php index 754259f..aa57c0d 100644 --- a/lib/ErrorHandler.php +++ b/lib/ErrorHandler.php @@ -45,6 +45,11 @@ final class ErrorHandler */ private $logVariables = true; + /** + * @var null|bool + */ + private $displayErrors; + /** * @var callable */ @@ -186,6 +191,21 @@ public function logVariables(): bool return $this->logVariables; } + public function setDisplayErrors(bool $displayErrors): void + { + $this->displayErrors = $displayErrors; + } + + public function displayErrors(): bool + { + if (null === $this->displayErrors) { + $this->setDisplayErrors((bool) \ini_get('display_errors')); + \assert(null !== $this->displayErrors); + } + + return $this->displayErrors; + } + /** * @param array $scream */ @@ -287,7 +307,7 @@ public function exceptionHandler(Throwable $exception): void } $output .= '

500: Errore interno

'; $output .= \PHP_EOL; - if (true === (bool) \ini_get('display_errors')) { + if ($this->displayErrors()) { $currentEx = $exception; do { $output .= \sprintf( diff --git a/tests/ErrorHandlerTest.php b/tests/ErrorHandlerTest.php index de7c07a..1f5ea8f 100644 --- a/tests/ErrorHandlerTest.php +++ b/tests/ErrorHandlerTest.php @@ -38,7 +38,6 @@ final class ErrorHandlerTest extends TestCase protected function setUp(): void { - \ini_set('display_errors', (string) false); $this->backupErrorLog = (string) \ini_get('error_log'); $this->errorLog = __DIR__ . \DIRECTORY_SEPARATOR . 'error_log_test'; \touch($this->errorLog); @@ -145,7 +144,7 @@ public function testHandleCliException(): void public function testHandleWebExceptionWithDisplay(): void { - \ini_set('display_errors', (string) true); + $this->errorHandler->setDisplayErrors(true); $this->errorHandler->setCli(false); $this->errorHandler->setLogErrors(true); @@ -161,7 +160,7 @@ public function testHandleWebExceptionWithDisplay(): void public function testHandleWebExceptionWithoutDisplay(): void { - \ini_set('display_errors', (string) false); + $this->errorHandler->setDisplayErrors(false); $this->errorHandler->setCli(false); $this->errorHandler->setLogErrors(true);