Skip to content

Commit

Permalink
[ErrorHandler] Never throw on warnings triggered by assert() and set …
Browse files Browse the repository at this point in the history
…assert.exception=1 in Debug::enable()
  • Loading branch information
nicolas-grekas committed Feb 7, 2020
1 parent 05663c3 commit f18ef6c
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/ErrorHandler/Debug.php
Expand Up @@ -29,6 +29,11 @@ public static function enable(): ErrorHandler
ini_set('display_errors', 1);
}

ini_set('zend.assertions', 1);
ini_set('assert.active', 1);
ini_set('assert.warning', 0);
ini_set('assert.exception', 1);

DebugClassLoader::enable();

return ErrorHandler::register(new ErrorHandler(new BufferingLogger(), true));
Expand Down
5 changes: 5 additions & 0 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Expand Up @@ -413,6 +413,11 @@ public function handleError(int $type, string $message, string $file, int $line)
$throw = $this->thrownErrors & $type & $level;
$type &= $level | $this->screamedErrors;

// Never throw on warnings triggered by assert()
if (E_WARNING === $type && 'a' === $message[0] && 0 === strncmp($message, 'assert(): ', 10)) {
$throw = 0;
}

if (!$type || (!$log && !$throw)) {
return !$silenced && $type && $log;
}
Expand Down
35 changes: 35 additions & 0 deletions src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php
Expand Up @@ -615,4 +615,39 @@ public function errorHandlerWhenLoggingProvider(): iterable
}
}
}

public function testAssertQuietEval()
{
$ini = [
ini_set('zend.assertions', 1),
ini_set('assert.active', 1),
ini_set('assert.bail', 0),
ini_set('assert.warning', 1),
ini_set('assert.callback', null),
ini_set('assert.exception', 0),
];

$logger = new BufferingLogger();
$handler = new ErrorHandler($logger);
$handler = ErrorHandler::register($handler);

try {
\assert(false);
} finally {
restore_error_handler();
restore_exception_handler();

ini_set('zend.assertions', $ini[0]);
ini_set('assert.active', $ini[1]);
ini_set('assert.bail', $ini[2]);
ini_set('assert.warning', $ini[3]);
ini_set('assert.callback', $ini[4]);
ini_set('assert.exception', $ini[5]);
}

$logs = $logger->cleanLogs();

$this->assertSame('warning', $logs[0][0]);
$this->assertSame('Warning: assert(): assert(false) failed', $logs[0][1]);
}
}

0 comments on commit f18ef6c

Please sign in to comment.