Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
CRAP
0.00% covered (danger)
0.00%
0 / 23
ShellSyntaxError
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 4
30
0.00% covered (danger)
0.00%
0 / 23
 __construct
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 7
 enableContext
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 disableContext
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 3
 updateMessage
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 10
<?php
namespace Shellbox\ShellParser;
use Shellbox\ShellboxError;
use Wikimedia\WikiPEG\Location;
/**
 * Exception class for shell command syntax errors. By default the message uses
 * multiple lines to show the exact location of the syntax error.
 */
class ShellSyntaxError extends ShellboxError {
    /** @var string */
    private $originalMessage;
    /** @var Location */
    private $location;
    /** @var string */
    private $input;
    /** @var bool */
    private $contextEnabled;
    /**
     * @internal
     * @param string $message The message from the PEG
     * @param Location $location The error location
     * @param string $input The complete input text
     */
    public function __construct( $message, Location $location, $input ) {
        parent::__construct( $message );
        $this->originalMessage = $message;
        $this->location = $location;
        $this->input = $input;
        $this->contextEnabled = true;
        $this->updateMessage();
    }
    /**
     * Enable multi-line context
     */
    public function enableContext() {
        $this->contextEnabled = true;
        $this->updateMessage();
    }
    /**
     * Disable multi-line context
     */
    public function disableContext() {
        $this->contextEnabled = false;
        $this->updateMessage();
    }
    /**
     * Update the message (stored in the parent) according to the contextEnabled
     * setting.
     */
    private function updateMessage() {
        if ( !$this->contextEnabled ) {
            $this->message = $this->originalMessage;
            return;
        }
        $lines = explode( "\n", $this->input );
        $line = $lines[$this->location->line - 1] ?? '';
        $this->message = $this->originalMessage . "\n" .
            $line . "\n" .
            str_repeat( ' ', $this->location->column - 1 ) . '^';
    }
}