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 Jul 30, 2023
1 parent 19526a3 commit 73ccbab
Show file tree
Hide file tree
Showing 18 changed files with 1,263 additions and 1,049 deletions.
8 changes: 7 additions & 1 deletion grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,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 @@ -722,6 +725,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 @@ -19,6 +19,8 @@ class ClassConst implements PhpParser\Builder

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

/**
* Creates a class constant builder
Expand Down Expand Up @@ -116,6 +118,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 @@ -126,7 +141,8 @@ public function getNode(): PhpParser\Node {
$this->constants,
$this->flags,
$this->attributes,
$this->attributeGroups
$this->attributeGroups,
$this->type
);
}
}
19 changes: 12 additions & 7 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 array $attributes Additional attributes
* @param Node\AttributeGroup[] $attrGroups PHP attribute groups
* @param Node\Const_[] $consts Constant declarations
* @param int $flags Modifiers
* @param array $attributes Additional attributes
* @param 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,079 changes: 1,045 additions & 1,034 deletions lib/PhpParser/Parser/Php7.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 @@ -814,7 +814,9 @@ protected function pStmt_ClassMethod(Stmt\ClassMethod $node) {
protected function pStmt_ClassConst(Stmt\ClassConst $node) {
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) {
Expand Down
2 changes: 2 additions & 0 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1278,6 +1278,7 @@ protected function initializeRemovalMap() {
'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 @@ -1319,6 +1320,7 @@ protected function initializeInsertionMap() {
'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 LNumber(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 @@ -745,6 +745,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -1508,6 +1509,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
6 changes: 5 additions & 1 deletion test/code/parser/stmt/class/constModifierErrors.test
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_STATIC (8)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -61,6 +62,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_ABSTRACT (16)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -99,6 +101,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_READONLY (64)
type: null
consts: array(
0: Const(
name: Identifier(
Expand Down Expand Up @@ -137,6 +140,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_PUBLIC (1)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -150,4 +154,4 @@ array(
)
)
)
)
)
7 changes: 6 additions & 1 deletion test/code/parser/stmt/class/constModifiers.test
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ array(
attrGroups: array(
)
flags: 0
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -42,6 +43,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_PUBLIC (1)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -57,6 +59,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_PROTECTED (2)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -72,6 +75,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_PRIVATE (4)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -87,6 +91,7 @@ array(
attrGroups: array(
)
flags: MODIFIER_FINAL (32)
type: null
consts: array(
0: Const(
name: Identifier(
Expand All @@ -100,4 +105,4 @@ array(
)
)
)
)
)
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
Loading

0 comments on commit 73ccbab

Please sign in to comment.