3 * This file is part of PHPUnit.
5 * (c) Sebastian Bergmann <sebastian@phpunit.de>
7 * For the full copyright and license information, please view the LICENSE
8 * file that was distributed with this source code.
11 // Workaround for http://bugs.php.net/bug.php?id=47987,
12 // see https://github.com/sebastianbergmann/phpunit/issues#issue/125 for details
13 // Use dirname(__DIR__) instead of using /../ because of https://github.com/facebook/hhvm/issues/5215
14 require_once dirname(__DIR__) . '/Framework/Error.php';
15 require_once dirname(__DIR__) . '/Framework/Error/Notice.php';
16 require_once dirname(__DIR__) . '/Framework/Error/Warning.php';
17 require_once dirname(__DIR__) . '/Framework/Error/Deprecated.php';
20 * Error handler that converts PHP errors and warnings to exceptions.
22 * @since Class available since Release 3.3.0
24 class PHPUnit_Util_ErrorHandler
26 protected static $errorStack = array();
29 * Returns the error stack.
33 public static function getErrorStack()
35 return self::$errorStack;
40 * @param string $errstr
41 * @param string $errfile
44 * @throws PHPUnit_Framework_Error
46 public static function handleError($errno, $errstr, $errfile, $errline)
48 if (!($errno & error_reporting())) {
52 self::$errorStack[] = array($errno, $errstr, $errfile, $errline);
54 $trace = debug_backtrace(false);
57 foreach ($trace as $frame) {
58 if ($frame['function'] == '__toString') {
63 if ($errno == E_NOTICE || $errno == E_USER_NOTICE || $errno == E_STRICT) {
64 if (PHPUnit_Framework_Error_Notice::$enabled !== true) {
68 $exception = 'PHPUnit_Framework_Error_Notice';
69 } elseif ($errno == E_WARNING || $errno == E_USER_WARNING) {
70 if (PHPUnit_Framework_Error_Warning::$enabled !== true) {
74 $exception = 'PHPUnit_Framework_Error_Warning';
75 } elseif ($errno == E_DEPRECATED || $errno == E_USER_DEPRECATED) {
76 if (PHPUnit_Framework_Error_Deprecated::$enabled !== true) {
80 $exception = 'PHPUnit_Framework_Error_Deprecated';
82 $exception = 'PHPUnit_Framework_Error';
85 throw new $exception($errstr, $errno, $errfile, $errline);
89 * Registers an error handler and returns a function that will restore
90 * the previous handler when invoked
92 * @param int $severity PHP predefined error constant
94 * @throws Exception if event of specified severity is emitted
96 public static function handleErrorOnce($severity = E_WARNING)
98 $terminator = function () {
99 static $expired = false;
102 // cleans temporary error handler
103 return restore_error_handler();
107 set_error_handler(function ($errno, $errstr) use ($severity) {
108 if ($errno === $severity) {