8000 Extract deprecation handler to a private static function · symfony/symfony@79921b3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 79921b3

Browse files
committed
Extract deprecation handler to a private static function
1 parent 4e6d0d1 commit 79921b3

File tree

1 file changed

+85
-80
lines changed

1 file changed

+85
-80
lines changed

src/Symfony/Bridge/PhpUnit/DeprecationErrorHandler.php

Lines changed: 85 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -59,83 +59,7 @@ public static function register($mode = 0)
5959

6060
self::computeMode($mode);
6161

62-
$deprecationHandler = function ($type, $msg, $file, $line, $context = array()) {
63-
$mode = self::$mode;
64-
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || self::MODE_DISABLED === $mode) {
65-
$ErrorHandler = self::utilPrefix().'ErrorHandler';
66-
67-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
68-
}
69-
70-
$trace = debug_backtrace();
71-
$group = 'other';
72-
$isVendor = self::MODE_WEAK_VENDORS === $mode && self::inVendors($file);
73-
74-
$i = \count($trace);
75-
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) {
76-
// No-op
77-
}
78-
79-
if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
80-
if (isset($trace[$i]['class']) && 0 === strpos($trace[$i]['class'], 'Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor')) {
81-
$parsedMsg = unserialize($msg);
82-
$msg = $parsedMsg['deprecation'];
83-
$class = $parsedMsg['class'];
84-
$method = $parsedMsg['method'];
85-
// If the deprecation has been triggered via
86-
// \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest()
87-
// then we need to use the serialized information to determine
88-
// if the error has been triggered from vendor code.
89-
$isVendor = self::MODE_WEAK_VENDORS === $mode && isset($parsedMsg['triggering_file']) && self::inVendors($parsedMsg['triggering_file']);
90-
} else {
91-
$class = isset($trace[$i]['object']) ? \get_class($trace[$i]['object']) : $trace[$i]['class'];
92-
$method = $trace[$i]['function'];
93-
}
94-
95-
$Test = self::utilPrefix().'Test';
96-
97-
if (0 !== error_reporting()) {
98-
$group = 'unsilenced';
99-
} elseif (0 === strpos($method, 'testLegacy')
100-
|| 0 === strpos($method, 'provideLegacy')
101-
|| 0 === strpos($method, 'getLegacy')
102-
|| strpos($class, '\Legacy')
103-
|| \in_array('legacy', $Test::getGroups($class, $method), true)
104-
) {
105-
$group = 'legacy';
106-
} elseif ($isVendor) {
107-
$group = 'remaining vendor';
108-
} else {
109-
$group = 'remaining';
110-
}
111-
112-
if (isset($mode[0]) && '/' === $mode[0] && preg_match($mode, $msg)) {
113-
$e = new \Exception($msg);
114-
$r = new \ReflectionProperty($e, 'trace');
115-
$r->setAccessible(true);
116-
$r->setValue($e, \array_slice($trace, 1, $i));
117-
118-
echo "\n".ucfirst($group).' deprecation triggered by '.$class.'::'.$method.':';
119-
echo "\n".$msg;
120-
echo "\nStack trace:";
121-
echo "\n".str_replace(' '.getcwd().\DIRECTORY_SEPARATOR, ' ', $e->getTraceAsString());
122-
echo "\n";
123-
124-
exit(1);
125-
}
126-
if ('legacy' !== $group && self::MODE_WEAK !== $mode) {
127-
$ref = &self::$deprecations[$group][$msg]['count'];
128-
++$ref;
129-
$ref = &self::$deprecations[$group][$msg][$class.'::'.$method];
130-
++$ref;
131-
}
132-
} elseif (self::MODE_WEAK !== $mode) {
133-
$ref = &self::$deprecations[$group][$msg]['count'];
134-
++$ref;
135-
}
136-
++self::$deprecations[$group.'Count'];
137-
};
138-
$oldErrorHandler = set_error_handler($deprecationHandler);
62+
$oldErrorHandler = set_error_handler(array(self::class, 'handleError'));
13963

14064
if (null !== $oldErrorHandler) {
14165
restore_error_handler();
@@ -145,15 +69,15 @@ public static function register($mode = 0)
14569
}
14670
} else {
14771
self::$isRegistered = true;
148-
register_shutdown_function(function () use ($deprecationHandler) {
72+
register_shutdown_function(function () {
14973
$mode = self::$mode;
15074
if (isset($mode[0]) && '/' === $mode[0]) {
15175
return;
15276
}
15377
$currErrorHandler = set_error_handler('var_dump');
15478
restore_error_handler();
15579

156-
if ($currErrorHandler !== $deprecationHandler) {
80+
if ($currErrorHandler !== array(self::class, 'handleError')) {
15781
echo "\n", self::colorize('THE ERROR HANDLER HAS CHANGED!', true, $mode), "\n";
15882
}
15983

@@ -306,6 +230,87 @@ private static function inVendors($path)
306230
return false;
307231
}
308232

233+
/**
234+
* @internal
235+
*/
236+
public static function handleError($type, $msg, $file, $line, $context = array())
237+
{
238+
$mode = self::$mode;
239+
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || self::MODE_DISABLED === $mode) {
240+
$ErrorHandler = self::utilPrefix().'ErrorHandler';
241+
242+
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
243+
}
244+
245+
$trace = debug_backtrace();
246+
$group = 'other';
247+
$isVendor = self::MODE_WEAK_VENDORS === $mode && self::inVendors($file);
248+
249+
$i = \count($trace);
250+
while (1 < $i && (!isset($trace[--$i]['class']) || ('ReflectionMethod' === $trace[$i]['class'] || 0 === strpos($trace[$i]['class'], 'PHPUnit_') || 0 === strpos($trace[$i]['class'], 'PHPUnit\\')))) {
251+
// No-op
252+
}
253+
254+
if (isset($trace[$i]['object']) || isset($trace[$i]['class'])) {
255+
if (isset($trace[$i]['class']) && 0 === strpos($trace[$i]['class'], 'Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerFor')) {
256+
$parsedMsg = unserialize($msg);
257+
$msg = $parsedMsg['deprecation'];
258+
$class = $parsedMsg['class'];
259+
$method = $parsedMsg['method'];
260+
// If the deprecation has been triggered via
261+
// \Symfony\Bridge\PhpUnit\Legacy\SymfonyTestsListenerTrait::endTest()
262+
// then we need to use the serialized information to determine
263+
// if the error has been triggered from vendor code.
264+
$isVendor = self::MODE_WEAK_VENDORS === $mode && isset($parsedMsg['triggering_file']) && self::inVendors($parsedMsg['triggering_file']);
265+
} else {
266+
$class = isset($trace[$i]['object']) ? \get_class($trace[$i]['object']) : $trace[$i]['class'];
267+
$method = $trace[$i]['function'];
268+
}
269+
270+
$Test = self::utilPrefix().'Test';
271+
272+
if (0 !== error_reporting()) {
273+
$group = 'unsilenced';
274+
} elseif (0 === strpos($method, 'testLegacy')
275+
|| 0 === strpos($method, 'provideLegacy')
276+
|| 0 === strpos($method, 'getLegacy')
277+
|| strpos($class, '\Legacy')
278+
|| \in_array('legacy', $Test::getGroups($class, $method), true)
279+
) {
280+
$group = 'legacy';
281+
} elseif ($isVendor) {
282+
$group = 'remaining vendor';
283+
} else {
284+
$group = 'remaining';
285+
}
286+
287+
if (isset($mode[0]) && '/' === $mode[0] && preg_match($mode, $msg)) {
288+
$e = new \Exception($msg);
289+
$r = new \ReflectionProperty($e, 'trace');
290+
$r->setAccessible(true);
291+
$r->setValue($e, \array_slice($trace, 1, $i));
292+
293+
echo "\n".ucfirst($group).' deprecation triggered by '.$class.'::'.$method.':';
294+
echo "\n".$msg;
295+
echo "\nStack trace:";
296+
echo "\n".str_replace(' '.getcwd().\DIRECTORY_SEPARATOR, ' ', $e->getTraceAsString());
297+
echo "\n";
298+
299+
exit(1);
300+
}
301+
if ('legacy' !== $group && self::MODE_WEAK !== $mode) {
302+
$ref = &self::$deprecations[$group][$msg]['count'];
303+
++$ref;
304+
$ref = &self::$deprecations[$group][$msg][$class.'::'.$method];
305+
++$ref;
306+
}
307+
} elseif (self::MODE_WEAK !== $mode) {
308+
$ref = &self::$deprecations[$group][$msg]['count'];
309+
++$ref;
310+
}
311+
++self::$deprecations[$group.'Count'];
312+
}
313+
309314
/**
310315
* @param string $str
311316
* @param bool $red
@@ -342,7 +347,7 @@ private static function displayDeprecations($groups, $mode)
342347
$mode
343348
), "\n";
344349

345-
uasort(self::$deprecations[$group], array(self::class, $cmp));
350+
uasort(self::$deprecations[$group], $cmp);
346351

347352
foreach (self::$deprecations[$group] as $msg => $notices) {
348353
echo "\n ", $notices['count'], 'x: ', $msg, "\n";

0 commit comments

Comments
 (0)
0