function Error::formatBacktrace

Same name in other branches
  1. 8.9.x core/lib/Drupal/Core/Utility/Error.php \Drupal\Core\Utility\Error::formatBacktrace()
  2. 10 core/lib/Drupal/Core/Utility/Error.php \Drupal\Core\Utility\Error::formatBacktrace()
  3. 11.x core/lib/Drupal/Core/Utility/Error.php \Drupal\Core\Utility\Error::formatBacktrace()

Formats a backtrace into a plain-text string.

The calls show values for scalar arguments and type names for complex ones.

Parameters

array $backtrace: A standard PHP backtrace.

Return value

string A plain-text line-wrapped string ready to be put inside <pre>.

5 calls to Error::formatBacktrace()
Error::renderExceptionSafe in core/lib/Drupal/Core/Utility/Error.php
Renders an exception error message without further exceptions.
ErrorTest::testFormatBacktrace in core/tests/Drupal/Tests/Core/Utility/ErrorTest.php
Tests the formatBacktrace() method.
FinalExceptionSubscriber::onException in core/lib/Drupal/Core/EventSubscriber/FinalExceptionSubscriber.php
Handles exceptions for this subscriber.
TestHttpClientMiddleware::__invoke in core/lib/Drupal/Core/Test/HttpClientMiddleware/TestHttpClientMiddleware.php
HTTP middleware that replaces the user agent for simpletest requests.
_drupal_log_error in core/includes/errors.inc
Logs a PHP error or exception and displays an error page in fatal cases.

File

core/lib/Drupal/Core/Utility/Error.php, line 151

Class

Error
Drupal error utility class.

Namespace

Drupal\Core\Utility

Code

public static function formatBacktrace(array $backtrace) {
    $return = '';
    foreach ($backtrace as $trace) {
        $call = [
            'function' => '',
            'args' => [],
        ];
        if (isset($trace['class'])) {
            $call['function'] = $trace['class'] . $trace['type'] . $trace['function'];
        }
        elseif (isset($trace['function'])) {
            $call['function'] = $trace['function'];
        }
        else {
            $call['function'] = 'main';
        }
        if (isset($trace['args'])) {
            foreach ($trace['args'] as $arg) {
                if (is_scalar($arg)) {
                    $call['args'][] = is_string($arg) ? '\'' . Xss::filter($arg) . '\'' : $arg;
                }
                else {
                    $call['args'][] = ucfirst(gettype($arg));
                }
            }
        }
        $line = '';
        if (isset($trace['line'])) {
            $line = " (Line: {$trace['line']})";
        }
        $return .= $call['function'] . '(' . implode(', ', $call['args']) . "){$line}\n";
    }
    return $return;
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.