Skip to content

Commit 28e42fa

Browse files
committed
Ability to scream silenced errors
1 parent 9e6d7a5 commit 28e42fa

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

lib/ErrorHandler.php

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ final class ErrorHandler
1616
private $logErrors;
1717
private $logVariables = true;
1818
private $emailCallback;
19+
private $scream = array();
1920

2021
private static $colors = array(
2122
'<error>' => "\033[37;41m",
@@ -132,6 +133,16 @@ public function logVariables(): bool
132133
return $this->logVariables;
133134
}
134135

136+
public function setScreamSilencesErrors(array $scream): void
137+
{
138+
$this->scream = $scream;
139+
}
140+
141+
public function getScreamSilencesErrors(): array
142+
{
143+
return $this->scream;
144+
}
145+
135146
public function register(): void
136147
{
137148
\set_error_handler(array($this, 'errorHandler'), \error_reporting());
@@ -141,7 +152,7 @@ public function register(): void
141152
public function errorHandler($errno, $errstr = '', $errfile = '', $errline = 0): void
142153
{
143154
// Mandatory check for @ operator
144-
if (0 === \error_reporting()) {
155+
if (0 === \error_reporting() and ! isset($this->scream[$errno])) {
145156
return;
146157
}
147158

tests/ErrorHandlerTest.php

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,31 @@ public function testRegisterBuiltinHandlers()
8484
$arrayPerVerificaErrori['undefined_index'];
8585
}
8686

87+
/**
88+
* @runInSeparateProcess
89+
* @preserveGlobalState disabled
90+
*/
91+
public function testScream()
92+
{
93+
$scream = array(
94+
\E_USER_WARNING => true,
95+
);
96+
97+
$this->assertEmpty($this->errorHandler->getScreamSilencesErrors());
98+
$this->errorHandler->setScreamSilencesErrors($scream);
99+
$this->assertSame($scream, $this->errorHandler->getScreamSilencesErrors());
100+
101+
$this->errorHandler->register();
102+
103+
@ \trigger_error(\uniqid('deprecated_'), \E_USER_DEPRECATED);
104+
105+
$warningMessage = \uniqid('warning_');
106+
$this->expectException(ErrorException::class);
107+
$this->expectExceptionMessageRegexp(\sprintf('/%s/', \preg_quote($warningMessage)));
108+
109+
@ \trigger_error($warningMessage, \E_USER_WARNING);
110+
}
111+
87112
public function testHandleCliException()
88113
{
89114
$memoryStream = \fopen('php://memory', 'r+');

0 commit comments

Comments
 (0)