vendor/symfony/debug/ErrorHandler.php line 581

Open in your IDE?
  1. <?php
  2. /*
  3.  * This file is part of the Symfony package.
  4.  *
  5.  * (c) Fabien Potencier <fabien@symfony.com>
  6.  *
  7.  * For the full copyright and license information, please view the LICENSE
  8.  * file that was distributed with this source code.
  9.  */
  10. namespace Symfony\Component\Debug;
  11. use Psr\Log\LoggerInterface;
  12. use Psr\Log\LogLevel;
  13. use Symfony\Component\Debug\Exception\FatalErrorException;
  14. use Symfony\Component\Debug\Exception\FatalThrowableError;
  15. use Symfony\Component\Debug\Exception\FlattenException;
  16. use Symfony\Component\Debug\Exception\OutOfMemoryException;
  17. use Symfony\Component\Debug\Exception\SilencedErrorContext;
  18. use Symfony\Component\Debug\FatalErrorHandler\ClassNotFoundFatalErrorHandler;
  19. use Symfony\Component\Debug\FatalErrorHandler\FatalErrorHandlerInterface;
  20. use Symfony\Component\Debug\FatalErrorHandler\UndefinedFunctionFatalErrorHandler;
  21. use Symfony\Component\Debug\FatalErrorHandler\UndefinedMethodFatalErrorHandler;
  22. @trigger_error(sprintf('The "%s" class is deprecated since Symfony 4.4, use "%s" instead.'ErrorHandler::class, \Symfony\Component\ErrorHandler\ErrorHandler::class), E_USER_DEPRECATED);
  23. /**
  24.  * A generic ErrorHandler for the PHP engine.
  25.  *
  26.  * Provides five bit fields that control how errors are handled:
  27.  * - thrownErrors: errors thrown as \ErrorException
  28.  * - loggedErrors: logged errors, when not @-silenced
  29.  * - scopedErrors: errors thrown or logged with their local context
  30.  * - tracedErrors: errors logged with their stack trace
  31.  * - screamedErrors: never @-silenced errors
  32.  *
  33.  * Each error level can be logged by a dedicated PSR-3 logger object.
  34.  * Screaming only applies to logging.
  35.  * Throwing takes precedence over logging.
  36.  * Uncaught exceptions are logged as E_ERROR.
  37.  * E_DEPRECATED and E_USER_DEPRECATED levels never throw.
  38.  * E_RECOVERABLE_ERROR and E_USER_ERROR levels always throw.
  39.  * Non catchable errors that can be detected at shutdown time are logged when the scream bit field allows so.
  40.  * As errors have a performance cost, repeated errors are all logged, so that the developer
  41.  * can see them and weight them as more important to fix than others of the same level.
  42.  *
  43.  * @author Nicolas Grekas <p@tchwork.com>
  44.  * @author GrĂ©goire Pineau <lyrixx@lyrixx.info>
  45.  *
  46.  * @final since Symfony 4.3
  47.  *
  48.  * @deprecated since Symfony 4.4, use Symfony\Component\ErrorHandler\ErrorHandler instead.
  49.  */
  50. class ErrorHandler
  51. {
  52.     private $levels = [
  53.         E_DEPRECATED => 'Deprecated',
  54.         E_USER_DEPRECATED => 'User Deprecated',
  55.         E_NOTICE => 'Notice',
  56.         E_USER_NOTICE => 'User Notice',
  57.         E_STRICT => 'Runtime Notice',
  58.         E_WARNING => 'Warning',
  59.         E_USER_WARNING => 'User Warning',
  60.         E_COMPILE_WARNING => 'Compile Warning',
  61.         E_CORE_WARNING => 'Core Warning',
  62.         E_USER_ERROR => 'User Error',
  63.         E_RECOVERABLE_ERROR => 'Catchable Fatal Error',
  64.         E_COMPILE_ERROR => 'Compile Error',
  65.         E_PARSE => 'Parse Error',
  66.         E_ERROR => 'Error',
  67.         E_CORE_ERROR => 'Core Error',
  68.     ];
  69.     private $loggers = [
  70.         E_DEPRECATED => [nullLogLevel::INFO],
  71.         E_USER_DEPRECATED => [nullLogLevel::INFO],
  72.         E_NOTICE => [nullLogLevel::WARNING],
  73.         E_USER_NOTICE => [nullLogLevel::WARNING],
  74.         E_STRICT => [nullLogLevel::WARNING],
  75.         E_WARNING => [nullLogLevel::WARNING],
  76.         E_USER_WARNING => [nullLogLevel::WARNING],
  77.         E_COMPILE_WARNING => [nullLogLevel::WARNING],
  78.         E_CORE_WARNING => [nullLogLevel::WARNING],
  79.         E_USER_ERROR => [nullLogLevel::CRITICAL],
  80.         E_RECOVERABLE_ERROR => [nullLogLevel::CRITICAL],
  81.         E_COMPILE_ERROR => [nullLogLevel::CRITICAL],
  82.         E_PARSE => [nullLogLevel::CRITICAL],
  83.         E_ERROR => [nullLogLevel::CRITICAL],
  84.         E_CORE_ERROR => [nullLogLevel::CRITICAL],
  85.     ];
  86.     private $thrownErrors 0x1FFF// E_ALL - E_DEPRECATED - E_USER_DEPRECATED
  87.     private $scopedErrors 0x1FFF// E_ALL - E_DEPRECATED - E_USER_DEPRECATED
  88.     private $tracedErrors 0x77FB// E_ALL - E_STRICT - E_PARSE
  89.     private $screamedErrors 0x55// E_ERROR + E_CORE_ERROR + E_COMPILE_ERROR + E_PARSE
  90.     private $loggedErrors 0;
  91.     private $traceReflector;
  92.     private $isRecursive 0;
  93.     private $isRoot false;
  94.     private $exceptionHandler;
  95.     private $bootstrappingLogger;
  96.     private static $reservedMemory;
  97.     private static $toStringException null;
  98.     private static $silencedErrorCache = [];
  99.     private static $silencedErrorCount 0;
  100.     private static $exitCode 0;
  101.     /**
  102.      * Registers the error handler.
  103.      *
  104.      * @param self|null $handler The handler to register
  105.      * @param bool      $replace Whether to replace or not any existing handler
  106.      *
  107.      * @return self The registered error handler
  108.      */
  109.     public static function register(self $handler null$replace true)
  110.     {
  111.         if (null === self::$reservedMemory) {
  112.             self::$reservedMemory str_repeat('x'10240);
  113.             register_shutdown_function(__CLASS__.'::handleFatalError');
  114.         }
  115.         if ($handlerIsNew null === $handler) {
  116.             $handler = new static();
  117.         }
  118.         if (null === $prev set_error_handler([$handler'handleError'])) {
  119.             restore_error_handler();
  120.             // Specifying the error types earlier would expose us to https://bugs.php.net/63206
  121.             set_error_handler([$handler'handleError'], $handler->thrownErrors $handler->loggedErrors);
  122.             $handler->isRoot true;
  123.         }
  124.         if ($handlerIsNew && \is_array($prev) && $prev[0] instanceof self) {
  125.             $handler $prev[0];
  126.             $replace false;
  127.         }
  128.         if (!$replace && $prev) {
  129.             restore_error_handler();
  130.             $handlerIsRegistered = \is_array($prev) && $handler === $prev[0];
  131.         } else {
  132.             $handlerIsRegistered true;
  133.         }
  134.         if (\is_array($prev set_exception_handler([$handler'handleException'])) && $prev[0] instanceof self) {
  135.             restore_exception_handler();
  136.             if (!$handlerIsRegistered) {
  137.                 $handler $prev[0];
  138.             } elseif ($handler !== $prev[0] && $replace) {
  139.                 set_exception_handler([$handler'handleException']);
  140.                 $p $prev[0]->setExceptionHandler(null);
  141.                 $handler->setExceptionHandler($p);
  142.                 $prev[0]->setExceptionHandler($p);
  143.             }
  144.         } else {
  145.             $handler->setExceptionHandler($prev);
  146.         }
  147.         $handler->throwAt(E_ALL $handler->thrownErrorstrue);
  148.         return $handler;
  149.     }
  150.     public function __construct(BufferingLogger $bootstrappingLogger null)
  151.     {
  152.         if ($bootstrappingLogger) {
  153.             $this->bootstrappingLogger $bootstrappingLogger;
  154.             $this->setDefaultLogger($bootstrappingLogger);
  155.         }
  156.         $this->traceReflector = new \ReflectionProperty('Exception''trace');
  157.         $this->traceReflector->setAccessible(true);
  158.     }
  159.     /**
  160.      * Sets a logger to non assigned errors levels.
  161.      *
  162.      * @param array|int $levels  An array map of E_* to LogLevel::* or an integer bit field of E_* constants
  163.      * @param bool      $replace Whether to replace or not any existing logger
  164.      */
  165.     public function setDefaultLogger(LoggerInterface $logger$levels E_ALL$replace false)
  166.     {
  167.         $loggers = [];
  168.         if (\is_array($levels)) {
  169.             foreach ($levels as $type => $logLevel) {
  170.                 if (empty($this->loggers[$type][0]) || $replace || $this->loggers[$type][0] === $this->bootstrappingLogger) {
  171.                     $loggers[$type] = [$logger$logLevel];
  172.                 }
  173.             }
  174.         } else {
  175.             if (null === $levels) {
  176.                 $levels E_ALL;
  177.             }
  178.             foreach ($this->loggers as $type => $log) {
  179.                 if (($type $levels) && (empty($log[0]) || $replace || $log[0] === $this->bootstrappingLogger)) {
  180.                     $log[0] = $logger;
  181.                     $loggers[$type] = $log;
  182.                 }
  183.             }
  184.         }
  185.         $this->setLoggers($loggers);
  186.     }
  187.     /**
  188.      * Sets a logger for each error level.
  189.      *
  190.      * @param array $loggers Error levels to [LoggerInterface|null, LogLevel::*] map
  191.      *
  192.      * @return array The previous map
  193.      *
  194.      * @throws \InvalidArgumentException
  195.      */
  196.     public function setLoggers(array $loggers)
  197.     {
  198.         $prevLogged $this->loggedErrors;
  199.         $prev $this->loggers;
  200.         $flush = [];
  201.         foreach ($loggers as $type => $log) {
  202.             if (!isset($prev[$type])) {
  203.                 throw new \InvalidArgumentException('Unknown error type: '.$type);
  204.             }
  205.             if (!\is_array($log)) {
  206.                 $log = [$log];
  207.             } elseif (!\array_key_exists(0$log)) {
  208.                 throw new \InvalidArgumentException('No logger provided.');
  209.             }
  210.             if (null === $log[0]) {
  211.                 $this->loggedErrors &= ~$type;
  212.             } elseif ($log[0] instanceof LoggerInterface) {
  213.                 $this->loggedErrors |= $type;
  214.             } else {
  215.                 throw new \InvalidArgumentException('Invalid logger provided.');
  216.             }
  217.             $this->loggers[$type] = $log $prev[$type];
  218.             if ($this->bootstrappingLogger && $prev[$type][0] === $this->bootstrappingLogger) {
  219.                 $flush[$type] = $type;
  220.             }
  221.         }
  222.         $this->reRegister($prevLogged $this->thrownErrors);
  223.         if ($flush) {
  224.             foreach ($this->bootstrappingLogger->cleanLogs() as $log) {
  225.                 $type $log[2]['exception'] instanceof \ErrorException $log[2]['exception']->getSeverity() : E_ERROR;
  226.                 if (!isset($flush[$type])) {
  227.                     $this->bootstrappingLogger->log($log[0], $log[1], $log[2]);
  228.                 } elseif ($this->loggers[$type][0]) {
  229.                     $this->loggers[$type][0]->log($this->loggers[$type][1], $log[1], $log[2]);
  230.                 }
  231.             }
  232.         }
  233.         return $prev;
  234.     }
  235.     /**
  236.      * Sets a user exception handler.
  237.      *
  238.      * @param callable $handler A handler that will be called on Exception
  239.      *
  240.      * @return callable|null The previous exception handler
  241.      */
  242.     public function setExceptionHandler(callable $handler null)
  243.     {
  244.         $prev $this->exceptionHandler;
  245.         $this->exceptionHandler $handler;
  246.         return $prev;
  247.     }
  248.     /**
  249.      * Sets the PHP error levels that throw an exception when a PHP error occurs.
  250.      *
  251.      * @param int  $levels  A bit field of E_* constants for thrown errors
  252.      * @param bool $replace Replace or amend the previous value
  253.      *
  254.      * @return int The previous value
  255.      */
  256.     public function throwAt($levels$replace false)
  257.     {
  258.         $prev $this->thrownErrors;
  259.         $this->thrownErrors = ($levels E_RECOVERABLE_ERROR E_USER_ERROR) & ~E_USER_DEPRECATED & ~E_DEPRECATED;
  260.         if (!$replace) {
  261.             $this->thrownErrors |= $prev;
  262.         }
  263.         $this->reRegister($prev $this->loggedErrors);
  264.         return $prev;
  265.     }
  266.     /**
  267.      * Sets the PHP error levels for which local variables are preserved.
  268.      *
  269.      * @param int  $levels  A bit field of E_* constants for scoped errors
  270.      * @param bool $replace Replace or amend the previous value
  271.      *
  272.      * @return int The previous value
  273.      */
  274.     public function scopeAt($levels$replace false)
  275.     {
  276.         $prev $this->scopedErrors;
  277.         $this->scopedErrors = (int) $levels;
  278.         if (!$replace) {
  279.             $this->scopedErrors |= $prev;
  280.         }
  281.         return $prev;
  282.     }
  283.     /**
  284.      * Sets the PHP error levels for which the stack trace is preserved.
  285.      *
  286.      * @param int  $levels  A bit field of E_* constants for traced errors
  287.      * @param bool $replace Replace or amend the previous value
  288.      *
  289.      * @return int The previous value
  290.      */
  291.     public function traceAt($levels$replace false)
  292.     {
  293.         $prev $this->tracedErrors;
  294.         $this->tracedErrors = (int) $levels;
  295.         if (!$replace) {
  296.             $this->tracedErrors |= $prev;
  297.         }
  298.         return $prev;
  299.     }
  300.     /**
  301.      * Sets the error levels where the @-operator is ignored.
  302.      *
  303.      * @param int  $levels  A bit field of E_* constants for screamed errors
  304.      * @param bool $replace Replace or amend the previous value
  305.      *
  306.      * @return int The previous value
  307.      */
  308.     public function screamAt($levels$replace false)
  309.     {
  310.         $prev $this->screamedErrors;
  311.         $this->screamedErrors = (int) $levels;
  312.         if (!$replace) {
  313.             $this->screamedErrors |= $prev;
  314.         }
  315.         return $prev;
  316.     }
  317.     /**
  318.      * Re-registers as a PHP error handler if levels changed.
  319.      */
  320.     private function reRegister(int $prev)
  321.     {
  322.         if ($prev !== $this->thrownErrors $this->loggedErrors) {
  323.             $handler set_error_handler('var_dump');
  324.             $handler = \is_array($handler) ? $handler[0] : null;
  325.             restore_error_handler();
  326.             if ($handler === $this) {
  327.                 restore_error_handler();
  328.                 if ($this->isRoot) {
  329.                     set_error_handler([$this'handleError'], $this->thrownErrors $this->loggedErrors);
  330.                 } else {
  331.                     set_error_handler([$this'handleError']);
  332.                 }
  333.             }
  334.         }
  335.     }
  336.     /**
  337.      * Handles errors by filtering then logging them according to the configured bit fields.
  338.      *
  339.      * @param int    $type    One of the E_* constants
  340.      * @param string $message
  341.      * @param string $file
  342.      * @param int    $line
  343.      *
  344.      * @return bool Returns false when no handling happens so that the PHP engine can handle the error itself
  345.      *
  346.      * @throws \ErrorException When $this->thrownErrors requests so
  347.      *
  348.      * @internal
  349.      */
  350.     public function handleError($type$message$file$line)
  351.     {
  352.         if (\PHP_VERSION_ID >= 70300 && E_WARNING === $type && '"' === $message[0] && false !== strpos($message'" targeting switch is equivalent to "break')) {
  353.             $type E_DEPRECATED;
  354.         }
  355.         // Level is the current error reporting level to manage silent error.
  356.         $level error_reporting();
  357.         $silenced === ($level $type);
  358.         // Strong errors are not authorized to be silenced.
  359.         $level |= E_RECOVERABLE_ERROR E_USER_ERROR E_DEPRECATED E_USER_DEPRECATED;
  360.         $log $this->loggedErrors $type;
  361.         $throw $this->thrownErrors $type $level;
  362.         $type &= $level $this->screamedErrors;
  363.         if (!$type || (!$log && !$throw)) {
  364.             return !$silenced && $type && $log;
  365.         }
  366.         $scope $this->scopedErrors $type;
  367.         if ($numArgs = \func_num_args()) {
  368.             $context $scope ? (func_get_arg(4) ?: []) : [];
  369.         } else {
  370.             $context = [];
  371.         }
  372.         if (isset($context['GLOBALS']) && $scope) {
  373.             $e $context;                  // Whatever the signature of the method,
  374.             unset($e['GLOBALS'], $context); // $context is always a reference in 5.3
  375.             $context $e;
  376.         }
  377.         if (false !== strpos($message"@anonymous\0")) {
  378.             $logMessage $this->levels[$type].': '.(new FlattenException())->setMessage($message)->getMessage();
  379.         } else {
  380.             $logMessage $this->levels[$type].': '.$message;
  381.         }
  382.         if (null !== self::$toStringException) {
  383.             $errorAsException self::$toStringException;
  384.             self::$toStringException null;
  385.         } elseif (!$throw && !($type $level)) {
  386.             if (!isset(self::$silencedErrorCache[$id $file.':'.$line])) {
  387.                 $lightTrace $this->tracedErrors $type $this->cleanTrace(debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS5), $type$file$linefalse) : [];
  388.                 $errorAsException = new SilencedErrorContext($type$file$line, isset($lightTrace[1]) ? [$lightTrace[0]] : $lightTrace);
  389.             } elseif (isset(self::$silencedErrorCache[$id][$message])) {
  390.                 $lightTrace null;
  391.                 $errorAsException self::$silencedErrorCache[$id][$message];
  392.                 ++$errorAsException->count;
  393.             } else {
  394.                 $lightTrace = [];
  395.                 $errorAsException null;
  396.             }
  397.             if (100 < ++self::$silencedErrorCount) {
  398.                 self::$silencedErrorCache $lightTrace = [];
  399.                 self::$silencedErrorCount 1;
  400.             }
  401.             if ($errorAsException) {
  402.                 self::$silencedErrorCache[$id][$message] = $errorAsException;
  403.             }
  404.             if (null === $lightTrace) {
  405.                 return true;
  406.             }
  407.         } else {
  408.             $errorAsException = new \ErrorException($logMessage0$type$file$line);
  409.             if ($throw || $this->tracedErrors $type) {
  410.                 $backtrace $errorAsException->getTrace();
  411.                 $lightTrace $this->cleanTrace($backtrace$type$file$line$throw);
  412.                 $this->traceReflector->setValue($errorAsException$lightTrace);
  413.             } else {
  414.                 $this->traceReflector->setValue($errorAsException, []);
  415.                 $backtrace = [];
  416.             }
  417.         }
  418.         if ($throw) {
  419.             if (\PHP_VERSION_ID 70400 && E_USER_ERROR $type) {
  420.                 for ($i 1; isset($backtrace[$i]); ++$i) {
  421.                     if (isset($backtrace[$i]['function'], $backtrace[$i]['type'], $backtrace[$i 1]['function'])
  422.                         && '__toString' === $backtrace[$i]['function']
  423.                         && '->' === $backtrace[$i]['type']
  424.                         && !isset($backtrace[$i 1]['class'])
  425.                         && ('trigger_error' === $backtrace[$i 1]['function'] || 'user_error' === $backtrace[$i 1]['function'])
  426.                     ) {
  427.                         // Here, we know trigger_error() has been called from __toString().
  428.                         // PHP triggers a fatal error when throwing from __toString().
  429.                         // A small convention allows working around the limitation:
  430.                         // given a caught $e exception in __toString(), quitting the method with
  431.                         // `return trigger_error($e, E_USER_ERROR);` allows this error handler
  432.                         // to make $e get through the __toString() barrier.
  433.                         foreach ($context as $e) {
  434.                             if ($e instanceof \Throwable && $e->__toString() === $message) {
  435.                                 self::$toStringException $e;
  436.                                 return true;
  437.                             }
  438.                         }
  439.                         // Display the original error message instead of the default one.
  440.                         $this->handleException($errorAsException);
  441.                         // Stop the process by giving back the error to the native handler.
  442.                         return false;
  443.                     }
  444.                 }
  445.             }
  446.             throw $errorAsException;
  447.         }
  448.         if ($this->isRecursive) {
  449.             $log 0;
  450.         } else {
  451.             if (\PHP_VERSION_ID < (\PHP_VERSION_ID 70400 70316 70404) && !\defined('HHVM_VERSION')) {
  452.                 $currentErrorHandler set_error_handler('var_dump');
  453.                 restore_error_handler();
  454.             }
  455.             try {
  456.                 $this->isRecursive true;
  457.                 $level = ($type $level) ? $this->loggers[$type][1] : LogLevel::DEBUG;
  458.                 $this->loggers[$type][0]->log($level$logMessage$errorAsException ? ['exception' => $errorAsException] : []);
  459.             } finally {
  460.                 $this->isRecursive false;
  461.                 if (\PHP_VERSION_ID < (\PHP_VERSION_ID 70400 70316 70404) && !\defined('HHVM_VERSION')) {
  462.                     set_error_handler($currentErrorHandler);
  463.                 }
  464.             }
  465.         }
  466.         return !$silenced && $type && $log;
  467.     }
  468.     /**
  469.      * Handles an exception by logging then forwarding it to another handler.
  470.      *
  471.      * @param \Exception|\Throwable $exception An exception to handle
  472.      * @param array                 $error     An array as returned by error_get_last()
  473.      *
  474.      * @internal
  475.      */
  476.     public function handleException($exception, array $error null)
  477.     {
  478.         if (null === $error) {
  479.             self::$exitCode 255;
  480.         }
  481.         if (!$exception instanceof \Exception) {
  482.             $exception = new FatalThrowableError($exception);
  483.         }
  484.         $type $exception instanceof FatalErrorException $exception->getSeverity() : E_ERROR;
  485.         $handlerException null;
  486.         if (($this->loggedErrors $type) || $exception instanceof FatalThrowableError) {
  487.             if (false !== strpos($message $exception->getMessage(), "@anonymous\0")) {
  488.                 $message = (new FlattenException())->setMessage($message)->getMessage();
  489.             }
  490.             if ($exception instanceof FatalErrorException) {
  491.                 if ($exception instanceof FatalThrowableError) {
  492.                     $error = [
  493.                         'type' => $type,
  494.                         'message' => $message,
  495.                         'file' => $exception->getFile(),
  496.                         'line' => $exception->getLine(),
  497.                     ];
  498.                 } else {
  499.                     $message 'Fatal '.$message;
  500.                 }
  501.             } elseif ($exception instanceof \ErrorException) {
  502.                 $message 'Uncaught '.$message;
  503.             } else {
  504.                 $message 'Uncaught Exception: '.$message;
  505.             }
  506.         }
  507.         if ($this->loggedErrors $type) {
  508.             try {
  509.                 $this->loggers[$type][0]->log($this->loggers[$type][1], $message, ['exception' => $exception]);
  510.             } catch (\Throwable $handlerException) {
  511.             }
  512.         }
  513.         if ($exception instanceof FatalErrorException && !$exception instanceof OutOfMemoryException && $error) {
  514.             foreach ($this->getFatalErrorHandlers() as $handler) {
  515.                 if ($e $handler->handleError($error$exception)) {
  516.                     $exception $e;
  517.                     break;
  518.                 }
  519.             }
  520.         }
  521.         $exceptionHandler $this->exceptionHandler;
  522.         $this->exceptionHandler null;
  523.         try {
  524.             if (null !== $exceptionHandler) {
  525.                 $exceptionHandler($exception);
  526.                 return;
  527.             }
  528.             $handlerException $handlerException ?: $exception;
  529.         } catch (\Throwable $handlerException) {
  530.         }
  531.         if ($exception === $handlerException) {
  532.             self::$reservedMemory null// Disable the fatal error handler
  533.             throw $exception// Give back $exception to the native handler
  534.         }
  535.         $this->handleException($handlerException);
  536.     }
  537.     /**
  538.      * Shutdown registered function for handling PHP fatal errors.
  539.      *
  540.      * @param array $error An array as returned by error_get_last()
  541.      *
  542.      * @internal
  543.      */
  544.     public static function handleFatalError(array $error null)
  545.     {
  546.         if (null === self::$reservedMemory) {
  547.             return;
  548.         }
  549.         $handler self::$reservedMemory null;
  550.         $handlers = [];
  551.         $previousHandler null;
  552.         $sameHandlerLimit 10;
  553.         while (!\is_array($handler) || !$handler[0] instanceof self) {
  554.             $handler set_exception_handler('var_dump');
  555.             restore_exception_handler();
  556.             if (!$handler) {
  557.                 break;
  558.             }
  559.             restore_exception_handler();
  560.             if ($handler !== $previousHandler) {
  561.                 array_unshift($handlers$handler);
  562.                 $previousHandler $handler;
  563.             } elseif (=== --$sameHandlerLimit) {
  564.                 $handler null;
  565.                 break;
  566.             }
  567.         }
  568.         foreach ($handlers as $h) {
  569.             set_exception_handler($h);
  570.         }
  571.         if (!$handler) {
  572.             return;
  573.         }
  574.         if ($handler !== $h) {
  575.             $handler[0]->setExceptionHandler($h);
  576.         }
  577.         $handler $handler[0];
  578.         $handlers = [];
  579.         if ($exit null === $error) {
  580.             $error error_get_last();
  581.         }
  582.         if ($error && $error['type'] &= E_PARSE E_ERROR E_CORE_ERROR E_COMPILE_ERROR) {
  583.             // Let's not throw anymore but keep logging
  584.             $handler->throwAt(0true);
  585.             $trace = isset($error['backtrace']) ? $error['backtrace'] : null;
  586.             if (=== strpos($error['message'], 'Allowed memory') || === strpos($error['message'], 'Out of memory')) {
  587.                 $exception = new OutOfMemoryException($handler->levels[$error['type']].': '.$error['message'], 0$error['type'], $error['file'], $error['line'], 2false$trace);
  588.             } else {
  589.                 $exception = new FatalErrorException($handler->levels[$error['type']].': '.$error['message'], 0$error['type'], $error['file'], $error['line'], 2true$trace);
  590.             }
  591.         } else {
  592.             $exception null;
  593.         }
  594.         try {
  595.             if (null !== $exception) {
  596.                 self::$exitCode 255;
  597.                 $handler->handleException($exception$error);
  598.             }
  599.         } catch (FatalErrorException $e) {
  600.             // Ignore this re-throw
  601.         }
  602.         if ($exit && self::$exitCode) {
  603.             $exitCode self::$exitCode;
  604.             register_shutdown_function('register_shutdown_function', function () use ($exitCode) { exit($exitCode); });
  605.         }
  606.     }
  607.     /**
  608.      * Gets the fatal error handlers.
  609.      *
  610.      * Override this method if you want to define more fatal error handlers.
  611.      *
  612.      * @return FatalErrorHandlerInterface[] An array of FatalErrorHandlerInterface
  613.      */
  614.     protected function getFatalErrorHandlers()
  615.     {
  616.         return [
  617.             new UndefinedFunctionFatalErrorHandler(),
  618.             new UndefinedMethodFatalErrorHandler(),
  619.             new ClassNotFoundFatalErrorHandler(),
  620.         ];
  621.     }
  622.     /**
  623.      * Cleans the trace by removing function arguments and the frames added by the error handler and DebugClassLoader.
  624.      */
  625.     private function cleanTrace(array $backtraceint $typestring $fileint $linebool $throw): array
  626.     {
  627.         $lightTrace $backtrace;
  628.         for ($i 0; isset($backtrace[$i]); ++$i) {
  629.             if (isset($backtrace[$i]['file'], $backtrace[$i]['line']) && $backtrace[$i]['line'] === $line && $backtrace[$i]['file'] === $file) {
  630.                 $lightTrace = \array_slice($lightTrace$i);
  631.                 break;
  632.             }
  633.         }
  634.         if (class_exists(DebugClassLoader::class, false)) {
  635.             for ($i = \count($lightTrace) - 2$i; --$i) {
  636.                 if (DebugClassLoader::class === ($lightTrace[$i]['class'] ?? null)) {
  637.                     array_splice($lightTrace, --$i2);
  638.                 }
  639.             }
  640.         }
  641.         if (!($throw || $this->scopedErrors $type)) {
  642.             for ($i 0; isset($lightTrace[$i]); ++$i) {
  643.                 unset($lightTrace[$i]['args'], $lightTrace[$i]['object']);
  644.             }
  645.         }
  646.         return $lightTrace;
  647.     }
  648. }