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