Skip to content

Commit

Permalink
[ErrorHandler] Make assert() silent when assert.quiet_eval=1
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Feb 7, 2020
1 parent 83a53a5 commit cd24287
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Symfony/Component/ErrorHandler/CHANGELOG.md
@@ -1,6 +1,11 @@
CHANGELOG
=========

5.1.0
-----

* made `assert()` silent when `assert.quiet_eval=1`

4.4.0
-----

Expand Down
8 changes: 8 additions & 0 deletions src/Symfony/Component/ErrorHandler/ErrorHandler.php
Expand Up @@ -104,6 +104,7 @@ class ErrorHandler
private static $silencedErrorCache = [];
private static $silencedErrorCount = 0;
private static $exitCode = 0;
private static $assertQuietEval = false;

/**
* Registers the error handler.
Expand All @@ -115,6 +116,8 @@ public static function register(self $handler = null, bool $replace = true): sel
register_shutdown_function(__CLASS__.'::handleFatalError');
}

self::$assertQuietEval = filter_var(ini_get('assert.quiet_eval'), FILTER_VALIDATE_BOOLEAN);

if ($handlerIsNew = null === $handler) {
$handler = new static();
}
Expand Down Expand Up @@ -413,6 +416,11 @@ public function handleError(int $type, string $message, string $file, int $line)
$throw = $this->thrownErrors & $type & $level;
$type &= $level | $this->screamedErrors;

// Make assert() silent when assert.quiet_eval=1, workaround https://bugs.php.net/75769
if (E_WARNING === $type && 'a' === $message[0] && self::$assertQuietEval && 0 === strncmp($message, 'assert(): ', 10)) {
$throw = $level = 0;
}

if (!$type || (!$log && !$throw)) {
return !$silenced && $type && $log;
}
Expand Down
37 changes: 37 additions & 0 deletions src/Symfony/Component/ErrorHandler/Tests/ErrorHandlerTest.php
Expand Up @@ -615,4 +615,41 @@ 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.quiet_eval', 1),
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.quiet_eval', $ini[5]);
ini_set('assert.exception', $ini[6]);
}

$logs = $logger->cleanLogs();

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

0 comments on commit cd24287

Please sign in to comment.