When a class, function, or method has PHP attributes, getStartLine() returns the line of the first attribute rather than the line of the declaration keyword (class, function, visibility modifier, etc.).
<?php declare(strict_types=1);
final class Example
{
#[Deprecated]
public function method(): string
{
return 'foo';
}
}
For the ClassMethod node representing method(), getStartLine() returns 4 (the #[Deprecated] line) instead of 5 (the public function line).
This might be technically correct since the attributes are part of the node, but there is no convenient way to get the start line of the declaration itself (excluding attributes). The current workaround is to use $node->name->getStartLine(), which happens to work because the name identifier is always on the declaration line. However, this feels like an implementation detail rather than a supported API.
Would it make sense to provide a method like getDeclarationStartLine() (or similar) on nodes that support attributes (Class_, Enum_, Interface_, Trait_, ClassMethod, Function_) that returns the start line of the declaration keyword, excluding any preceding attribute groups?
Alternatively, if $node->name->getStartLine() is considered the intended way to achieve this, it would be helpful to document that.
When a class, function, or method has PHP attributes,
getStartLine()returns the line of the first attribute rather than the line of the declaration keyword (class,function, visibility modifier, etc.).For the
ClassMethodnode representingmethod(),getStartLine()returns4(the#[Deprecated]line) instead of5(thepublic functionline).This might be technically correct since the attributes are part of the node, but there is no convenient way to get the start line of the declaration itself (excluding attributes). The current workaround is to use
$node->name->getStartLine(), which happens to work because the name identifier is always on the declaration line. However, this feels like an implementation detail rather than a supported API.Would it make sense to provide a method like
getDeclarationStartLine()(or similar) on nodes that support attributes (Class_,Enum_,Interface_,Trait_,ClassMethod,Function_) that returns the start line of the declaration keyword, excluding any preceding attribute groups?Alternatively, if
$node->name->getStartLine()is considered the intended way to achieve this, it would be helpful to document that.