-<?php
+<?php declare(strict_types=1);
namespace PhpParser;
* @param array|int $attributes Attributes of node/token where error occurred
* (or start line of error -- deprecated)
*/
- public function __construct($message, $attributes = array()) {
+ public function __construct(string $message, $attributes = []) {
$this->rawMessage = (string) $message;
if (is_array($attributes)) {
$this->attributes = $attributes;
} else {
- $this->attributes = array('startLine' => $attributes);
+ $this->attributes = ['startLine' => $attributes];
}
$this->updateMessage();
}
*
* @return string Error message
*/
- public function getRawMessage() {
+ public function getRawMessage() : string {
return $this->rawMessage;
}
*
* @return int Error start line
*/
- public function getStartLine() {
- return isset($this->attributes['startLine']) ? $this->attributes['startLine'] : -1;
+ public function getStartLine() : int {
+ return $this->attributes['startLine'] ?? -1;
}
/**
*
* @return int Error end line
*/
- public function getEndLine() {
- return isset($this->attributes['endLine']) ? $this->attributes['endLine'] : -1;
+ public function getEndLine() : int {
+ return $this->attributes['endLine'] ?? -1;
}
-
/**
* Gets the attributes of the node/token the error occurred at.
*
* @return array
*/
- public function getAttributes() {
+ public function getAttributes() : array {
return $this->attributes;
}
/**
- * Sets the attributes of the node/token the error occured at.
+ * Sets the attributes of the node/token the error occurred at.
*
* @param array $attributes
*/
*
* @param string $message Error message
*/
- public function setRawMessage($message) {
+ public function setRawMessage(string $message) {
$this->rawMessage = (string) $message;
$this->updateMessage();
}
*
* @param int $line Error start line
*/
- public function setStartLine($line) {
+ public function setStartLine(int $line) {
$this->attributes['startLine'] = (int) $line;
$this->updateMessage();
}
*
* @return bool
*/
- public function hasColumnInfo() {
- return isset($this->attributes['startFilePos']) && isset($this->attributes['endFilePos']);
+ public function hasColumnInfo() : bool {
+ return isset($this->attributes['startFilePos'], $this->attributes['endFilePos']);
}
/**
* @param string $code Source code of the file
* @return int
*/
- public function getStartColumn($code) {
+ public function getStartColumn(string $code) : int {
if (!$this->hasColumnInfo()) {
throw new \RuntimeException('Error does not have column information');
}
* @param string $code Source code of the file
* @return int
*/
- public function getEndColumn($code) {
+ 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']);
}
- public function getMessageWithColumnInfo($code) {
+ /**
+ * 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),
);
}
- private function toColumn($code, $pos) {
+ /**
+ * 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');
}