From 37b92818294d6983c54b5168ec848c9c3b353223 Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 30 Aug 2022 14:38:32 +0300 Subject: [PATCH 1/2] Fix Token methods Fix result in cases when `pos` and/or `line` not set --- lib/PhpParser/Token.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/PhpParser/Token.php b/lib/PhpParser/Token.php index 6683310f16..9af7005c05 100644 --- a/lib/PhpParser/Token.php +++ b/lib/PhpParser/Token.php @@ -8,11 +8,17 @@ class Token extends Internal\TokenPolyfill { /** Get (exclusive) zero-based end position of the token. */ public function getEndPos(): int { - return $this->pos + \strlen($this->text); + return $this->pos < 0 ? -1 : $this->pos + \strlen($this->text); } /** Get 1-based end line number of the token. */ public function getEndLine(): int { + if ($this->line < 1) { + return -1; + } + if (!isset($this->text[1])) { + return $this->line; + } return $this->line + \substr_count($this->text, "\n"); } } From 5b691163ae360de7fa63f9b2a5c3fc48db4cd75d Mon Sep 17 00:00:00 2001 From: Anton Date: Tue, 30 Aug 2022 14:39:45 +0300 Subject: [PATCH 2/2] Update Token.php --- lib/PhpParser/Token.php | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/lib/PhpParser/Token.php b/lib/PhpParser/Token.php index 9af7005c05..47fbab4044 100644 --- a/lib/PhpParser/Token.php +++ b/lib/PhpParser/Token.php @@ -8,15 +8,12 @@ class Token extends Internal\TokenPolyfill { /** Get (exclusive) zero-based end position of the token. */ public function getEndPos(): int { - return $this->pos < 0 ? -1 : $this->pos + \strlen($this->text); + return $this->pos < 0 ? $this->pos : $this->pos + \strlen($this->text); } /** Get 1-based end line number of the token. */ public function getEndLine(): int { - if ($this->line < 1) { - return -1; - } - if (!isset($this->text[1])) { + if ($this->line < 1 || !isset($this->text[1])) { return $this->line; } return $this->line + \substr_count($this->text, "\n");