-<?php
+<?php declare(strict_types=1);
namespace PhpParser;
protected $text;
protected $line;
protected $filePos;
+ protected $tokenPos;
/**
* Constructs a comment node.
*
- * @param string $text Comment text (including comment delimiters like /*)
- * @param int $startLine Line number the comment started on
- * @param int $startFilePos File offset the comment started on
+ * @param string $text Comment text (including comment delimiters like /*)
+ * @param int $startLine Line number the comment started on
+ * @param int $startFilePos File offset the comment started on
+ * @param int $startTokenPos Token offset the comment started on
*/
- public function __construct($text, $startLine = -1, $startFilePos = -1) {
+ public function __construct(
+ string $text, int $startLine = -1, int $startFilePos = -1, int $startTokenPos = -1
+ ) {
$this->text = $text;
$this->line = $startLine;
$this->filePos = $startFilePos;
+ $this->tokenPos = $startTokenPos;
}
/**
*
* @return string The comment text (including comment delimiters like /*)
*/
- public function getText() {
+ public function getText() : string {
return $this->text;
}
*
* @return int Line number
*/
- public function getLine() {
+ public function getLine() : int {
return $this->line;
}
*
* @return int File offset
*/
- public function getFilePos() {
+ public function getFilePos() : int {
return $this->filePos;
}
+ /**
+ * Gets the token offset the comment started on.
+ *
+ * @return int Token offset
+ */
+ public function getTokenPos() : int {
+ return $this->tokenPos;
+ }
+
/**
* Gets the comment text.
*
* @return string The comment text (including comment delimiters like /*)
*/
- public function __toString() {
+ public function __toString() : string {
return $this->text;
}
return $text;
}
- private function getShortestWhitespacePrefixLen($str) {
+ /**
+ * Get length of shortest whitespace prefix (at the start of a line).
+ *
+ * If there is a line with no prefix whitespace, 0 is a valid return value.
+ *
+ * @param string $str String to check
+ * @return int Length in characters. Tabs count as single characters.
+ */
+ private function getShortestWhitespacePrefixLen(string $str) : int {
$lines = explode("\n", $str);
- $shortestPrefixLen = INF;
+ $shortestPrefixLen = \INF;
foreach ($lines as $line) {
preg_match('(^\s*)', $line, $matches);
$prefixLen = strlen($matches[0]);
return $shortestPrefixLen;
}
- public function jsonSerialize() {
+ /**
+ * @return array
+ * @psalm-return array{nodeType:string, text:mixed, line:mixed, filePos:mixed}
+ */
+ public function jsonSerialize() : array {
// Technically not a node, but we make it look like one anyway
$type = $this instanceof Comment\Doc ? 'Comment_Doc' : 'Comment';
return [
'text' => $this->text,
'line' => $this->line,
'filePos' => $this->filePos,
+ 'tokenPos' => $this->tokenPos,
];
}
-}
\ No newline at end of file
+}