Prevent off-by-one errors in line-number related methods

This commit is contained in:
Markus Staab 2024-03-13 11:49:39 +01:00 committed by Nikita Popov
parent 139676794d
commit 09691fc86e
4 changed files with 10 additions and 0 deletions

View File

@ -46,6 +46,7 @@ class Comment implements \JsonSerializable {
* Gets the line number the comment started on.
*
* @return int Line number (or -1 if not available)
* @phpstan-return -1|positive-int
*/
public function getStartLine(): int {
return $this->startLine;
@ -73,6 +74,7 @@ class Comment implements \JsonSerializable {
* Gets the line number the comment ends on.
*
* @return int Line number (or -1 if not available)
* @phpstan-return -1|positive-int
*/
public function getEndLine(): int {
return $this->endLine;

View File

@ -32,6 +32,7 @@ class Error extends \RuntimeException {
* 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;
@ -41,6 +42,7 @@ class Error extends \RuntimeException {
* 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;

View File

@ -21,6 +21,7 @@ interface Node {
* Gets line the node started in (alias of getStartLine).
*
* @return int Start line (or -1 if not available)
* @phpstan-return -1|positive-int
*
* @deprecated Use getStartLine() instead
*/
@ -32,6 +33,7 @@ interface Node {
* Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int Start line (or -1 if not available)
* @phpstan-return -1|positive-int
*/
public function getStartLine(): int;
@ -41,6 +43,7 @@ interface Node {
* Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int End line (or -1 if not available)
* @phpstan-return -1|positive-int
*/
public function getEndLine(): int;

View File

@ -19,6 +19,7 @@ abstract class NodeAbstract implements Node, \JsonSerializable {
* Gets line the node started in (alias of getStartLine).
*
* @return int Start line (or -1 if not available)
* @phpstan-return -1|positive-int
*/
public function getLine(): int {
return $this->attributes['startLine'] ?? -1;
@ -30,6 +31,7 @@ abstract class NodeAbstract implements Node, \JsonSerializable {
* Requires the 'startLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int Start line (or -1 if not available)
* @phpstan-return -1|positive-int
*/
public function getStartLine(): int {
return $this->attributes['startLine'] ?? -1;
@ -41,6 +43,7 @@ abstract class NodeAbstract implements Node, \JsonSerializable {
* Requires the 'endLine' attribute to be enabled in the lexer (enabled by default).
*
* @return int End line (or -1 if not available)
* @phpstan-return -1|positive-int
*/
public function getEndLine(): int {
return $this->attributes['endLine'] ?? -1;