Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,10 @@ top_statement:
| T_USE use_declarations semi { $$ = Stmt\Use_[$2, Stmt\Use_::TYPE_NORMAL]; }
| T_USE use_type use_declarations semi { $$ = Stmt\Use_[$3, $2]; }
| group_use_declaration
| T_CONST constant_declaration_list semi { $$ = Stmt\Const_[$2]; }
| T_CONST constant_declaration_list semi { $$ = new Stmt\Const_($2, attributes(), []); }
| attributes T_CONST constant_declaration_list semi
{ $$ = new Stmt\Const_($3, attributes(), $1);
$this->checkConstantAttributes($$); }
;

use_type:
Expand Down
12 changes: 10 additions & 2 deletions lib/PhpParser/Node/Stmt/Const_.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,28 @@
class Const_ extends Node\Stmt {
/** @var Node\Const_[] Constant declarations */
public array $consts;
/** @var Node\AttributeGroup[] PHP attribute groups */
public array $attrGroups;

/**
* Constructs a const list node.
*
* @param Node\Const_[] $consts Constant declarations
* @param array<string, mixed> $attributes Additional attributes
* @param list<Node\AttributeGroup> $attrGroups PHP attribute groups
*/
public function __construct(array $consts, array $attributes = []) {
public function __construct(
array $consts,
array $attributes = [],
array $attrGroups = []
) {
$this->attributes = $attributes;
$this->attrGroups = $attrGroups;
$this->consts = $consts;
}

public function getSubNodeNames(): array {
return ['consts'];
return ['attrGroups', 'consts'];
}

public function getType(): string {
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/NodeVisitor/NameResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ public function enterNode(Node $node) {
foreach ($node->consts as $const) {
$this->addNamespacedName($const);
}
$this->resolveAttrGroups($node);
} elseif ($node instanceof Stmt\ClassConst) {
if (null !== $node->type) {
$node->type = $this->resolveType($node->type);
Expand Down
2,111 changes: 1,055 additions & 1,056 deletions lib/PhpParser/Parser/Php7.php

Large diffs are not rendered by default.

2,142 changes: 1,083 additions & 1,059 deletions lib/PhpParser/Parser/Php8.php

Large diffs are not rendered by default.

8 changes: 8 additions & 0 deletions lib/PhpParser/ParserAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use PhpParser\Node\Stmt\Class_;
use PhpParser\Node\Stmt\ClassConst;
use PhpParser\Node\Stmt\ClassMethod;
use PhpParser\Node\Stmt\Const_;
use PhpParser\Node\Stmt\Else_;
use PhpParser\Node\Stmt\ElseIf_;
use PhpParser\Node\Stmt\Enum_;
Expand Down Expand Up @@ -1202,6 +1203,13 @@ protected function checkPropertyHookModifiers(int $a, int $b, int $modifierPos):
}
}

protected function checkConstantAttributes(Const_ $node): void {
if ($node->attrGroups !== [] && count($node->consts) > 1) {
$this->emitError(new Error(
'Cannot use attributes on multiple constants at once', $node->getAttributes()));
}
}

/**
* @param Property|Param $node
*/
Expand Down
4 changes: 3 additions & 1 deletion lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -878,7 +878,9 @@ protected function pStmt_Function(Stmt\Function_ $node): string {
}

protected function pStmt_Const(Stmt\Const_ $node): string {
return 'const ' . $this->pCommaSeparated($node->consts) . ';';
return $this->pAttrGroups($node->attrGroups)
. 'const '
. $this->pCommaSeparated($node->consts) . ';';
}

protected function pStmt_Declare(Stmt\Declare_ $node): string {
Expand Down
1 change: 1 addition & 0 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1634,6 +1634,7 @@ protected function initializeEmptyListInsertionMap(): void {
Stmt\Trait_::class . '->attrGroups' => [null, '', "\n"],
Expr\ArrowFunction::class . '->attrGroups' => [null, '', ' '],
Expr\Closure::class . '->attrGroups' => [null, '', ' '],
Stmt\Const_::class . '->attrGroups' => [null, '', "\n"],
PrintableNewAnonClassNode::class . '->attrGroups' => [\T_NEW, ' ', ''],

/* These cannot be empty to start with:
Expand Down
5 changes: 5 additions & 0 deletions test/PhpParser/NodeVisitor/NameResolverTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,9 @@ function(A $a) : A {};
fn(A $a): A => $a;
fn(?A $a): ?A => $a;

#[X]
const EXAMPLE = true;

A::b();
A::$b;
A::B;
Expand Down Expand Up @@ -338,6 +341,8 @@ function fn4(?array $a): ?array
#[\NS\X] fn(array $a): array => $a;
fn(\NS\A $a): \NS\A => $a;
fn(?\NS\A $a): ?\NS\A => $a;
#[\NS\X]
const EXAMPLE = true;
\NS\A::b();
\NS\A::$b;
\NS\A::B;
Expand Down
50 changes: 50 additions & 0 deletions test/code/formatPreservation/constants.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
Constants
-----
<?php

const FOO = true;

#[A]
const BAR = true;
-----
$attrGroup = new Node\AttributeGroup([
new Node\Attribute(new Node\Name('B'), []),
]);
$stmts[0]->attrGroups[] = $attrGroup;
$stmts[1]->attrGroups[] = $attrGroup;
-----
<?php

#[B]
const FOO = true;

#[A]
#[B]
const BAR = true;
-----
<?php

#[ A, B]
const FOO = true;

#[
A,
B,
]
const BAR = true;
-----
$attr = new Node\Attribute(new Node\Name('C'), []);
$stmts[0]->attrGroups[0]->attrs[] = $attr;
$stmts[1]->attrGroups[0]->attrs[] = $attr;
-----
<?php

#[ A, B, C]
const FOO = true;

#[
A,
B,
C,
]
const BAR = true;
4 changes: 4 additions & 0 deletions test/code/parser/errorHandling/recovery.test
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ array(
)
)
3: Stmt_Const(
attrGroups: array(
)
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -629,6 +631,8 @@ array(
)
)
3: Stmt_Const(
attrGroups: array(
)
consts: array(
0: Const(
name: Identifier(
Expand Down
Loading