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 May 20, 2023
1 parent 5c267f5 commit 91da191
Show file tree
Hide file tree
Showing 9 changed files with 948 additions and 921 deletions.
4 changes: 2 additions & 2 deletions grammar/php.y
Original file line number Diff line number Diff line change
Expand Up @@ -1076,8 +1076,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 @@ -18,6 +18,8 @@
class PrintableNewAnonClassNode extends Expr {
/** @var Node\AttributeGroup[] PHP attribute groups */
public $attrGroups;
/** @var int Modifiers */
public $flags;
/** @var (Node\Arg|Node\VariadicPlaceholder)[] Arguments */
public $args;
/** @var null|Node\Name Name of extended class */
Expand All @@ -36,11 +38,12 @@ class PrintableNewAnonClassNode extends Expr {
* @param array<string, mixed> $attributes Attributes
*/
public function __construct(
array $attrGroups, array $args, ?Node\Name $extends, array $implements,
array $attrGroups, int $flags, array $args, ?Node\Name $extends, 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 @@ -53,7 +56,7 @@ public static function fromNewNode(Expr\New_ $newNode): self {
// 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 @@ -63,6 +66,6 @@ public function getType(): string {
}

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

Large diffs are not rendered by default.

914 changes: 450 additions & 464 deletions lib/PhpParser/Parser/Php8.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 @@ -1633,6 +1633,7 @@ protected function initializeModifierChangeMap(): void {
Stmt\ClassMethod::class . '->flags' => ['pModifiers', \T_FUNCTION],
Stmt\Class_::class . '->flags' => ['pModifiers', \T_CLASS],
Stmt\Property::class . '->flags' => ['pModifiers', \T_VARIABLE],
PrintableNewAnonClassNode::class . '->flags' => ['pModifiers', \T_CLASS],
Param::class . '->flags' => ['pModifiers', \T_VARIABLE],
Expr\Closure::class . '->static' => ['pStatic', \T_FUNCTION],
Expr\ArrowFunction::class . '->static' => ['pStatic', \T_FN],
Expand Down
1 change: 1 addition & 0 deletions test/PhpParser/PrettyPrinterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -210,6 +210,7 @@ public function testFormatPreservingPrint($name, $code, $modification, $expected
use PhpParser\Node\Expr;
use PhpParser\Node\Scalar;
use PhpParser\Node\Stmt;
use PhpParser\Modifiers;
\$fn = function(&\$stmts) { $modification };
CODE
);
Expand Down
11 changes: 11 additions & 0 deletions test/code/formatPreservation/modifierChange.test
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,14 @@ function test(
public T3 $z
= 'x',
) {}
-----
<?php
new class {};
new readonly class {};
-----
$stmts[0]->expr->class->flags = Modifiers::READONLY;
$stmts[1]->expr->class->flags = 0;
-----
<?php
readonly class {};
class {};
25 changes: 25 additions & 0 deletions test/code/parser/stmt/class/readonlyAnonyous.test
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
Readonly anonymous class
-----
<?php

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

0 comments on commit 91da191

Please sign in to comment.