Skip to content

Commit 869bc81

Browse files
committed
Copied deprecation error handler from core
1 parent cfc0762 commit 869bc81

File tree

2 files changed

+128
-1
lines changed

2 files changed

+128
-1
lines changed

bootstrap/bootstrap.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@
1111
'php composer.phar install'.$nl);
1212
}
1313

14-
1514
use Doctrine\Common\Annotations\AnnotationRegistry;
15+
use Symfony\Cmf\Component\Testing\ErrorHandler\DeprecationErrorHandler;
16+
17+
if (class_exists('PHPUnit_Util_ErrorHandler')) {
18+
DeprecationErrorHandler::register();
19+
}
20+
1621
AnnotationRegistry::registerLoader(function($class) use ($loader) {
1722
$loader->loadClass($class);
1823

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

0 commit comments

Comments
 (0)