diff vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php @ 14:1fec387a4317

Update Drupal core to 8.5.2 via Composer
author Chris Cannam
date Mon, 23 Apr 2018 09:46:53 +0100
parents 7a779792577d
children c2387f117808
line wrap: on
line diff
--- a/vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php	Mon Apr 23 09:33:26 2018 +0100
+++ b/vendor/symfony/phpunit-bridge/DeprecationErrorHandler.php	Mon Apr 23 09:46:53 2018 +0100
@@ -19,6 +19,7 @@
 class DeprecationErrorHandler
 {
     const MODE_WEAK = 'weak';
+    const MODE_WEAK_VENDORS = 'weak_vendors';
     const MODE_DISABLED = 'disabled';
 
     private static $isRegistered = false;
@@ -28,6 +29,7 @@
      *
      * The following reporting modes are supported:
      * - use "weak" to hide the deprecation report but keep a global count;
+     * - use "weak_vendors" to act as "weak" but only for vendors;
      * - use "/some-regexp/" to stop the test suite whenever a deprecation
      *   message matches the given regular expression;
      * - use a number to define the upper bound of allowed deprecations,
@@ -41,6 +43,8 @@
             return;
         }
 
+        $UtilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
+
         $getMode = function () use ($mode) {
             static $memoizedMode = false;
 
@@ -50,40 +54,86 @@
             if (false === $mode) {
                 $mode = getenv('SYMFONY_DEPRECATIONS_HELPER');
             }
-            if (DeprecationErrorHandler::MODE_WEAK !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
+            if (DeprecationErrorHandler::MODE_WEAK !== $mode && DeprecationErrorHandler::MODE_WEAK_VENDORS !== $mode && (!isset($mode[0]) || '/' !== $mode[0])) {
                 $mode = preg_match('/^[1-9][0-9]*$/', $mode) ? (int) $mode : 0;
             }
 
             return $memoizedMode = $mode;
         };
 
+        $inVendors = function ($path) {
+            /** @var string[] absolute paths to vendor directories */
+            static $vendors;
+            if (null === $vendors) {
+                foreach (get_declared_classes() as $class) {
+                    if ('C' === $class[0] && 0 === strpos($class, 'ComposerAutoloaderInit')) {
+                        $r = new \ReflectionClass($class);
+                        $v = dirname(dirname($r->getFileName()));
+                        if (file_exists($v.'/composer/installed.json')) {
+                            $vendors[] = $v;
+                        }
+                    }
+                }
+            }
+            $realPath = realpath($path);
+            if (false === $realPath && '-' !== $path && 'Standard input code' !== $path) {
+                return true;
+            }
+            foreach ($vendors as $vendor) {
+                if (0 === strpos($realPath, $vendor) && false !== strpbrk(substr($realPath, strlen($vendor), 1), '/'.DIRECTORY_SEPARATOR)) {
+                    return true;
+                }
+            }
+
+            return false;
+        };
+
         $deprecations = array(
             'unsilencedCount' => 0,
             'remainingCount' => 0,
             'legacyCount' => 0,
             'otherCount' => 0,
+            'remaining vendorCount' => 0,
             'unsilenced' => array(),
             'remaining' => array(),
             'legacy' => array(),
             'other' => array(),
+            'remaining vendor' => array(),
         );
-        $deprecationHandler = function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, $getMode) {
+        $deprecationHandler = function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, $getMode, $UtilPrefix, $inVendors) {
             $mode = $getMode();
             if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || DeprecationErrorHandler::MODE_DISABLED === $mode) {
-                return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
+                $ErrorHandler = $UtilPrefix.'ErrorHandler';
+
+                return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
             }
 
             $trace = debug_backtrace(true);
             $group = 'other';
+            $isVendor = DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode && $inVendors($file);
 
             $i = count($trace);
-            while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_')))) {
+            while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) {
                 // No-op
             }
 
             if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
-                $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
-                $method = $trace[$i]['function'];
+                if (isset($trace[$i]['class']) && 0 === strpos($trace[$i]['class'], 'Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor')) {
+                    $parsedMsg = unserialize($msg);
+                    $msg = $parsedMsg['deprecation'];
+                    $class = $parsedMsg['class'];
+                    $method = $parsedMsg['method'];
+                    // If the deprecation has been triggered via
+                    // \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest()
+                    // then we need to use the serialized information to determine
+                    // if the error has been triggered from vendor code.
+                    $isVendor = DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode && isset($parsedMsg['triggering_file']) && $inVendors($parsedMsg['triggering_file']);
+                } else {
+                    $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
+                    $method = $trace[$i]['function'];
+                }
+
+                $Test = $UtilPrefix.'Test';
 
                 if (0 !== error_reporting()) {
                     $group = 'unsilenced';
@@ -91,9 +141,11 @@
                     || 0 === strpos($method, 'provideLegacy')
                     || 0 === strpos($method, 'getLegacy')
                     || strpos($class, '\Legacy')
-                    || in_array('legacy', \PHPUnit_Util_Test::getGroups($class, $method), true)
+                    || in_array('legacy', $Test::getGroups($class, $method), true)
                 ) {
                     $group = 'legacy';
+                } elseif ($isVendor) {
+                    $group = 'remaining vendor';
                 } else {
                     $group = 'remaining';
                 }
@@ -128,7 +180,7 @@
 
         if (null !== $oldErrorHandler) {
             restore_error_handler();
-            if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) {
+            if (array($UtilPrefix.'ErrorHandler', 'handleError') === $oldErrorHandler) {
                 restore_error_handler();
                 self::register($mode);
             }
@@ -162,41 +214,95 @@
                     return $b['count'] - $a['count'];
                 };
 
-                foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
-                    if ($deprecations[$group.'Count']) {
-                        echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";
+                $groups = array('unsilenced', 'remaining');
+                if (DeprecationErrorHandler::MODE_WEAK_VENDORS === $mode) {
+                    $groups[] = 'remaining vendor';
+                }
+                array_push($groups, 'legacy', 'other');
 
-                        uasort($deprecations[$group], $cmp);
+                $displayDeprecations = function ($deprecations) use ($colorize, $cmp, $groups) {
+                    foreach ($groups as $group) {
+                        if ($deprecations[$group.'Count']) {
+                            echo "\n", $colorize(
+                                sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']),
+                                'legacy' !== $group && 'remaining vendor' !== $group
+                            ), "\n";
 
-                        foreach ($deprecations[$group] as $msg => $notices) {
-                            echo "\n", rtrim($msg, '.'), ': ', $notices['count'], "x\n";
+                            uasort($deprecations[$group], $cmp);
 
-                            arsort($notices);
+                            foreach ($deprecations[$group] as $msg => $notices) {
+                                echo "\n  ", $notices['count'], 'x: ', $msg, "\n";
 
-                            foreach ($notices as $method => $count) {
-                                if ('count' !== $method) {
-                                    echo '    ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
+                                arsort($notices);
+
+                                foreach ($notices as $method => $count) {
+                                    if ('count' !== $method) {
+                                        echo '    ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
+                                    }
                                 }
                             }
                         }
                     }
-                }
-                if (!empty($notices)) {
-                    echo "\n";
+                    if (!empty($notices)) {
+                        echo "\n";
+                    }
+                };
+
+                $displayDeprecations($deprecations);
+
+                // store failing status
+                $isFailing = DeprecationErrorHandler::MODE_WEAK !== $mode && $mode < $deprecations['unsilencedCount'] + $deprecations['remainingCount'] + $deprecations['otherCount'];
+
+                // reset deprecations array
+                foreach ($deprecations as $group => $arrayOrInt) {
+                    $deprecations[$group] = is_int($arrayOrInt) ? 0 : array();
                 }
 
-                if (DeprecationErrorHandler::MODE_WEAK !== $mode && $mode < $deprecations['unsilencedCount'] + $deprecations['remainingCount'] + $deprecations['otherCount']) {
-                    exit(1);
-                }
+                register_shutdown_function(function () use (&$deprecations, $isFailing, $displayDeprecations, $mode) {
+                    foreach ($deprecations as $group => $arrayOrInt) {
+                        if (0 < (is_int($arrayOrInt) ? $arrayOrInt : count($arrayOrInt))) {
+                            echo "Shutdown-time deprecations:\n";
+                            break;
+                        }
+                    }
+                    $displayDeprecations($deprecations);
+                    if ($isFailing || DeprecationErrorHandler::MODE_WEAK !== $mode && $mode < $deprecations['unsilencedCount'] + $deprecations['remainingCount'] + $deprecations['otherCount']) {
+                        exit(1);
+                    }
+                });
             });
         }
     }
 
+    public static function collectDeprecations($outputFile)
+    {
+        $deprecations = array();
+        $previousErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = array()) use (&$deprecations, &$previousErrorHandler) {
+            if (E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) {
+                if ($previousErrorHandler) {
+                    return $previousErrorHandler($type, $msg, $file, $line, $context);
+                }
+                static $autoload = true;
+
+                $ErrorHandler = class_exists('PHPUnit_Util_ErrorHandler', $autoload) ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler';
+                $autoload = false;
+
+                return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
+            }
+            $deprecations[] = array(error_reporting(), $msg, $file);
+        });
+
+        register_shutdown_function(function () use ($outputFile, &$deprecations) {
+            file_put_contents($outputFile, serialize($deprecations));
+        });
+    }
+
     private static function hasColorSupport()
     {
         if ('\\' === DIRECTORY_SEPARATOR) {
             return
-                '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
+                defined('STDOUT') && function_exists('sapi_windows_vt100_support') && sapi_windows_vt100_support(STDOUT)
+                || '10.0.10586' === PHP_WINDOWS_VERSION_MAJOR.'.'.PHP_WINDOWS_VERSION_MINOR.'.'.PHP_WINDOWS_VERSION_BUILD
                 || false !== getenv('ANSICON')
                 || 'ON' === getenv('ConEmuANSI')
                 || 'xterm' === getenv('TERM');