Skip to content

Commit

Permalink
Support readonly anonymous classes
Browse files Browse the repository at this point in the history
  • Loading branch information
nikic committed Jul 30, 2023
1 parent 73ccbab commit 05e84f7
Show file tree
Hide file tree
Showing 7 changed files with 467 additions and 421 deletions.
4 changes: 2 additions & 2 deletions grammar/php7.y
Original file line number Diff line number Diff line change
Expand Up @@ -949,8 +949,8 @@ expr:
;

anonymous_class:
optional_attributes T_CLASS ctor_arguments extends_from implements_list '{' class_statement_list '}'
{ $$ = array(Stmt\Class_[null, ['type' => 0, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]], $3);
optional_attributes class_entry_type ctor_arguments extends_from implements_list '{' class_statement_list '}'
{ $$ = array(Stmt\Class_[null, ['type' => $2, 'extends' => $4, 'implements' => $5, 'stmts' => $7, 'attrGroups' => $1]], $3);
$this->checkClass($$[0], -1); }
;

Expand Down
9 changes: 6 additions & 3 deletions lib/PhpParser/Internal/PrintableNewAnonClassNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class PrintableNewAnonClassNode extends Expr
{
/** @var Node\AttributeGroup[] PHP attribute groups */
public $attrGroups;
/** @var int Modifiers */
public $flags;
/** @var Node\Arg[] Arguments */
public $args;
/** @var null|Node\Name Name of extended class */
Expand All @@ -29,11 +31,12 @@ class PrintableNewAnonClassNode extends Expr
public $stmts;

public function __construct(
array $attrGroups, array $args, Node\Name $extends = null, array $implements,
array $attrGroups, int $flags, array $args, Node\Name $extends = null, array $implements,
array $stmts, array $attributes
) {
parent::__construct($attributes);
$this->attrGroups = $attrGroups;
$this->flags = $flags;
$this->args = $args;
$this->extends = $extends;
$this->implements = $implements;
Expand All @@ -46,7 +49,7 @@ public static function fromNewNode(Expr\New_ $newNode) {
// We don't assert that $class->name is null here, to allow consumers to assign unique names
// to anonymous classes for their own purposes. We simplify ignore the name here.
return new self(
$class->attrGroups, $newNode->args, $class->extends, $class->implements,
$class->attrGroups, $class->flags, $newNode->args, $class->extends, $class->implements,
$class->stmts, $newNode->getAttributes()
);
}
Expand All @@ -56,6 +59,6 @@ public function getType() : string {
}

public function getSubNodeNames() : array {
return ['attrGroups', 'args', 'extends', 'implements', 'stmts'];
return ['attrGroups', 'flags', 'args', 'extends', 'implements', 'stmts'];
}
}
830 changes: 415 additions & 415 deletions lib/PhpParser/Parser/Php7.php

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -1510,6 +1510,7 @@ protected function initializeModifierChangeMap() {
'Stmt_ClassMethod->flags' => \T_FUNCTION,
'Stmt_Class->flags' => \T_CLASS,
'Stmt_Property->flags' => \T_VARIABLE,
'Expr_PrintableNewAnonClass->flags' => \T_CLASS,
'Param->flags' => \T_VARIABLE,
//'Stmt_TraitUseAdaptation_Alias->newModifier' => 0, // TODO
];
Expand Down
13 changes: 12 additions & 1 deletion test/code/formatPreservation/modifierChange.test
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,15 @@ function test(
= 'z',
public T3 $z
= 'x',
) {}
) {}
-----
<?php
new class {};
new readonly class {};
-----
$stmts[0]->expr->class->flags = Stmt\Class_::MODIFIER_READONLY;
$stmts[1]->expr->class->flags = 0;
-----
<?php
readonly class {};
class {};
26 changes: 26 additions & 0 deletions test/code/parser/stmt/class/readonlyAnonyous.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
Readonly anonymous class
-----
<?php

new readonly class {};
-----
!!php7
array(
0: Stmt_Expression(
expr: Expr_New(
class: Stmt_Class(
attrGroups: array(
)
flags: MODIFIER_READONLY (64)
name: null
extends: null
implements: array(
)
stmts: array(
)
)
args: array(
)
)
)
)
5 changes: 5 additions & 0 deletions test/code/prettyPrinter/expr/anonymousClass.test
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@ new class($a) extends A {
$this->a = $a;
}
};
new readonly class {};
-----
!!php7
new class
{
};
Expand All @@ -25,3 +27,6 @@ new class($a) extends A
$this->a = $a;
}
};
new readonly class
{
};

0 comments on commit 05e84f7

Please sign in to comment.