Skip to content

Commit 1a948d0

Browse files
authored
Customizable display error flag (#3)
1 parent 7712198 commit 1a948d0

File tree

2 files changed

+23
-4
lines changed

2 files changed

+23
-4
lines changed

lib/ErrorHandler.php

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ final class ErrorHandler
4545
*/
4646
private $logVariables = true;
4747

48+
/**
49+
* @var null|bool
50+
*/
51+
private $displayErrors;
52+
4853
/**
4954
* @var callable
5055
*/
@@ -186,6 +191,21 @@ public function logVariables(): bool
186191
return $this->logVariables;
187192
}
188193

194+
public function setDisplayErrors(bool $displayErrors): void
195+
{
196+
$this->displayErrors = $displayErrors;
197+
}
198+
199+
public function displayErrors(): bool
200+
{
201+
if (null === $this->displayErrors) {
202+
$this->setDisplayErrors((bool) \ini_get('display_errors'));
203+
\assert(null !== $this->displayErrors);
204+
}
205+
206+
return $this->displayErrors;
207+
}
208+
189209
/**
190210
* @param array<int, bool> $scream
191211
*/
@@ -287,7 +307,7 @@ public function exceptionHandler(Throwable $exception): void
287307
}
288308
$output .= '<h1>500: Errore interno</h1>';
289309
$output .= \PHP_EOL;
290-
if (true === (bool) \ini_get('display_errors')) {
310+
if ($this->displayErrors()) {
291311
$currentEx = $exception;
292312
do {
293313
$output .= \sprintf(

tests/ErrorHandlerTest.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ final class ErrorHandlerTest extends TestCase
3838

3939
protected function setUp(): void
4040
{
41-
\ini_set('display_errors', (string) false);
4241
$this->backupErrorLog = (string) \ini_get('error_log');
4342
$this->errorLog = __DIR__ . \DIRECTORY_SEPARATOR . 'error_log_test';
4443
\touch($this->errorLog);
@@ -145,7 +144,7 @@ public function testHandleCliException(): void
145144

146145
public function testHandleWebExceptionWithDisplay(): void
147146
{
148-
\ini_set('display_errors', (string) true);
147+
$this->errorHandler->setDisplayErrors(true);
149148
$this->errorHandler->setCli(false);
150149
$this->errorHandler->setLogErrors(true);
151150

@@ -161,7 +160,7 @@ public function testHandleWebExceptionWithDisplay(): void
161160

162161
public function testHandleWebExceptionWithoutDisplay(): void
163162
{
164-
\ini_set('display_errors', (string) false);
163+
$this->errorHandler->setDisplayErrors(false);
165164
$this->errorHandler->setCli(false);
166165
$this->errorHandler->setLogErrors(true);
167166

0 commit comments

Comments
 (0)