Skip to content

Commit

Permalink
Add support for typed constants
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed May 20, 2023
1 parent 9a5d5c1 commit 5c267f5
Show file tree
Hide file tree
Showing 19 changed files with 2,295 additions and 2,053 deletions.
8 changes: 7 additions & 1 deletion grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,10 @@ non_empty_class_const_list:
;

class_const:
identifier_maybe_reserved '=' expr { $$ = Node\Const_[$1, $3]; }
T_STRING '=' expr
{ $$ = Node\Const_[new Node\Identifier($1, stackAttributes(#1)), $3]; }
| semi_reserved '=' expr
{ $$ = Node\Const_[new Node\Identifier($1, stackAttributes(#1)), $3]; }
;

inner_statement_list_ex:
Expand Down Expand Up @@ -842,6 +845,9 @@ class_statement:
| optional_attributes method_modifiers T_CONST class_const_list semi
{ $$ = new Stmt\ClassConst($4, $2, attributes(), $1);
$this->checkClassConst($$, #2); }
| optional_attributes method_modifiers T_CONST type_expr class_const_list semi
{ $$ = new Stmt\ClassConst($5, $2, attributes(), $1, $4);
$this->checkClassConst($$, #2); }
| optional_attributes method_modifiers T_FUNCTION optional_ref identifier_maybe_reserved '(' parameter_list ')'
optional_return_type method_body
{ $$ = Stmt\ClassMethod[$5, ['type' => $2, 'byRef' => $4, 'params' => $7, 'returnType' => $9, 'stmts' => $10, 'attrGroups' => $1]];
Expand Down
18 changes: 17 additions & 1 deletion lib/PhpParser/Builder/ClassConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ class ClassConst implements PhpParser\Builder {

/** @var list<Node\AttributeGroup> */
protected $attributeGroups = [];
/** @var Identifier|Node\Name|Node\ComplexType */
protected $type;

/**
* Creates a class constant builder
Expand Down Expand Up @@ -119,6 +121,19 @@ public function addAttribute($attribute) {
return $this;
}

/**
* Sets the constant type.
*
* @param string|Node\Name|Identifier|Node\ComplexType $type
*
* @return $this
*/
public function setType($type) {
$this->type = BuilderHelpers::normalizeType($type);

return $this;
}

/**
* Returns the built class node.
*
Expand All @@ -129,7 +144,8 @@ public function getNode(): PhpParser\Node {
$this->constants,
$this->flags,
$this->attributes,
$this->attributeGroups
$this->attributeGroups,
$this->type
);
}
}
15 changes: 10 additions & 5 deletions lib/PhpParser/Node/Stmt/ClassConst.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,31 +10,36 @@ class ClassConst extends Node\Stmt {
public $flags;
/** @var Node\Const_[] Constant declarations */
public $consts;
/** @var Node\AttributeGroup[] */
/** @var Node\AttributeGroup[] PHP attribute groups */
public $attrGroups;
/** @var Node\Identifier|Node\Name|Node\ComplexType Type declaration */
public $type;

/**
* Constructs a class const list node.
*
* @param Node\Const_[] $consts Constant declarations
* @param int $flags Modifiers
* @param Node\Const_[] $consts Constant declarations
* @param int $flags Modifiers
* @param array<string, mixed> $attributes Additional attributes
* @param list<Node\AttributeGroup> $attrGroups PHP attribute groups
* @param null|string|Node\Identifier|Node\Name|Node\ComplexType $type Type declaration
*/
public function __construct(
array $consts,
int $flags = 0,
array $attributes = [],
array $attrGroups = []
array $attrGroups = [],
$type = null
) {
$this->attributes = $attributes;
$this->flags = $flags;
$this->consts = $consts;
$this->attrGroups = $attrGroups;
$this->type = $type;
}

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

/**
Expand Down
2,050 changes: 1,032 additions & 1,018 deletions lib/PhpParser/Parser/Php7.php

Large diffs are not rendered by default.

2,074 changes: 1,050 additions & 1,024 deletions lib/PhpParser/Parser/Php8.php

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -842,7 +842,9 @@ protected function pStmt_ClassMethod(Stmt\ClassMethod $node): string {
protected function pStmt_ClassConst(Stmt\ClassConst $node): string {
return $this->pAttrGroups($node->attrGroups)
. $this->pModifiers($node->flags)
. 'const ' . $this->pCommaSeparated($node->consts) . ';';
. 'const '
. (null !== $node->type ? $this->p($node->type) . ' ' : '')
. $this->pCommaSeparated($node->consts) . ';';
}

protected function pStmt_Function(Stmt\Function_ $node): string {
Expand Down
2 changes: 2 additions & 0 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1391,6 +1391,7 @@ protected function initializeRemovalMap(): void {
'Param->default' => $stripEquals,
'Stmt_Break->num' => $stripBoth,
'Stmt_Catch->var' => $stripLeft,
'Stmt_ClassConst->type' => $stripRight,
'Stmt_ClassMethod->returnType' => $stripColon,
'Stmt_Class->extends' => ['left' => \T_EXTENDS],
'Stmt_Enum->scalarType' => $stripColon,
Expand Down Expand Up @@ -1434,6 +1435,7 @@ protected function initializeInsertionMap(): void {
'Stmt_Break->num' => [\T_BREAK, false, ' ', null],
'Stmt_Catch->var' => [null, false, ' ', null],
'Stmt_ClassMethod->returnType' => [')', false, ': ', null],
'Stmt_ClassConst->type' => [\T_CONST, false, ' ', null],
'Stmt_Class->extends' => [null, false, ' extends ', null],
'Stmt_Enum->scalarType' => [null, false, ' : ', null],
'Stmt_EnumCase->expr' => [null, false, ' = ', null],
Expand Down
12 changes: 12 additions & 0 deletions test/PhpParser/Builder/ClassConstTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,18 @@ public function testAddAttribute() {
);
}

public function testType() {
$node = $this->createClassConstBuilder('TYPE', 1)
->setType('int')
->getNode();
$this->assertEquals(
new Stmt\ClassConst(
[new Const_('TYPE', new Int_(1))],
0, [], [], new Identifier('int')),
$node
);
}

/**
* @dataProvider provideTestDefaultValues
*/
Expand Down
9 changes: 9 additions & 0 deletions test/code/formatPreservation/insertionOfNullable.test
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,10 @@ X
private
$x
;

const
X
= 1;
}

foreach (
Expand Down Expand Up @@ -86,6 +90,7 @@ $stmts[9]->expr = new Expr\Variable('x');
$stmts[10]->extends = new Node\Name\FullyQualified('Bar');
$stmts[10]->stmts[0]->returnType = new Node\Name('Y');
$stmts[10]->stmts[1]->props[0]->default = new Scalar\DNumber(42.0);
$stmts[10]->stmts[2]->type = new Node\Identifier('int');
$stmts[11]->keyVar = new Expr\Variable('z');
$stmts[12]->vars[0]->default = new Scalar\String_('abc');
$stmts[13]->finally = new Stmt\Finally_([]);
Expand Down Expand Up @@ -140,6 +145,10 @@ X extends \Bar
private
$x = 42.0
;

const int
X
= 1;
}

foreach (
Expand Down
10 changes: 10 additions & 0 deletions test/code/formatPreservation/removalViaNull.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ Bar
y
;
}

const
int
X
= 1;
}

$foo [ $bar ];
Expand Down Expand Up @@ -97,6 +102,7 @@ $stmts[2]->extends = null;
$stmts[2]->stmts[0]->returnType = null;
$stmts[2]->stmts[1]->props[0]->default = null;
$stmts[2]->stmts[2]->adaptations[0]->newName = null;
$stmts[2]->stmts[3]->type = null;
$stmts[3]->expr->dim = null;
$stmts[4]->expr->expr = null;
$stmts[5]->expr->if = null;
Expand Down Expand Up @@ -141,6 +147,10 @@ Foo
public
;
}

const
X
= 1;
}

$foo [];
Expand Down
2 changes: 2 additions & 0 deletions test/code/parser/errorHandling/recovery.test
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -1495,6 +1496,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down
2 changes: 2 additions & 0 deletions test/code/parser/semiReserved.test
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -175,6 +176,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down
1 change: 1 addition & 0 deletions test/code/parser/stmt/class/anonymous.test
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down
4 changes: 4 additions & 0 deletions test/code/parser/stmt/class/constModifierErrors.test
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ array(
attrGroups: array(
)
flags: STATIC (8)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -59,6 +60,7 @@ array(
attrGroups: array(
)
flags: ABSTRACT (16)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -96,6 +98,7 @@ array(
attrGroups: array(
)
flags: READONLY (64)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -133,6 +136,7 @@ array(
attrGroups: array(
)
flags: PUBLIC (1)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down
5 changes: 5 additions & 0 deletions test/code/parser/stmt/class/constModifiers.test
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -41,6 +42,7 @@ array(
attrGroups: array(
)
flags: PUBLIC (1)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -56,6 +58,7 @@ array(
attrGroups: array(
)
flags: PROTECTED (2)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -71,6 +74,7 @@ array(
attrGroups: array(
)
flags: PRIVATE (4)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -86,6 +90,7 @@ array(
attrGroups: array(
)
flags: FINAL (32)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down
1 change: 1 addition & 0 deletions test/code/parser/stmt/class/simple.test
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down

0 comments on commit 5c267f5

Please sign in to comment.