|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <[email protected]> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Bridge\PhpUnit; |
| 13 | + |
| 14 | +/** |
| 15 | + * Catch deprecation notices and print a summary report at the end of the test suite |
| 16 | + * |
| 17 | + * @author Nicolas Grekas <[email protected]> |
| 18 | + */ |
| 19 | +class DeprecationErrorHandler |
| 20 | +{ |
| 21 | + private static $isRegistered = false; |
| 22 | + |
| 23 | + public static function register($strict = false) |
| 24 | + { |
| 25 | + if (self::$isRegistered) { |
| 26 | + return; |
| 27 | + } |
| 28 | + $deprecations = array( |
| 29 | + 'remainingCount' => 0, |
| 30 | + 'legacyCount' => 0, |
| 31 | + 'otherCount' => 0, |
| 32 | + 'remaining' => array(), |
| 33 | + 'legacy' => array(), |
| 34 | + 'other' => array(), |
| 35 | + ); |
| 36 | + $deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $strict) { |
| 37 | + if (E_USER_DEPRECATED !== $type) { |
| 38 | + return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context); |
| 39 | + } |
| 40 | + |
| 41 | + $trace = debug_backtrace(PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT : true); |
| 42 | + |
| 43 | + $i = count($trace); |
| 44 | + while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) { |
| 45 | + // No-op |
| 46 | + } |
| 47 | + |
| 48 | + if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) { |
| 49 | + $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class']; |
| 50 | + $method = $trace[$i]['function']; |
| 51 | + |
| 52 | + $group = 0 === strpos($method, 'testLegacy') || 0 === strpos($method, 'provideLegacy') || 0 === strpos($method, 'getLegacy') || strpos($class, '\Legacy') || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true) ? 'legacy' : 'remaining'; |
| 53 | + |
| 54 | + if ('legacy' === $group && 0 === (error_reporting() & E_USER_DEPRECATED)) { |
| 55 | + $ref =& $deprecations[$group]['Silenced']['count']; |
| 56 | + ++$ref; |
| 57 | + } else { |
| 58 | + $ref =& $deprecations[$group][$msg]['count']; |
| 59 | + ++$ref; |
| 60 | + $ref =& $deprecations[$group][$msg][$class.'::'.$method]; |
| 61 | + ++$ref; |
| 62 | + } |
| 63 | + } else { |
| 64 | + $group = 'other'; |
| 65 | + $ref =& $deprecations[$group][$msg]['count']; |
| 66 | + ++$ref; |
| 67 | + } |
| 68 | + ++$deprecations[$group.'Count']; |
| 69 | + unset($trace, $ref); |
| 70 | + |
| 71 | + if ('legacy' !== $group) { |
| 72 | + try { |
| 73 | + $e = $strict ? error_reporting(-1) : error_reporting(); |
| 74 | + $result = \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context); |
| 75 | + error_reporting($e); |
| 76 | + } catch (\Exception $x) { |
| 77 | + error_reporting($e); |
| 78 | + |
| 79 | + throw $x; |
| 80 | + } |
| 81 | + |
| 82 | + return $result; |
| 83 | + } |
| 84 | + }; |
| 85 | + $oldErrorHandler = set_error_handler($deprecationHandler); |
| 86 | + |
| 87 | + if (null !== $oldErrorHandler) { |
| 88 | + restore_error_handler(); |
| 89 | + if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) { |
| 90 | + restore_error_handler(); |
| 91 | + self::register(); |
| 92 | + } |
| 93 | + } else { |
| 94 | + self::$isRegistered = true; |
| 95 | + if (self::hasColorSupport()) { |
| 96 | + $colorize = function ($str, $red) { |
| 97 | + $color = $red ? '41;37' : '43;30'; |
| 98 | + |
| 99 | + return "\x1B[{$color}m{$str}\x1B[0m"; |
| 100 | + }; |
| 101 | + } else { |
| 102 | + $colorize = function ($str) {return $str;}; |
| 103 | + } |
| 104 | + register_shutdown_function(function () use (&$deprecations, $deprecationHandler, $colorize) { |
| 105 | + $currErrorHandler = set_error_handler('var_dump'); |
| 106 | + restore_error_handler(); |
| 107 | + |
| 108 | + if ($currErrorHandler !== $deprecationHandler) { |
| 109 | + echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n"; |
| 110 | + } |
| 111 | + |
| 112 | + $cmp = function ($a, $b) { |
| 113 | + return $b['count'] - $a['count']; |
| 114 | + }; |
| 115 | + |
| 116 | + foreach (array('remaining', 'legacy', 'other') as $group) { |
| 117 | + if ($deprecations[$group]) { |
| 118 | + echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n"; |
| 119 | + |
| 120 | + uasort($deprecations[$group], $cmp); |
| 121 | + |
| 122 | + foreach ($deprecations[$group] as $msg => $notices) { |
| 123 | + echo "\n", $msg, ': ', $notices['count'], "x\n"; |
| 124 | + |
| 125 | + arsort($notices); |
| 126 | + |
| 127 | + foreach ($notices as $method => $count) { |
| 128 | + if ('count' !== $method) { |
| 129 | + echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n"; |
| 130 | + } |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + if (!empty($notices)) { |
| 136 | + echo "\n"; |
| 137 | + } |
| 138 | + }); |
| 139 | + } |
| 140 | + } |
| 141 | + |
| 142 | + private static function hasColorSupport() |
| 143 | + { |
| 144 | + if ('\\' === DIRECTORY_SEPARATOR) { |
| 145 | + return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI'); |
| 146 | + } |
| 147 | + |
| 148 | + return defined('STDOUT') && function_exists('posix_isatty') && @posix_isatty(STDOUT); |
| 149 | + } |
| 150 | +} |
0 commit comments