Automattic\WooCommerce\Vendor\GraphQL\Error

FormattedError::highlightSourceAtLocationprivate staticWC 1.0

Render a helpful description of the location of the error in the Automattic\WooCommerce\Vendor\GraphQL Source document.

Method of the class: FormattedError{}

No Hooks.

Returns

null. Nothing (null).

Usage

$result = FormattedError::highlightSourceAtLocation( $source, $location ): string;
$source(Source) (required)
.
$location(SourceLocation) (required)
.

FormattedError::highlightSourceAtLocation() code WC 10.9.1

private static function highlightSourceAtLocation(Source $source, SourceLocation $location): string
{
    $line = $location->line;
    $lineOffset = $source->locationOffset->line - 1;
    $columnOffset = self::getColumnOffset($source, $location);
    $contextLine = $line + $lineOffset;
    $contextColumn = $location->column + $columnOffset;
    $prevLineNum = (string) ($contextLine - 1);
    $lineNum = (string) $contextLine;
    $nextLineNum = (string) ($contextLine + 1);
    $padLen = strlen($nextLineNum);

    $lines = Utils::splitLines($source->body);
    $lines[0] = self::spaces($source->locationOffset->column - 1) . $lines[0];

    $outputLines = [
        "{$source->name} ({$contextLine}:{$contextColumn})",
        $line >= 2 ? (self::leftPad($padLen, $prevLineNum) . ': ' . $lines[$line - 2]) : null,
        self::leftPad($padLen, $lineNum) . ': ' . $lines[$line - 1],
        self::spaces(2 + $padLen + $contextColumn - 1) . '^',
        $line < count($lines) ? self::leftPad($padLen, $nextLineNum) . ': ' . $lines[$line] : null,
    ];

    return implode("\n", array_filter($outputLines));
}