ErrorHandler.php (3463B)
1 <?php 2 /* 3 * This file is part of PHPUnit. 4 * 5 * (c) Sebastian Bergmann <sebastian@phpunit.de> 6 * 7 * For the full copyright and license information, please view the LICENSE 8 * file that was distributed with this source code. 9 */ 10 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'; 18 19 /** 20 * Error handler that converts PHP errors and warnings to exceptions. 21 * 22 * @since Class available since Release 3.3.0 23 */ 24 class PHPUnit_Util_ErrorHandler 25 { 26 protected static $errorStack = array(); 27 28 /** 29 * Returns the error stack. 30 * 31 * @return array 32 */ 33 public static function getErrorStack() 34 { 35 return self::$errorStack; 36 } 37 38 /** 39 * @param int $errno 40 * @param string $errstr 41 * @param string $errfile 42 * @param int $errline 43 * 44 * @throws PHPUnit_Framework_Error 45 */ 46 public static function handleError($errno, $errstr, $errfile, $errline) 47 { 48 if (!($errno & error_reporting())) { 49 return false; 50 } 51 52 self::$errorStack[] = array($errno, $errstr, $errfile, $errline); 53 54 $trace = debug_backtrace(false); 55 array_shift($trace); 56 57 foreach ($trace as $frame) { 58 if ($frame['function'] == '__toString') { 59 return false; 60 } 61 } 62 63 if ($errno == E_NOTICE || $errno == E_USER_NOTICE || $errno == E_STRICT) { 64 if (PHPUnit_Framework_Error_Notice::$enabled !== true) { 65 return false; 66 } 67 68 $exception = 'PHPUnit_Framework_Error_Notice'; 69 } elseif ($errno == E_WARNING || $errno == E_USER_WARNING) { 70 if (PHPUnit_Framework_Error_Warning::$enabled !== true) { 71 return false; 72 } 73 74 $exception = 'PHPUnit_Framework_Error_Warning'; 75 } elseif ($errno == E_DEPRECATED || $errno == E_USER_DEPRECATED) { 76 if (PHPUnit_Framework_Error_Deprecated::$enabled !== true) { 77 return false; 78 } 79 80 $exception = 'PHPUnit_Framework_Error_Deprecated'; 81 } else { 82 $exception = 'PHPUnit_Framework_Error'; 83 } 84 85 throw new $exception($errstr, $errno, $errfile, $errline); 86 } 87 88 /** 89 * Registers an error handler and returns a function that will restore 90 * the previous handler when invoked 91 * 92 * @param int $severity PHP predefined error constant 93 * 94 * @throws Exception if event of specified severity is emitted 95 */ 96 public static function handleErrorOnce($severity = E_WARNING) 97 { 98 $terminator = function () { 99 static $expired = false; 100 if (!$expired) { 101 $expired = true; 102 // cleans temporary error handler 103 return restore_error_handler(); 104 } 105 }; 106 107 set_error_handler(function ($errno, $errstr) use ($severity) { 108 if ($errno === $severity) { 109 return; 110 } 111 112 return false; 113 }); 114 115 return $terminator; 116 } 117 }