Skip to content

Commit 91f7796

Browse files
committed
Add Node property overloads for accessing attributes
Node attributes are now also accessible as ordinary properties. Accessing an undefined attribute throws -- the getAttribute() default value behavior can be replicated using coalescing. @Property declarations for all the standard attributes have been added to the relevant nodes. Of course, whether they are actually available depends on configuration.
1 parent 4e25f51 commit 91f7796

File tree

13 files changed

+135
-9
lines changed

13 files changed

+135
-9
lines changed

lib/PhpParser/Node.php

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,20 @@
22

33
namespace PhpParser;
44

5+
/**
6+
* AST node.
7+
*
8+
* The following attributes are available depending on the 'usedAttributes' option passed to the
9+
* lexer.
10+
*
11+
* @property Comment[] $comments Comments preceding this node
12+
* @property int $startLine Line the node starts at (1-based)
13+
* @property int $endLine Line the node end at (1-based)
14+
* @property int $startFilePos File offset the node starts at (0-based)
15+
* @property int $endFilePos File offset the node ends at (0-based)
16+
* @property int $startTokenPos Token offset the node starts at (0-based)
17+
* @property int $endTokenPos Token offset the node ends at (0-based)
18+
*/
519
interface Node
620
{
721
/**
@@ -41,6 +55,15 @@ public function setLine($line);
4155
*/
4256
public function getDocComment();
4357

58+
/**
59+
* Sets the doc comment of the node.
60+
*
61+
* This will either replace an existing doc comment or add it to the comments array.
62+
*
63+
* @param Comment\Doc $docComment Doc comment to set
64+
*/
65+
public function setDocComment(Comment\Doc $docComment);
66+
4467
/**
4568
* Sets an attribute on a node.
4669
*
@@ -74,4 +97,37 @@ public function &getAttribute($key, $default = null);
7497
* @return array
7598
*/
7699
public function getAttributes();
100+
101+
/**
102+
* Get the value of an attribute.
103+
*
104+
* @param string $key Name of the attribute
105+
*
106+
* @return mixed Value of the attribute
107+
*/
108+
public function &__get($key);
109+
110+
/**
111+
* Sets the value of an attribute.
112+
*
113+
* @param string $key Name of the attribute
114+
* @param mixed $value Value to set
115+
*/
116+
public function __set($key, $value);
117+
118+
/**
119+
* Check whether an attribute exists and is non-null.
120+
*
121+
* @param string $key Name of the attribute
122+
*
123+
* @return bool Whether the attribute exists and is non-null
124+
*/
125+
public function __isset($key);
126+
127+
/**
128+
* Removes an attribute.
129+
*
130+
* @param string $key Name of the attribute.
131+
*/
132+
public function __unset($key);
77133
}

lib/PhpParser/Node/Const_.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use PhpParser\NodeAbstract;
66

7+
/**
8+
* @property Name $namespacedName Namespace-prefixed name (requires NameResolver). This only applies
9+
* to freestanding (non-class) constants.
10+
*/
711
class Const_ extends NodeAbstract
812
{
913
/** @var string Name */

lib/PhpParser/Node/Expr/Array_.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node\Expr;
66

7+
/**
8+
* @property int $kind One of the KIND_* class constants
9+
*/
710
class Array_ extends Expr
811
{
912
// For use in "kind" attribute

lib/PhpParser/Node/Expr/Exit_.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node\Expr;
66

7+
/**
8+
* @property int $kind One of the KIND_* class constants
9+
*/
710
class Exit_ extends Expr
811
{
912
/* For use in "kind" attribute */

lib/PhpParser/Node/Scalar/Encapsed.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@
44

55
use PhpParser\Node\Scalar;
66

7+
/**
8+
* @property int $kind One of the String_::KIND_* class constants
9+
* @property string $docLabel Label of doc comment (only available if string defined as doc string)
10+
*/
711
class Encapsed extends Scalar
812
{
913
/** @var array Encaps list */

lib/PhpParser/Node/Scalar/LNumber.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use PhpParser\Error;
66
use PhpParser\Node\Scalar;
77

8+
/**
9+
* @property int $kind One of the KIND_* class constants
10+
*/
811
class LNumber extends Scalar
912
{
1013
/* For use in "kind" attribute */

lib/PhpParser/Node/Scalar/String_.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,10 @@
55
use PhpParser\Error;
66
use PhpParser\Node\Scalar;
77

8+
/**
9+
* @property int $kind One of the KIND_* class constants
10+
* @property string $docLabel Label of doc comment (only available if string defined as doc string)
11+
*/
812
class String_ extends Scalar
913
{
1014
/* For use in "kind" attribute */

lib/PhpParser/Node/Stmt/ClassLike.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node;
66

7+
/**
8+
* @property Node\Name $namespacedName Namespace-prefixed name (requires NameResolver)
9+
*/
710
abstract class ClassLike extends Node\Stmt {
811
/** @var string Name */
912
public $name;

lib/PhpParser/Node/Stmt/Function_.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
use PhpParser\Node;
66
use PhpParser\Node\FunctionLike;
77

8+
/**
9+
* @property Node\Name $namespacedName Namespace-prefixed name (requires NameResolver)
10+
*/
811
class Function_ extends Node\Stmt implements FunctionLike
912
{
1013
/** @var bool Whether function returns by reference */

lib/PhpParser/Node/Stmt/InlineHTML.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44

55
use PhpParser\Node\Stmt;
66

7+
/**
8+
* @property bool $hasLeadingNewline Whether the inline has a leading newline that has been ignored
9+
*/
710
class InlineHTML extends Stmt
811
{
912
/** @var string String */

0 commit comments

Comments
 (0)