annotate vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php @ 17:129ea1e6d783

Update, including to Drupal core 8.6.10
author Chris Cannam
date Thu, 28 Feb 2019 13:21:36 +0000
parents c2387f117808
children af1871eacc83
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 /*
Chris@0 4 * This file is part of the Symfony package.
Chris@0 5 *
Chris@0 6 * (c) Fabien Potencier <fabien@symfony.com>
Chris@0 7 *
Chris@0 8 * For the full copyright and license information, please view the LICENSE
Chris@0 9 * file that was distributed with this source code.
Chris@0 10 */
Chris@0 11
Chris@0 12 namespace Symfony\Bridge\PhpUnit;
Chris@0 13
Chris@0 14 /**
Chris@0 15 * Catch deprecation notices and print a summary report at the end of the test suite.
Chris@0 16 *
Chris@0 17 * @author Nicolas Grekas <p@tchwork.com>
Chris@0 18 */
Chris@0 19 class DeprecationErrorHandler
Chris@0 20 {
Chris@0 21 const MODE_WEAK = 'weak';
Chris@14 22 const MODE_WEAK_VENDORS = 'weak_vendors';
Chris@0 23 const MODE_DISABLED = 'disabled';
Chris@0 24
Chris@0 25 private static $isRegistered = false;
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Registers and configures the deprecation handler.
Chris@0 29 *
Chris@0 30 * The following reporting modes are supported:
Chris@0 31 * - use "weak" to hide the deprecation report but keep a global count;
Chris@14 32 * - use "weak_vendors" to act as "weak" but only for vendors;
Chris@0 33 * - use "/some-regexp/" to stop the test suite whenever a deprecation
Chris@0 34 * message matches the given regular expression;
Chris@0 35 * - use a number to define the upper bound of allowed deprecations,
Chris@17 36 * making the test suite fail whenever more notices are triggered.
Chris@0 37 *
Chris@0 38 * @param int|string|false $mode The reporting mode, defaults to not allowing any deprecations
Chris@0 39 */
Chris@0 40 public static function register($mode = 0)
Chris@0 41 {
Chris@0 42 if (self::$isRegistered) {
Chris@0 43 return;
Chris@0 44 }
Chris@0 45
Chris@14 46 $UtilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
Chris@14 47
Chris@0 48 $getMode = function () use ($mode) {
Chris@0 49 static $memoizedMode = false;
Chris@0 50
Chris@0 51 if (false !== $memoizedMode) {
Chris@0 52 return $memoizedMode;
Chris@0 53 }
Chris@0 54 if (false === $mode) {
Chris@0 55 $mode = getenv('SYMFONY_DEPRECATIONS_HELPER');
Chris@0 56 }
Chris@17 57 if (self::MODE_DISABLED !== $mode
Chris@17 58 && self::MODE_WEAK !== $mode
Chris@17 59 && self::MODE_WEAK_VENDORS !== $mode
Chris@17 60 && (!isset($mode[0]) || '/' !== $mode[0])
Chris@17 61 ) {
Chris@0 62 $mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
Chris@0 63 }
Chris@0 64
Chris@0 65 return $memoizedMode = $mode;
Chris@0 66 };
Chris@0 67
Chris@14 68 $inVendors = function ($path) {
Chris@14 69 /** @var string[] absolute paths to vendor directories */
Chris@14 70 static $vendors;
Chris@14 71 if (null === $vendors) {
Chris@14 72 foreach (get_declared_classes() as $class) {
Chris@14 73 if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
Chris@14 74 $r = new \ReflectionClass($class);
Chris@17 75 $v = \dirname(\dirname($r->getFileName()));
Chris@14 76 if (file_exists($v.'/composer/installed.json')) {
Chris@14 77 $vendors[] = $v;
Chris@14 78 }
Chris@14 79 }
Chris@14 80 }
Chris@14 81 }
Chris@14 82 $realPath = realpath($path);
Chris@14 83 if (false === $realPath && '-' !== $path && 'Standard input code' !== $path) {
Chris@14 84 return true;
Chris@14 85 }
Chris@14 86 foreach ($vendors as $vendor) {
Chris@17 87 if (0 === strpos($realPath, $vendor) && false !== strpbrk(substr($realPath, \strlen($vendor), 1), '/'.\DIRECTORY_SEPARATOR)) {
Chris@14 88 return true;
Chris@14 89 }
Chris@14 90 }
Chris@14 91
Chris@14 92 return false;
Chris@14 93 };
Chris@14 94
Chris@0 95 $deprecations = array(
Chris@0 96 'unsilencedCount' => 0,
Chris@0 97 'remainingCount' => 0,
Chris@0 98 'legacyCount' => 0,
Chris@0 99 'otherCount' => 0,
Chris@14 100 'remaining vendorCount' => 0,
Chris@0 101 'unsilenced' => array(),
Chris@0 102 'remaining' => array(),
Chris@0 103 'legacy' => array(),
Chris@0 104 'other' => array(),
Chris@14 105 'remaining vendor' => array(),
Chris@0 106 );
Chris@14 107 $deprecationHandler = function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, $getMode, $UtilPrefix, $inVendors) {
Chris@0 108 $mode = $getMode();
Chris@17 109 if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || self::MODE_DISABLED === $mode) {
Chris@14 110 $ErrorHandler = $UtilPrefix.'ErrorHandler';
Chris@14 111
Chris@14 112 return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
Chris@0 113 }
Chris@0 114
Chris@17 115 $trace = debug_backtrace();
Chris@0 116 $group = 'other';
Chris@17 117 $isVendor = self::MODE_WEAK_VENDORS === $mode && $inVendors($file);
Chris@0 118
Chris@17 119 $i = \count($trace);
Chris@14 120 while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) {
Chris@0 121 // No-op
Chris@0 122 }
Chris@0 123
Chris@0 124 if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
Chris@14 125 if (isset($trace[$i]['class']) && 0 === strpos($trace[$i]['class'], 'Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor')) {
Chris@14 126 $parsedMsg = unserialize($msg);
Chris@14 127 $msg = $parsedMsg['deprecation'];
Chris@14 128 $class = $parsedMsg['class'];
Chris@14 129 $method = $parsedMsg['method'];
Chris@14 130 // If the deprecation has been triggered via
Chris@14 131 // \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest()
Chris@14 132 // then we need to use the serialized information to determine
Chris@14 133 // if the error has been triggered from vendor code.
Chris@17 134 $isVendor = self::MODE_WEAK_VENDORS === $mode && isset($parsedMsg['triggering_file']) && $inVendors($parsedMsg['triggering_file']);
Chris@14 135 } else {
Chris@17 136 $class = isset($trace[$i]['object']) ? \get_class($trace[$i]['object']) : $trace[$i]['class'];
Chris@14 137 $method = $trace[$i]['function'];
Chris@14 138 }
Chris@14 139
Chris@14 140 $Test = $UtilPrefix.'Test';
Chris@0 141
Chris@0 142 if (0 !== error_reporting()) {
Chris@0 143 $group = 'unsilenced';
Chris@0 144 } elseif (0 === strpos($method, 'testLegacy')
Chris@0 145 || 0 === strpos($method, 'provideLegacy')
Chris@0 146 || 0 === strpos($method, 'getLegacy')
Chris@0 147 || strpos($class, '\Legacy')
Chris@17 148 || \in_array('legacy', $Test::getGroups($class, $method), true)
Chris@0 149 ) {
Chris@0 150 $group = 'legacy';
Chris@14 151 } elseif ($isVendor) {
Chris@14 152 $group = 'remaining vendor';
Chris@0 153 } else {
Chris@0 154 $group = 'remaining';
Chris@0 155 }
Chris@0 156
Chris@0 157 if (isset($mode[0]) && '/' === $mode[0] && preg_match($mode, $msg)) {
Chris@0 158 $e = new \Exception($msg);
Chris@0 159 $r = new \ReflectionProperty($e, 'trace');
Chris@0 160 $r->setAccessible(true);
Chris@17 161 $r->setValue($e, \array_slice($trace, 1, $i));
Chris@0 162
Chris@0 163 echo "\n".ucfirst($group).' deprecation triggered by '.$class.'::'.$method.':';
Chris@0 164 echo "\n".$msg;
Chris@0 165 echo "\nStack trace:";
Chris@17 166 echo "\n".str_replace(' '.getcwd().\DIRECTORY_SEPARATOR, ' ', $e->getTraceAsString());
Chris@0 167 echo "\n";
Chris@0 168
Chris@0 169 exit(1);
Chris@0 170 }
Chris@17 171 if ('legacy' !== $group && self::MODE_WEAK !== $mode) {
Chris@0 172 $ref = &$deprecations[$group][$msg]['count'];
Chris@0 173 ++$ref;
Chris@0 174 $ref = &$deprecations[$group][$msg][$class.'::'.$method];
Chris@0 175 ++$ref;
Chris@0 176 }
Chris@17 177 } elseif (self::MODE_WEAK !== $mode) {
Chris@0 178 $ref = &$deprecations[$group][$msg]['count'];
Chris@0 179 ++$ref;
Chris@0 180 }
Chris@0 181 ++$deprecations[$group.'Count'];
Chris@0 182 };
Chris@0 183 $oldErrorHandler = set_error_handler($deprecationHandler);
Chris@0 184
Chris@0 185 if (null !== $oldErrorHandler) {
Chris@0 186 restore_error_handler();
Chris@14 187 if (array($UtilPrefix.'ErrorHandler', 'handleError') === $oldErrorHandler) {
Chris@0 188 restore_error_handler();
Chris@0 189 self::register($mode);
Chris@0 190 }
Chris@0 191 } else {
Chris@0 192 self::$isRegistered = true;
Chris@0 193 if (self::hasColorSupport()) {
Chris@0 194 $colorize = function ($str, $red) {
Chris@0 195 $color = $red ? '41;37' : '43;30';
Chris@0 196
Chris@0 197 return "\x1B[{$color}m{$str}\x1B[0m";
Chris@0 198 };
Chris@0 199 } else {
Chris@0 200 $colorize = function ($str) { return $str; };
Chris@0 201 }
Chris@0 202 register_shutdown_function(function () use ($getMode, &$deprecations, $deprecationHandler, $colorize) {
Chris@0 203 $mode = $getMode();
Chris@0 204 if (isset($mode[0]) && '/' === $mode[0]) {
Chris@0 205 return;
Chris@0 206 }
Chris@0 207 $currErrorHandler = set_error_handler('var_dump');
Chris@0 208 restore_error_handler();
Chris@0 209
Chris@17 210 if (self::MODE_WEAK === $mode) {
Chris@0 211 $colorize = function ($str) { return $str; };
Chris@0 212 }
Chris@0 213 if ($currErrorHandler !== $deprecationHandler) {
Chris@0 214 echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
Chris@0 215 }
Chris@0 216
Chris@0 217 $cmp = function ($a, $b) {
Chris@0 218 return $b['count'] - $a['count'];
Chris@0 219 };
Chris@0 220
Chris@14 221 $groups = array('unsilenced', 'remaining');
Chris@17 222 if (self::MODE_WEAK_VENDORS === $mode) {
Chris@14 223 $groups[] = 'remaining vendor';
Chris@14 224 }
Chris@14 225 array_push($groups, 'legacy', 'other');
Chris@0 226
Chris@14 227 $displayDeprecations = function ($deprecations) use ($colorize, $cmp, $groups) {
Chris@14 228 foreach ($groups as $group) {
Chris@14 229 if ($deprecations[$group.'Count']) {
Chris@14 230 echo "\n", $colorize(
Chris@14 231 sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']),
Chris@14 232 'legacy' !== $group && 'remaining vendor' !== $group
Chris@14 233 ), "\n";
Chris@0 234
Chris@14 235 uasort($deprecations[$group], $cmp);
Chris@0 236
Chris@14 237 foreach ($deprecations[$group] as $msg => $notices) {
Chris@14 238 echo "\n ", $notices['count'], 'x: ', $msg, "\n";
Chris@0 239
Chris@14 240 arsort($notices);
Chris@14 241
Chris@14 242 foreach ($notices as $method => $count) {
Chris@14 243 if ('count' !== $method) {
Chris@14 244 echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
Chris@14 245 }
Chris@0 246 }
Chris@0 247 }
Chris@0 248 }
Chris@0 249 }
Chris@14 250 if (!empty($notices)) {
Chris@14 251 echo "\n";
Chris@14 252 }
Chris@14 253 };
Chris@14 254
Chris@14 255 $displayDeprecations($deprecations);
Chris@14 256
Chris@14 257 // store failing status
Chris@17 258 $isFailing = self::MODE_WEAK !== $mode && $mode < $deprecations['unsilencedCount'] + $deprecations['remainingCount'] + $deprecations['otherCount'];
Chris@14 259
Chris@14 260 // reset deprecations array
Chris@14 261 foreach ($deprecations as $group => $arrayOrInt) {
Chris@17 262 $deprecations[$group] = \is_int($arrayOrInt) ? 0 : array();
Chris@0 263 }
Chris@0 264
Chris@14 265 register_shutdown_function(function () use (&$deprecations, $isFailing, $displayDeprecations, $mode) {
Chris@14 266 foreach ($deprecations as $group => $arrayOrInt) {
Chris@17 267 if (0 < (\is_int($arrayOrInt) ? $arrayOrInt : \count($arrayOrInt))) {
Chris@14 268 echo "Shutdown-time deprecations:\n";
Chris@14 269 break;
Chris@14 270 }
Chris@14 271 }
Chris@14 272 $displayDeprecations($deprecations);
Chris@17 273 if ($isFailing || self::MODE_WEAK !== $mode && $mode < $deprecations['unsilencedCount'] + $deprecations['remainingCount'] + $deprecations['otherCount']) {
Chris@14 274 exit(1);
Chris@14 275 }
Chris@14 276 });
Chris@0 277 });
Chris@0 278 }
Chris@0 279 }
Chris@0 280
Chris@14 281 public static function collectDeprecations($outputFile)
Chris@14 282 {
Chris@14 283 $deprecations = array();
Chris@14 284 $previousErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, &$previousErrorHandler) {
Chris@14 285 if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
Chris@14 286 if ($previousErrorHandler) {
Chris@14 287 return $previousErrorHandler($type, $msg, $file, $line, $context);
Chris@14 288 }
Chris@14 289 static $autoload = true;
Chris@14 290
Chris@14 291 $ErrorHandler = class_exists('PHPUnit_Util_ErrorHandler', $autoload) ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler';
Chris@14 292 $autoload = false;
Chris@14 293
Chris@14 294 return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
Chris@14 295 }
Chris@14 296 $deprecations[] = array(error_reporting(), $msg, $file);
Chris@14 297 });
Chris@14 298
Chris@14 299 register_shutdown_function(function () use ($outputFile, &$deprecations) {
Chris@14 300 file_put_contents($outputFile, serialize($deprecations));
Chris@14 301 });
Chris@14 302 }
Chris@14 303
Chris@16 304 /**
Chris@16 305 * Returns true if STDOUT is defined and supports colorization.
Chris@16 306 *
Chris@16 307 * Reference: Composer\XdebugHandler\Process::supportsColor
Chris@16 308 * https://github.com/composer/xdebug-handler
Chris@16 309 *
Chris@16 310 * @return bool
Chris@16 311 */
Chris@0 312 private static function hasColorSupport()
Chris@0 313 {
Chris@17 314 if (!\defined('STDOUT')) {
Chris@16 315 return false;
Chris@16 316 }
Chris@16 317
Chris@17 318 if ('Hyper' === getenv('TERM_PROGRAM')) {
Chris@17 319 return true;
Chris@17 320 }
Chris@17 321
Chris@17 322 if (\DIRECTORY_SEPARATOR === '\\') {
Chris@17 323 return (\function_exists('sapi_windows_vt100_support')
Chris@16 324 && sapi_windows_vt100_support(STDOUT))
Chris@0 325 || false !== getenv('ANSICON')
Chris@0 326 || 'ON' === getenv('ConEmuANSI')
Chris@0 327 || 'xterm' === getenv('TERM');
Chris@0 328 }
Chris@0 329
Chris@17 330 if (\function_exists('stream_isatty')) {
Chris@16 331 return stream_isatty(STDOUT);
Chris@16 332 }
Chris@16 333
Chris@17 334 if (\function_exists('posix_isatty')) {
Chris@16 335 return posix_isatty(STDOUT);
Chris@16 336 }
Chris@16 337
Chris@16 338 $stat = fstat(STDOUT);
Chris@16 339 // Check if formatted mode is S_IFCHR
Chris@16 340 return $stat ? 0020000 === ($stat['mode'] & 0170000) : false;
Chris@0 341 }
Chris@0 342 }