PageRenderTime 62ms CodeModel.GetById 14ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/symfony/symfony/src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

https://gitlab.com/matijabelec/bigpandadev
PHP | 141 lines | 108 code | 19 blank | 14 comment | 28 complexity | 73bb610528922da1096a6500fcfccedb MD5 | raw file
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bridge\PhpUnit;
  11. /**
  12. * Catch deprecation notices and print a summary report at the end of the test suite.
  13. *
  14. * @author Nicolas Grekas <p@tchwork.com>
  15. */
  16. class DeprecationErrorHandler
  17. {
  18. private static $isRegistered = false;
  19. public static function register($mode = false)
  20. {
  21. if (self::$isRegistered) {
  22. return;
  23. }
  24. $deprecations = array(
  25. 'unsilencedCount' => 0,
  26. 'remainingCount' => 0,
  27. 'legacyCount' => 0,
  28. 'otherCount' => 0,
  29. 'unsilenced' => array(),
  30. 'remaining' => array(),
  31. 'legacy' => array(),
  32. 'other' => array(),
  33. );
  34. $deprecationHandler = function ($type, $msg, $file, $line, $context) use (&$deprecations, $mode) {
  35. if (E_USER_DEPRECATED !== $type) {
  36. return \PHPUnit_Util_ErrorHandler::handleError($type, $msg, $file, $line, $context);
  37. }
  38. $trace = debug_backtrace(PHP_VERSION_ID >= 50400 ? DEBUG_BACKTRACE_IGNORE_ARGS | DEBUG_BACKTRACE_PROVIDE_OBJECT : true);
  39. $i = count($trace);
  40. while (isset($trace[--$i]['class']) && ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_'))) {
  41. // No-op
  42. }
  43. if (0 !== error_reporting()) {
  44. $group = 'unsilenced';
  45. $ref = &$deprecations[$group][$msg]['count'];
  46. ++$ref;
  47. } elseif (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
  48. $class = isset($trace[$i]['object']) ? get_class($trace[$i]['object']) : $trace[$i]['class'];
  49. $method = $trace[$i]['function'];
  50. $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';
  51. if ('legacy' !== $group && 'weak' !== $mode) {
  52. $ref = &$deprecations[$group][$msg]['count'];
  53. ++$ref;
  54. $ref = &$deprecations[$group][$msg][$class.'::'.$method];
  55. ++$ref;
  56. }
  57. } else {
  58. $group = 'other';
  59. $ref = &$deprecations[$group][$msg]['count'];
  60. ++$ref;
  61. }
  62. ++$deprecations[$group.'Count'];
  63. };
  64. $oldErrorHandler = set_error_handler($deprecationHandler);
  65. if (null !== $oldErrorHandler) {
  66. restore_error_handler();
  67. if (array('PHPUnit_Util_ErrorHandler', 'handleError') === $oldErrorHandler) {
  68. restore_error_handler();
  69. self::register($mode);
  70. }
  71. } else {
  72. self::$isRegistered = true;
  73. if (self::hasColorSupport()) {
  74. $colorize = function ($str, $red) {
  75. $color = $red ? '41;37' : '43;30';
  76. return "\x1B[{$color}m{$str}\x1B[0m";
  77. };
  78. } else {
  79. $colorize = function ($str) {return $str;};
  80. }
  81. register_shutdown_function(function () use ($mode, &$deprecations, $deprecationHandler, $colorize) {
  82. $currErrorHandler = set_error_handler('var_dump');
  83. restore_error_handler();
  84. if ($currErrorHandler !== $deprecationHandler) {
  85. echo "\n", $colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
  86. }
  87. $cmp = function ($a, $b) {
  88. return $b['count'] - $a['count'];
  89. };
  90. foreach (array('unsilenced', 'remaining', 'legacy', 'other') as $group) {
  91. if ($deprecations[$group.'Count']) {
  92. echo "\n", $colorize(sprintf('%s deprecation notices (%d)', ucfirst($group), $deprecations[$group.'Count']), 'legacy' !== $group), "\n";
  93. uasort($deprecations[$group], $cmp);
  94. foreach ($deprecations[$group] as $msg => $notices) {
  95. echo "\n", rtrim($msg, '.'), ': ', $notices['count'], "x\n";
  96. arsort($notices);
  97. foreach ($notices as $method => $count) {
  98. if ('count' !== $method) {
  99. echo ' ', $count, 'x in ', preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method), "\n";
  100. }
  101. }
  102. }
  103. }
  104. }
  105. if (!empty($notices)) {
  106. echo "\n";
  107. }
  108. if ('weak' !== $mode && ($deprecations['unsilenced'] || $deprecations['remaining'] || $deprecations['other'])) {
  109. exit(1);
  110. }
  111. });
  112. }
  113. }
  114. private static function hasColorSupport()
  115. {
  116. if ('\\' === DIRECTORY_SEPARATOR) {
  117. return false !== getenv('ANSICON') || 'ON' === getenv('ConEmuANSI');
  118. }
  119. return defined('STDOUT') && function_exists('posix_isatty') && @posix_isatty(STDOUT);
  120. }
  121. }