Skip to main content
Drupal API
User account menu
  • Log in

Breadcrumb

  1. Drupal Core 11.1.x
  2. Error.php

class Error

Same name in this branch
  1. 11.1.x vendor/phpunit/phpunit/src/Framework/TestStatus/Error.php \PHPUnit\Framework\TestStatus\Error
  2. 11.1.x vendor/nikic/php-parser/lib/PhpParser/Node/Expr/Error.php \PhpParser\Node\Expr\Error
  3. 11.1.x vendor/twig/twig/src/Error/Error.php \Twig\Error\Error
  4. 11.1.x core/lib/Drupal/Core/Utility/Error.php \Drupal\Core\Utility\Error

Hierarchy

  • class \PhpParser\Error extends \PhpParser\RuntimeException

Expanded class hierarchy of Error

10 files declare their use of Error
Calculator.php in vendor/sebastian/complexity/src/Calculator.php
Collecting.php in vendor/nikic/php-parser/lib/PhpParser/ErrorHandler/Collecting.php
Counter.php in vendor/sebastian/lines-of-code/src/Counter.php
Emulative.php in vendor/nikic/php-parser/lib/PhpParser/Lexer/Emulative.php
Int_.php in vendor/nikic/php-parser/lib/PhpParser/Node/Scalar/Int_.php

... See full list

60 string references to 'Error'
AbstractWebDriver::curl in vendor/lullabot/php-webdriver/lib/WebDriver/AbstractWebDriver.php
Curl request to webdriver server.
Application::doRun in vendor/symfony/console/Application.php
Runs the current application.
Auditor::audit in vendor/composer/composer/src/Composer/Advisory/Auditor.php
Checkstyle::generateFileReport in vendor/squizlabs/php_codesniffer/src/Reports/Checkstyle.php
Generate a partial report for a single processed file.
Code::generateFileReport in vendor/squizlabs/php_codesniffer/src/Reports/Code.php
Generate a partial report for a single processed file.

... See full list

File

vendor/nikic/php-parser/lib/PhpParser/Error.php, line 5

Namespace

PhpParser
View source
class Error extends \RuntimeException {
    protected string $rawMessage;
    
    /** @var array<string, mixed> */
    protected array $attributes;
    
    /**
     * Creates an Exception signifying a parse error.
     *
     * @param string $message Error message
     * @param array<string, mixed> $attributes Attributes of node/token where error occurred
     */
    public function __construct(string $message, array $attributes = []) {
        $this->rawMessage = $message;
        $this->attributes = $attributes;
        $this->updateMessage();
    }
    
    /**
     * Gets the error message
     *
     * @return string Error message
     */
    public function getRawMessage() : string {
        return $this->rawMessage;
    }
    
    /**
     * Gets the line the error starts in.
     *
     * @return int Error start line
     * @phpstan-return -1|positive-int
     */
    public function getStartLine() : int {
        return $this->attributes['startLine'] ?? -1;
    }
    
    /**
     * Gets the line the error ends in.
     *
     * @return int Error end line
     * @phpstan-return -1|positive-int
     */
    public function getEndLine() : int {
        return $this->attributes['endLine'] ?? -1;
    }
    
    /**
     * Gets the attributes of the node/token the error occurred at.
     *
     * @return array<string, mixed>
     */
    public function getAttributes() : array {
        return $this->attributes;
    }
    
    /**
     * Sets the attributes of the node/token the error occurred at.
     *
     * @param array<string, mixed> $attributes
     */
    public function setAttributes(array $attributes) : void {
        $this->attributes = $attributes;
        $this->updateMessage();
    }
    
    /**
     * Sets the line of the PHP file the error occurred in.
     *
     * @param string $message Error message
     */
    public function setRawMessage(string $message) : void {
        $this->rawMessage = $message;
        $this->updateMessage();
    }
    
    /**
     * Sets the line the error starts in.
     *
     * @param int $line Error start line
     */
    public function setStartLine(int $line) : void {
        $this->attributes['startLine'] = $line;
        $this->updateMessage();
    }
    
    /**
     * Returns whether the error has start and end column information.
     *
     * For column information enable the startFilePos and endFilePos in the lexer options.
     */
    public function hasColumnInfo() : bool {
        return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
    }
    
    /**
     * Gets the start column (1-based) into the line where the error started.
     *
     * @param string $code Source code of the file
     */
    public function getStartColumn(string $code) : int {
        if (!$this->hasColumnInfo()) {
            throw new \RuntimeException('Error does not have column information');
        }
        return $this->toColumn($code, $this->attributes['startFilePos']);
    }
    
    /**
     * Gets the end column (1-based) into the line where the error ended.
     *
     * @param string $code Source code of the file
     */
    public function getEndColumn(string $code) : int {
        if (!$this->hasColumnInfo()) {
            throw new \RuntimeException('Error does not have column information');
        }
        return $this->toColumn($code, $this->attributes['endFilePos']);
    }
    
    /**
     * Formats message including line and column information.
     *
     * @param string $code Source code associated with the error, for calculation of the columns
     *
     * @return string Formatted message
     */
    public function getMessageWithColumnInfo(string $code) : string {
        return sprintf('%s from %d:%d to %d:%d', $this->getRawMessage(), $this->getStartLine(), $this->getStartColumn($code), $this->getEndLine(), $this->getEndColumn($code));
    }
    
    /**
     * Converts a file offset into a column.
     *
     * @param string $code Source code that $pos indexes into
     * @param int $pos 0-based position in $code
     *
     * @return int 1-based column (relative to start of line)
     */
    private function toColumn(string $code, int $pos) : int {
        if ($pos > strlen($code)) {
            throw new \RuntimeException('Invalid position information');
        }
        $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
        if (false === $lineStartPos) {
            $lineStartPos = -1;
        }
        return $pos - $lineStartPos;
    }
    
    /**
     * Updates the exception message after a change to rawMessage or rawLine.
     */
    protected function updateMessage() : void {
        $this->message = $this->rawMessage;
        if (-1 === $this->getStartLine()) {
            $this->message .= ' on unknown line';
        }
        else {
            $this->message .= ' on line ' . $this->getStartLine();
        }
    }

}

Members

Title Sort descending Modifiers Object type Summary
Error::$attributes protected property @var array&lt;string, mixed&gt;
Error::$rawMessage protected property
Error::getAttributes public function Gets the attributes of the node/token the error occurred at.
Error::getEndColumn public function Gets the end column (1-based) into the line where the error ended.
Error::getEndLine public function Gets the line the error ends in.
Error::getMessageWithColumnInfo public function Formats message including line and column information.
Error::getRawMessage public function Gets the error message
Error::getStartColumn public function Gets the start column (1-based) into the line where the error started.
Error::getStartLine public function Gets the line the error starts in.
Error::hasColumnInfo public function Returns whether the error has start and end column information.
Error::setAttributes public function Sets the attributes of the node/token the error occurred at.
Error::setRawMessage public function Sets the line of the PHP file the error occurred in.
Error::setStartLine public function Sets the line the error starts in.
Error::toColumn private function Converts a file offset into a column.
Error::updateMessage protected function Updates the exception message after a change to rawMessage or rawLine.
Error::__construct public function Creates an Exception signifying a parse error.

API Navigation

  • Drupal Core 11.1.x
  • Topics
  • Classes
  • Functions
  • Constants
  • Globals
  • Files
  • Namespaces
  • Deprecated
  • Services
RSS feed
Powered by Drupal