Skip to content

Commit e425b6a

Browse files
Merge branch '3.4' into 4.3
* 3.4: Minor fixes Use namespaced Phpunit classes [Form] remove leftover int child phpdoc Support DateTimeInterface in IntlDateFormatter::format [PhpUnitBridge] fixed PHPUnit 8.3 compatibility: method handleError was renamed to __invoke Use PHPunit assertion
2 parents 5c61a87 + 14934c8 commit e425b6a

File tree

1 file changed

+27
-10
lines changed

1 file changed

+27
-10
lines changed

DeprecationErrorHandler.php

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111

1212
namespace Symfony\Bridge\PhpUnit;
1313

14+
use PHPUnit\Framework\TestResult;
15+
use PHPUnit\Util\ErrorHandler;
1416
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
1517
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
1618

@@ -49,6 +51,7 @@ class DeprecationErrorHandler
4951

5052
private static $isRegistered = false;
5153
private static $utilPrefix;
54+
private static $isAtLeastPhpUnit83;
5255

5356
/**
5457
* Registers and configures the deprecation handler.
@@ -73,14 +76,15 @@ public static function register($mode = 0)
7376
}
7477

7578
self::$utilPrefix = class_exists('PHPUnit_Util_ErrorHandler') ? 'PHPUnit_Util_' : 'PHPUnit\Util\\';
79+
self::$isAtLeastPhpUnit83 = method_exists('PHPUnit\Util\ErrorHandler', '__invoke');
7680

7781
$handler = new self();
7882
$oldErrorHandler = set_error_handler([$handler, 'handleError']);
7983

8084
if (null !== $oldErrorHandler) {
8185
restore_error_handler();
8286

83-
if ([self::$utilPrefix.'ErrorHandler', 'handleError'] === $oldErrorHandler) {
87+
if ($oldErrorHandler instanceof ErrorHandler || [self::$utilPrefix.'ErrorHandler', 'handleError'] === $oldErrorHandler) {
8488
restore_error_handler();
8589
self::register($mode);
8690
}
@@ -100,12 +104,7 @@ public static function collectDeprecations($outputFile)
100104
return $previousErrorHandler($type, $msg, $file, $line, $context);
101105
}
102106

103-
static $autoload = true;
104-
105-
$ErrorHandler = class_exists('PHPUnit_Util_ErrorHandler', $autoload) ? 'PHPUnit_Util_ErrorHandler' : 'PHPUnit\Util\ErrorHandler';
106-
$autoload = false;
107-
108-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
107+
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
109108
}
110109

111110
$deprecations[] = [error_reporting(), $msg, $file];
@@ -122,9 +121,7 @@ public static function collectDeprecations($outputFile)
122121
public function handleError($type, $msg, $file, $line, $context = [])
123122
{
124123
if ((E_USER_DEPRECATED !== $type && E_DEPRECATED !== $type) || !$this->getConfiguration()->isEnabled()) {
125-
$ErrorHandler = self::$utilPrefix.'ErrorHandler';
126-
127-
return $ErrorHandler::handleError($type, $msg, $file, $line, $context);
124+
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
128125
}
129126

130127
$deprecation = new Deprecation($msg, debug_backtrace(), $file);
@@ -319,6 +316,26 @@ private function displayDeprecations($groups, $configuration)
319316
}
320317
}
321318

319+
private static function getPhpUnitErrorHandler()
320+
{
321+
if (!self::$isAtLeastPhpUnit83) {
322+
return (class_exists('PHPUnit_Util_ErrorHandler', false) ? 'PHPUnit_Util_' : 'PHPUnit\Util\\').'ErrorHandler::handleError';
323+
}
324+
325+
foreach (debug_backtrace(DEBUG_BACKTRACE_PROVIDE_OBJECT | DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
326+
if (isset($frame['object']) && $frame['object'] instanceof TestResult) {
327+
return new ErrorHandler(
328+
$frame['object']->getConvertDeprecationsToExceptions(),
329+
$frame['object']->getConvertErrorsToExceptions(),
330+
$frame['object']->getConvertNoticesToExceptions(),
331+
$frame['object']->getConvertWarningsToExceptions()
332+
);
333+
}
334+
}
335+
336+
return function () { return false; };
337+
}
338+
322339
/**
323340
* Returns true if STDOUT is defined and supports colorization.
324341
*

0 commit comments

Comments
 (0)