annotate vendor/nikic/php-parser/lib/PhpParser/Error.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents c2387f117808
children
rev   line source
Chris@13 1 <?php declare(strict_types=1);
Chris@0 2
Chris@0 3 namespace PhpParser;
Chris@0 4
Chris@0 5 class Error extends \RuntimeException
Chris@0 6 {
Chris@0 7 protected $rawMessage;
Chris@0 8 protected $attributes;
Chris@0 9
Chris@0 10 /**
Chris@0 11 * Creates an Exception signifying a parse error.
Chris@0 12 *
Chris@0 13 * @param string $message Error message
Chris@0 14 * @param array|int $attributes Attributes of node/token where error occurred
Chris@0 15 * (or start line of error -- deprecated)
Chris@0 16 */
Chris@13 17 public function __construct(string $message, $attributes = []) {
Chris@16 18 $this->rawMessage = $message;
Chris@0 19 if (is_array($attributes)) {
Chris@0 20 $this->attributes = $attributes;
Chris@0 21 } else {
Chris@13 22 $this->attributes = ['startLine' => $attributes];
Chris@0 23 }
Chris@0 24 $this->updateMessage();
Chris@0 25 }
Chris@0 26
Chris@0 27 /**
Chris@0 28 * Gets the error message
Chris@0 29 *
Chris@0 30 * @return string Error message
Chris@0 31 */
Chris@13 32 public function getRawMessage() : string {
Chris@0 33 return $this->rawMessage;
Chris@0 34 }
Chris@0 35
Chris@0 36 /**
Chris@0 37 * Gets the line the error starts in.
Chris@0 38 *
Chris@0 39 * @return int Error start line
Chris@0 40 */
Chris@13 41 public function getStartLine() : int {
Chris@13 42 return $this->attributes['startLine'] ?? -1;
Chris@0 43 }
Chris@0 44
Chris@0 45 /**
Chris@0 46 * Gets the line the error ends in.
Chris@0 47 *
Chris@0 48 * @return int Error end line
Chris@0 49 */
Chris@13 50 public function getEndLine() : int {
Chris@13 51 return $this->attributes['endLine'] ?? -1;
Chris@0 52 }
Chris@0 53
Chris@0 54 /**
Chris@0 55 * Gets the attributes of the node/token the error occurred at.
Chris@0 56 *
Chris@0 57 * @return array
Chris@0 58 */
Chris@13 59 public function getAttributes() : array {
Chris@0 60 return $this->attributes;
Chris@0 61 }
Chris@0 62
Chris@0 63 /**
Chris@13 64 * Sets the attributes of the node/token the error occurred at.
Chris@0 65 *
Chris@0 66 * @param array $attributes
Chris@0 67 */
Chris@0 68 public function setAttributes(array $attributes) {
Chris@0 69 $this->attributes = $attributes;
Chris@0 70 $this->updateMessage();
Chris@0 71 }
Chris@0 72
Chris@0 73 /**
Chris@0 74 * Sets the line of the PHP file the error occurred in.
Chris@0 75 *
Chris@0 76 * @param string $message Error message
Chris@0 77 */
Chris@13 78 public function setRawMessage(string $message) {
Chris@16 79 $this->rawMessage = $message;
Chris@0 80 $this->updateMessage();
Chris@0 81 }
Chris@0 82
Chris@0 83 /**
Chris@0 84 * Sets the line the error starts in.
Chris@0 85 *
Chris@0 86 * @param int $line Error start line
Chris@0 87 */
Chris@13 88 public function setStartLine(int $line) {
Chris@16 89 $this->attributes['startLine'] = $line;
Chris@0 90 $this->updateMessage();
Chris@0 91 }
Chris@0 92
Chris@0 93 /**
Chris@0 94 * Returns whether the error has start and end column information.
Chris@0 95 *
Chris@0 96 * For column information enable the startFilePos and endFilePos in the lexer options.
Chris@0 97 *
Chris@0 98 * @return bool
Chris@0 99 */
Chris@13 100 public function hasColumnInfo() : bool {
Chris@13 101 return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
Chris@0 102 }
Chris@0 103
Chris@0 104 /**
Chris@0 105 * Gets the start column (1-based) into the line where the error started.
Chris@0 106 *
Chris@0 107 * @param string $code Source code of the file
Chris@0 108 * @return int
Chris@0 109 */
Chris@13 110 public function getStartColumn(string $code) : int {
Chris@0 111 if (!$this->hasColumnInfo()) {
Chris@0 112 throw new \RuntimeException('Error does not have column information');
Chris@0 113 }
Chris@0 114
Chris@0 115 return $this->toColumn($code, $this->attributes['startFilePos']);
Chris@0 116 }
Chris@0 117
Chris@0 118 /**
Chris@0 119 * Gets the end column (1-based) into the line where the error ended.
Chris@0 120 *
Chris@0 121 * @param string $code Source code of the file
Chris@0 122 * @return int
Chris@0 123 */
Chris@13 124 public function getEndColumn(string $code) : int {
Chris@0 125 if (!$this->hasColumnInfo()) {
Chris@0 126 throw new \RuntimeException('Error does not have column information');
Chris@0 127 }
Chris@0 128
Chris@0 129 return $this->toColumn($code, $this->attributes['endFilePos']);
Chris@0 130 }
Chris@0 131
Chris@13 132 /**
Chris@13 133 * Formats message including line and column information.
Chris@13 134 *
Chris@13 135 * @param string $code Source code associated with the error, for calculation of the columns
Chris@13 136 *
Chris@13 137 * @return string Formatted message
Chris@13 138 */
Chris@13 139 public function getMessageWithColumnInfo(string $code) : string {
Chris@0 140 return sprintf(
Chris@0 141 '%s from %d:%d to %d:%d', $this->getRawMessage(),
Chris@0 142 $this->getStartLine(), $this->getStartColumn($code),
Chris@0 143 $this->getEndLine(), $this->getEndColumn($code)
Chris@0 144 );
Chris@0 145 }
Chris@0 146
Chris@13 147 /**
Chris@13 148 * Converts a file offset into a column.
Chris@13 149 *
Chris@13 150 * @param string $code Source code that $pos indexes into
Chris@13 151 * @param int $pos 0-based position in $code
Chris@13 152 *
Chris@13 153 * @return int 1-based column (relative to start of line)
Chris@13 154 */
Chris@13 155 private function toColumn(string $code, int $pos) : int {
Chris@0 156 if ($pos > strlen($code)) {
Chris@0 157 throw new \RuntimeException('Invalid position information');
Chris@0 158 }
Chris@0 159
Chris@0 160 $lineStartPos = strrpos($code, "\n", $pos - strlen($code));
Chris@0 161 if (false === $lineStartPos) {
Chris@0 162 $lineStartPos = -1;
Chris@0 163 }
Chris@0 164
Chris@0 165 return $pos - $lineStartPos;
Chris@0 166 }
Chris@0 167
Chris@0 168 /**
Chris@0 169 * Updates the exception message after a change to rawMessage or rawLine.
Chris@0 170 */
Chris@0 171 protected function updateMessage() {
Chris@0 172 $this->message = $this->rawMessage;
Chris@0 173
Chris@0 174 if (-1 === $this->getStartLine()) {
Chris@0 175 $this->message .= ' on unknown line';
Chris@0 176 } else {
Chris@0 177 $this->message .= ' on line ' . $this->getStartLine();
Chris@0 178 }
Chris@0 179 }
Chris@0 180 }