-
-
Notifications
You must be signed in to change notification settings - Fork 46
/
DeprecationErrorHandler.php
439 lines (369 loc) · 15.6 KB
/
DeprecationErrorHandler.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Symfony\Bridge\PhpUnit;
use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\TestResult;
use PHPUnit\Runner\ErrorHandler;
use PHPUnit\Util\Error\Handler;
use PHPUnit\Util\ErrorHandler as UtilErrorHandler;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Configuration;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\Deprecation;
use Symfony\Bridge\PhpUnit\DeprecationErrorHandler\DeprecationGroup;
use Symfony\Component\ErrorHandler\DebugClassLoader;
/**
* Catch deprecation notices and print a summary report at the end of the test suite.
*
* @author Nicolas Grekas <[email protected]>
*/
class DeprecationErrorHandler
{
public const MODE_DISABLED = 'disabled';
public const MODE_WEAK = 'max[total]=999999&verbose=0';
public const MODE_STRICT = 'max[total]=0';
private $mode;
private $configuration;
/**
* @var DeprecationGroup[]
*/
private $deprecationGroups = [];
private static $isRegistered = false;
private static $errorHandler;
public function __construct()
{
$this->resetDeprecationGroups();
}
/**
* Registers and configures the deprecation handler.
*
* The mode is a query string with options:
* - "disabled" to enable/disable the deprecation handler
* - "verbose" to enable/disable displaying the deprecation report
* - "quiet" to disable displaying the deprecation report only for some groups (i.e. quiet[]=other)
* - "max" to configure the number of deprecations to allow before exiting with a non-zero
* status code; it's an array with keys "total", "self", "direct" and "indirect"
*
* The default mode is "max[total]=0&verbose=1".
*
* The mode can alternatively be "/some-regexp/" to stop the test suite whenever
* a deprecation message matches the given regular expression.
*
* @param int|string|false $mode The reporting mode, defaults to not allowing any deprecations
*/
public static function register($mode = 0)
{
if (self::$isRegistered) {
return;
}
$handler = new self();
$oldErrorHandler = set_error_handler([$handler, 'handleError']);
if (null !== $oldErrorHandler) {
restore_error_handler();
if (
$oldErrorHandler instanceof UtilErrorHandler
|| [UtilErrorHandler::class, 'handleError'] === $oldErrorHandler
|| $oldErrorHandler instanceof ErrorHandler
|| [ErrorHandler::class, 'handleError'] === $oldErrorHandler
) {
restore_error_handler();
self::register($mode);
}
} else {
$handler->mode = $mode;
self::$isRegistered = true;
register_shutdown_function([$handler, 'shutdown']);
}
}
public static function collectDeprecations($outputFile)
{
$deprecations = [];
$previousErrorHandler = set_error_handler(function ($type, $msg, $file, $line, $context = []) use (&$deprecations, &$previousErrorHandler) {
if (\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type && (\E_WARNING !== $type || false === strpos($msg, '" targeting switch is equivalent to "break'))) {
if ($previousErrorHandler) {
return $previousErrorHandler($type, $msg, $file, $line, $context);
}
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
}
$filesStack = [];
foreach (debug_backtrace() as $frame) {
if (!isset($frame['file']) || \in_array($frame['function'], ['require', 'require_once', 'include', 'include_once'], true)) {
continue;
}
$filesStack[] = $frame['file'];
}
$deprecations[] = [error_reporting() & $type, $msg, $file, $filesStack];
return null;
});
register_shutdown_function(function () use ($outputFile, &$deprecations) {
file_put_contents($outputFile, serialize($deprecations));
});
}
/**
* @internal
*/
public function handleError($type, $msg, $file, $line, $context = [])
{
if ((\E_USER_DEPRECATED !== $type && \E_DEPRECATED !== $type && (\E_WARNING !== $type || false === strpos($msg, '" targeting switch is equivalent to "break'))) || !$this->getConfiguration()->isEnabled()) {
return \call_user_func(self::getPhpUnitErrorHandler(), $type, $msg, $file, $line, $context);
}
$trace = debug_backtrace();
if (isset($trace[1]['function'], $trace[1]['args'][0]) && ('trigger_error' === $trace[1]['function'] || 'user_error' === $trace[1]['function'])) {
$msg = $trace[1]['args'][0];
}
$deprecation = new Deprecation($msg, $trace, $file, \E_DEPRECATED === $type);
if ($deprecation->isMuted()) {
return null;
}
if ($this->getConfiguration()->isIgnoredDeprecation($deprecation)) {
return null;
}
if ($this->getConfiguration()->isBaselineDeprecation($deprecation)) {
return null;
}
$msg = $deprecation->getMessage();
if (\E_DEPRECATED !== $type && (error_reporting() & $type)) {
$group = 'unsilenced';
} elseif ($deprecation->isLegacy()) {
$group = 'legacy';
} else {
$group = [
Deprecation::TYPE_SELF => 'self',
Deprecation::TYPE_DIRECT => 'direct',
Deprecation::TYPE_INDIRECT => 'indirect',
Deprecation::TYPE_UNDETERMINED => 'other',
][$deprecation->getType()];
}
if ($this->getConfiguration()->shouldDisplayStackTrace($msg)) {
echo "\n".ucfirst($group).' '.$deprecation->toString();
exit(1);
}
if ('legacy' === $group) {
$this->deprecationGroups[$group]->addNotice();
} elseif ($deprecation->originatesFromAnObject()) {
$class = $deprecation->originatingClass();
$method = $deprecation->originatingMethod();
$this->deprecationGroups[$group]->addNoticeFromObject($msg, $class, $method);
} else {
$this->deprecationGroups[$group]->addNoticeFromProceduralCode($msg);
}
return null;
}
/**
* @internal
*/
public function shutdown()
{
$configuration = $this->getConfiguration();
if ($configuration->isInRegexMode()) {
return;
}
if (class_exists(DebugClassLoader::class, false)) {
DebugClassLoader::checkClasses();
}
$currErrorHandler = set_error_handler('is_int');
restore_error_handler();
if ($currErrorHandler !== [$this, 'handleError']) {
echo "\n", self::colorize('THE ERROR HANDLER HAS CHANGED!', true), "\n";
}
$groups = array_keys($this->deprecationGroups);
// store failing status
$isFailing = !$configuration->tolerates($this->deprecationGroups);
$this->displayDeprecations($groups, $configuration);
$this->resetDeprecationGroups();
register_shutdown_function(function () use ($isFailing, $groups, $configuration) {
foreach ($this->deprecationGroups as $group) {
if ($group->count() > 0) {
echo "Shutdown-time deprecations:\n";
break;
}
}
$isFailingAtShutdown = !$configuration->tolerates($this->deprecationGroups);
$this->displayDeprecations($groups, $configuration);
if ($configuration->isGeneratingBaseline()) {
$configuration->writeBaseline();
}
if ($isFailing || $isFailingAtShutdown) {
exit(1);
}
});
}
private function resetDeprecationGroups()
{
$this->deprecationGroups = [
'unsilenced' => new DeprecationGroup(),
'self' => new DeprecationGroup(),
'direct' => new DeprecationGroup(),
'indirect' => new DeprecationGroup(),
'legacy' => new DeprecationGroup(),
'other' => new DeprecationGroup(),
];
}
private function getConfiguration()
{
if (null !== $this->configuration) {
return $this->configuration;
}
if (false === $mode = $this->mode) {
$mode = $_SERVER['SYMFONY_DEPRECATIONS_HELPER'] ?? $_ENV['SYMFONY_DEPRECATIONS_HELPER'] ?? getenv('SYMFONY_DEPRECATIONS_HELPER');
}
if ('strict' === $mode) {
return $this->configuration = Configuration::inStrictMode();
}
if (self::MODE_DISABLED === $mode) {
return $this->configuration = Configuration::inDisabledMode();
}
if ('weak' === $mode) {
return $this->configuration = Configuration::inWeakMode();
}
if (isset($mode[0]) && '/' === $mode[0]) {
return $this->configuration = Configuration::fromRegex($mode);
}
if (preg_match('/^[1-9][0-9]*$/', (string) $mode)) {
return $this->configuration = Configuration::fromNumber($mode);
}
if (!$mode) {
return $this->configuration = Configuration::fromNumber(0);
}
return $this->configuration = Configuration::fromUrlEncodedString((string) $mode);
}
private static function colorize(string $str, bool $red): string
{
if (!self::hasColorSupport()) {
return $str;
}
$color = $red ? '41;37' : '43;30';
return "\x1B[{$color}m{$str}\x1B[0m";
}
/**
* @param string[] $groups
*/
private function displayDeprecations(array $groups, Configuration $configuration): void
{
$cmp = function ($a, $b) {
return $b->count() - $a->count();
};
if ($configuration->shouldWriteToLogFile()) {
if (false === $handle = @fopen($file = $configuration->getLogFile(), 'a')) {
throw new \InvalidArgumentException(sprintf('The configured log file "%s" is not writeable.', $file));
}
} else {
$handle = fopen('php://output', 'w');
}
foreach ($groups as $group) {
if ($this->deprecationGroups[$group]->count()) {
$deprecationGroupMessage = sprintf(
'%s deprecation notices (%d)',
\in_array($group, ['direct', 'indirect', 'self'], true) ? "Remaining $group" : ucfirst($group),
$this->deprecationGroups[$group]->count()
);
if ($configuration->shouldWriteToLogFile()) {
fwrite($handle, "\n$deprecationGroupMessage\n");
} else {
fwrite($handle, "\n".self::colorize($deprecationGroupMessage, 'legacy' !== $group && 'indirect' !== $group)."\n");
}
// Skip the verbose output if the group is quiet and not failing according to its threshold:
if ('legacy' !== $group && !$configuration->verboseOutput($group) && $configuration->toleratesForGroup($group, $this->deprecationGroups)) {
continue;
}
$notices = $this->deprecationGroups[$group]->notices();
uasort($notices, $cmp);
foreach ($notices as $msg => $notice) {
fwrite($handle, sprintf("\n %sx: %s\n", $notice->count(), $msg));
$countsByCaller = $notice->getCountsByCaller();
arsort($countsByCaller);
$limit = 5;
foreach ($countsByCaller as $method => $count) {
if ('count' !== $method) {
if (!$limit--) {
fwrite($handle, " ...\n");
break;
}
fwrite($handle, sprintf(" %dx in %s\n", $count, preg_replace('/(.*)\\\\(.*?::.*?)$/', '$2 from $1', $method)));
}
}
}
}
}
if (!empty($notices)) {
fwrite($handle, "\n");
}
}
private static function getPhpUnitErrorHandler(): callable
{
if (!$eh = self::$errorHandler) {
if (class_exists(Handler::class)) {
$eh = self::$errorHandler = Handler::class;
} elseif (method_exists(UtilErrorHandler::class, '__invoke')) {
$eh = self::$errorHandler = UtilErrorHandler::class;
} elseif (method_exists(ErrorHandler::class, '__invoke')) {
$eh = self::$errorHandler = ErrorHandler::class;
} else {
return self::$errorHandler = 'PHPUnit\Util\ErrorHandler::handleError';
}
}
if ('PHPUnit\Util\ErrorHandler::handleError' === $eh) {
return $eh;
}
foreach (debug_backtrace(\DEBUG_BACKTRACE_PROVIDE_OBJECT | \DEBUG_BACKTRACE_IGNORE_ARGS) as $frame) {
if (!isset($frame['object'])) {
continue;
}
if ($frame['object'] instanceof TestResult) {
return new $eh(
$frame['object']->getConvertDeprecationsToExceptions(),
$frame['object']->getConvertErrorsToExceptions(),
$frame['object']->getConvertNoticesToExceptions(),
$frame['object']->getConvertWarningsToExceptions()
);
} elseif (ErrorHandler::class === $eh && $frame['object'] instanceof TestCase) {
return function (int $errorNumber, string $errorString, string $errorFile, int $errorLine) {
ErrorHandler::instance()($errorNumber, $errorString, $errorFile, $errorLine);
return true;
};
}
}
return function () { return false; };
}
/**
* Returns true if STDOUT is defined and supports colorization.
*
* Reference: Composer\XdebugHandler\Process::supportsColor
* https://github.com/composer/xdebug-handler
*/
private static function hasColorSupport(): bool
{
if (!\defined('STDOUT')) {
return false;
}
// Follow https://no-color.org/
if ('' !== ($_SERVER['NO_COLOR'] ?? getenv('NO_COLOR') ?: '')) {
return false;
}
// Detect msysgit/mingw and assume this is a tty because detection
// does not work correctly, see https://github.com/composer/composer/issues/9690
if (!@stream_isatty(\STDOUT) && !\in_array(strtoupper((string) getenv('MSYSTEM')), ['MINGW32', 'MINGW64'], true)) {
return false;
}
if ('\\' === \DIRECTORY_SEPARATOR && @sapi_windows_vt100_support(\STDOUT)) {
return true;
}
if ('Hyper' === getenv('TERM_PROGRAM')
|| false !== getenv('COLORTERM')
|| false !== getenv('ANSICON')
|| 'ON' === getenv('ConEmuANSI')
) {
return true;
}
if ('dumb' === $term = (string) getenv('TERM')) {
return false;
}
// See https://github.com/chalk/supports-color/blob/d4f413efaf8da045c5ab440ed418ef02dbb28bf1/index.js#L157
return preg_match('/^((screen|xterm|vt100|vt220|putty|rxvt|ansi|cygwin|linux).*)|(.*-256(color)?(-bce)?)$/', $term);
}
}