Skip to content

Commit 6d546ce

Browse files
committed
Support attributes on traits and interfaces
They were already supported on ClassDeclaration. The php runtime allows attributes on all classlikes, including traits/interfaces.
1 parent 4e9127c commit 6d546ce

38 files changed

+265
-0
lines changed

src/Node/Statement/InterfaceDeclaration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Microsoft\PhpParser\ClassLike;
1010
use Microsoft\PhpParser\NamespacedNameInterface;
1111
use Microsoft\PhpParser\NamespacedNameTrait;
12+
use Microsoft\PhpParser\Node\AttributeGroup;
1213
use Microsoft\PhpParser\Node\InterfaceBaseClause;
1314
use Microsoft\PhpParser\Node\InterfaceMembers;
1415
use Microsoft\PhpParser\Node\StatementNode;
@@ -17,6 +18,9 @@
1718
class InterfaceDeclaration extends StatementNode implements NamespacedNameInterface, ClassLike {
1819
use NamespacedNameTrait;
1920

21+
/** @var AttributeGroup[]|null */
22+
public $attributes;
23+
2024
/** @var Token */
2125
public $interfaceKeyword;
2226

@@ -30,6 +34,7 @@ class InterfaceDeclaration extends StatementNode implements NamespacedNameInterf
3034
public $interfaceMembers;
3135

3236
const CHILD_NAMES = [
37+
'attributes',
3338
'interfaceKeyword',
3439
'name',
3540
'interfaceBaseClause',

src/Node/Statement/TraitDeclaration.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,17 @@
99
use Microsoft\PhpParser\ClassLike;
1010
use Microsoft\PhpParser\NamespacedNameInterface;
1111
use Microsoft\PhpParser\NamespacedNameTrait;
12+
use Microsoft\PhpParser\Node\AttributeGroup;
1213
use Microsoft\PhpParser\Node\StatementNode;
1314
use Microsoft\PhpParser\Node\TraitMembers;
1415
use Microsoft\PhpParser\Token;
1516

1617
class TraitDeclaration extends StatementNode implements NamespacedNameInterface, ClassLike {
1718
use NamespacedNameTrait;
1819

20+
/** @var AttributeGroup[]|null */
21+
public $attributes;
22+
1923
/** @var Token */
2024
public $traitKeyword;
2125

@@ -26,6 +30,7 @@ class TraitDeclaration extends StatementNode implements NamespacedNameInterface,
2630
public $traitMembers;
2731

2832
const CHILD_NAMES = [
33+
'attributes',
2934
'traitKeyword',
3035
'name',
3136
'traitMembers'

src/Parser.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -726,6 +726,8 @@ private function parseAttributeStatement($parentNode) {
726726

727727
if ($statement instanceof FunctionLike ||
728728
$statement instanceof ClassDeclaration ||
729+
$statement instanceof TraitDeclaration ||
730+
$statement instanceof InterfaceDeclaration ||
729731
$statement instanceof ClassConstDeclaration ||
730732
$statement instanceof PropertyDeclaration ||
731733
$statement instanceof MissingDeclaration ||

tests/cases/parser/interfaceDeclaration1.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
{
1515
"InterfaceDeclaration": {
16+
"attributes": null,
1617
"interfaceKeyword": {
1718
"kind": "InterfaceKeyword",
1819
"textLength": 9

tests/cases/parser/interfaceDeclaration10.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
{
1515
"InterfaceDeclaration": {
16+
"attributes": null,
1617
"interfaceKeyword": {
1718
"kind": "InterfaceKeyword",
1819
"textLength": 9

tests/cases/parser/interfaceDeclaration11.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
{
1515
"InterfaceDeclaration": {
16+
"attributes": null,
1617
"interfaceKeyword": {
1718
"kind": "InterfaceKeyword",
1819
"textLength": 9

tests/cases/parser/interfaceDeclaration12.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
{
1515
"InterfaceDeclaration": {
16+
"attributes": null,
1617
"interfaceKeyword": {
1718
"kind": "InterfaceKeyword",
1819
"textLength": 9

tests/cases/parser/interfaceDeclaration13.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
{
1515
"InterfaceDeclaration": {
16+
"attributes": null,
1617
"interfaceKeyword": {
1718
"kind": "InterfaceKeyword",
1819
"textLength": 9

tests/cases/parser/interfaceDeclaration14.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
{
1515
"InterfaceDeclaration": {
16+
"attributes": null,
1617
"interfaceKeyword": {
1718
"kind": "InterfaceKeyword",
1819
"textLength": 9

tests/cases/parser/interfaceDeclaration15.php.tree

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
{
1515
"InterfaceDeclaration": {
16+
"attributes": null,
1617
"interfaceKeyword": {
1718
"kind": "InterfaceKeyword",
1819
"textLength": 9

0 commit comments

Comments
 (0)