1111
1212namespace Symfony \Bridge \PhpUnit ;
1313
14+ use PHPUnit \Framework \TestResult ;
15+ use PHPUnit \Util \ErrorHandler ;
16+
1417/**
1518 * Catch deprecation notices and print a summary report at the end of the test suite.
1619 *
@@ -23,6 +26,7 @@ class DeprecationErrorHandler
2326const MODE_DISABLED ='disabled ' ;
2427
2528private static $ isRegistered =false ;
29+ private static $ isAtLeastPhpUnit83 ;
2630
2731/**
2832 * Registers and configures the deprecation handler.
@@ -44,6 +48,7 @@ public static function register($mode = 0)
4448 }
4549
4650$ UtilPrefix =class_exists ('PHPUnit_Util_ErrorHandler ' ) ?'PHPUnit_Util_ ' :'PHPUnit\Util \\' ;
51+ self ::$ isAtLeastPhpUnit83 =method_exists (ErrorHandler::class,'__invoke ' );
4752
4853$ getMode =function ()use ($ mode ) {
4954static $ memoizedMode =false ;
@@ -106,9 +111,7 @@ public static function register($mode = 0)
106111 );
107112$ deprecationHandler =function ($ type ,$ msg ,$ file ,$ line ,$ context =array ())use (&$ deprecations ,$ getMode ,$ UtilPrefix ,$ inVendors ) {
108113if ((E_USER_DEPRECATED !==$ type &&E_DEPRECATED !==$ type ) || DeprecationErrorHandler::MODE_DISABLED ===$ mode =$ getMode ()) {
109- $ ErrorHandler =$ UtilPrefix .'ErrorHandler ' ;
110-
111- return $ ErrorHandler ::handleError ($ type ,$ msg ,$ file ,$ line ,$ context );
114+ return \call_user_func (DeprecationErrorHandler::getPhpUnitErrorHandler (),$ type ,$ msg ,$ file ,$ line ,$ context );
112115 }
113116
114117$ trace =debug_backtrace ();
@@ -183,7 +186,7 @@ public static function register($mode = 0)
183186
184187if (null !==$ oldErrorHandler ) {
185188restore_error_handler ();
186- if (array ($ UtilPrefix .'ErrorHandler ' ,'handleError ' ) ===$ oldErrorHandler ) {
189+ if ($ oldErrorHandler instanceof ErrorHandler || array ($ UtilPrefix .'ErrorHandler ' ,'handleError ' ) ===$ oldErrorHandler ) {
187190restore_error_handler ();
188191self ::register ($ mode );
189192 }
@@ -285,12 +288,8 @@ public static function collectDeprecations($outputFile)
285288if ($ previousErrorHandler ) {
286289return $ previousErrorHandler ($ type ,$ msg ,$ file ,$ line ,$ context );
287290 }
288- static $ autoload =true ;
289291
290- $ ErrorHandler =class_exists ('PHPUnit_Util_ErrorHandler ' ,$ autoload ) ?'PHPUnit_Util_ErrorHandler ' :'PHPUnit\Util\ErrorHandler ' ;
291- $ autoload =false ;
292-
293- return $ ErrorHandler ::handleError ($ type ,$ msg ,$ file ,$ line ,$ context );
292+ return \call_user_func (DeprecationErrorHandler::getPhpUnitErrorHandler (),$ type ,$ msg ,$ file ,$ line ,$ context );
294293 }
295294$ deprecations [] =array (error_reporting (),$ msg ,$ file );
296295 });
@@ -300,6 +299,29 @@ public static function collectDeprecations($outputFile)
300299 });
301300 }
302301
302+ /**
303+ * @internal
304+ */
305+ public static function getPhpUnitErrorHandler ()
306+ {
307+ if (!self ::$ isAtLeastPhpUnit83 ) {
308+ return (class_exists ('PHPUnit_Util_ErrorHandler ' ,false ) ?'PHPUnit_Util_ ' :'PHPUnit\Util \\' ).'ErrorHandler::handleError ' ;
309+ }
310+
311+ foreach (debug_backtrace (DEBUG_BACKTRACE_PROVIDE_OBJECT |DEBUG_BACKTRACE_IGNORE_ARGS )as $ frame ) {
312+ if (isset ($ frame ['object ' ]) &&$ frame ['object ' ]instanceof TestResult) {
313+ return new ErrorHandler (
314+ $ frame ['object ' ]->getConvertDeprecationsToExceptions (),
315+ $ frame ['object ' ]->getConvertErrorsToExceptions (),
316+ $ frame ['object ' ]->getConvertNoticesToExceptions (),
317+ $ frame ['object ' ]->getConvertWarningsToExceptions ()
318+ );
319+ }
320+ }
321+
322+ return function () {return false ; };
323+ }
324+
303325/**
304326 * Returns true if STDOUT is defined and supports colorization.
305327 *