<?php
/*
* php error handler / suppressed deprecation notices
* @link https://stackoverflow.com/a/74791189/367456
*/
// don't have E_DEPRECATED in reporting on @3v4l.org
error_reporting(error_reporting() ^ E_DEPRECATED);
// no interest in arguments
ini_set('zend.exception_ignore_args', '1');
/* track */ new Deprecations;
$mixed = null;
$string = @trim($mixed);
$html = @htmlspecialchars($mixed, ENT_QUOTES | ENT_HTML5);
$forgotten = trim($mixed);
class Deprecations
{
public function __construct($handler = true, $reporter = true)
{
$reporter && register_shutdown_function(static function () use (&$deprecations) {
// stream collected deprecations as JavaScript Object Notation (JSON) Text Sequences on the SAPI diagnostic channel
// shutdown handling could condense repeated reports etc. as could the error handler. example material, there must
// be no shutdown function at all.
foreach ((array)$deprecations as $deprecation) {
$error = $deprecation['error'];
unset($deprecation['error']);
is_object($error)
&& get_class($error) === ErrorException::class
&& $error = eval('return ' . substr(ltrim(var_export($error, true), '\\'), 27) . ';');
error_log("\x1E" . json_encode(
['E_DEPRECATED', [...array_values($deprecation), $error['trace'] ?? null]],
JSON_UNESCAPED_SLASHES
));
}
error_log("\x1E");
});
$handler && set_error_handler(static function ($type, $message, $file, $line) use (&$deprecations) {
// capture the currently active error reporting level to decide whether
// reporting is active (throws) or not (standard PHP error handler)
$reporting = error_reporting();
// capture backtraces, your mileage may vary: with many of them,
// you may not be interested in the beginning (resource constraints!).
$error = new ErrorException($message, $type, $reporting, $file, $line);
// capture E_DEPRECATED
if ($type === E_DEPRECATED) {
$deprecations[] =
['deprecations' => count($deprecations ?: [])]
+ get_defined_vars();
}
if (!($reporting & $type)) {
// This error code is not included in error_reporting, so let it fall
// through to the standard PHP error handler
return false;
}
// throwing error handler, your mileage may vary, controlled by
// error_reporting() / error_reporting (ini)
throw $error;
}, E_DEPRECATED);
}
}
["E_DEPRECATED",[0,8192,"trim(): Passing null to parameter #1 ($string) of type string is deprecated","/in/FYhhM",16,4437,[{"function":"{closure}","class":"Deprecations","type":"::"},{"file":"/in/FYhhM","line":16,"function":"trim"}]]]
["E_DEPRECATED",[1,8192,"htmlspecialchars(): Passing null to parameter #1 ($string) of type string is deprecated","/in/FYhhM",17,4437,[{"function":"{closure}","class":"Deprecations","type":"::"},{"file":"/in/FYhhM","line":17,"function":"htmlspecialchars"}]]]
["E_DEPRECATED",[2,8192,"trim(): Passing null to parameter #1 ($string) of type string is deprecated","/in/FYhhM",19,-8193,[{"function":"{closure}","class":"Deprecations","type":"::"},{"file":"/in/FYhhM","line":19,"function":"trim"}]]]