Skip to content
Open
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
4 changes: 4 additions & 0 deletions src/main/php/lang/ast/syntax/PHP.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -1130,6 +1130,10 @@ public function __construct() {
$parse->forward();
$statements= $this->statements($parse);
$parse->expecting('}', 'method declaration');
} else if ('=>' === $parse->token->value) { // Single expression
$parse->forward();
$statements= [new ReturnStatement($this->expression($parse, 0), $parse->token->line)];
Copy link
Member Author

@thekid thekid Oct 3, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is inconsistent with how short and long forms of property accessors are parsed - they make use of the Block class:

  • body for => [expr]: Expr
  • body for { [statement]; }: Block([Statement])

$parse->expecting(';', 'method expression');
} else if (';' === $parse->token->value) { // Abstract or interface method
$statements= null;
$parse->expecting(';', 'method declaration');
Expand Down
9 changes: 9 additions & 0 deletions src/test/php/lang/ast/unittest/parse/MembersTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,15 @@ public function method_with_annotations() {
$this->assertParsed([$class], 'class A { #[Test, Ignore("Not implemented")] public function a() { } }');
}

#[Test]
public function single_expression_method() {
$expr= new ReturnStatement(new Literal('true', self::LINE), self::LINE);
$class= new ClassDeclaration([], new IsValue('\\A'), null, [], [], null, null, self::LINE);
$class->declare(new Method(['private'], 'a', new Signature([], null, false, self::LINE), [$expr], null, null, self::LINE));

$this->assertParsed([$class], 'class A { private function a() => true; }');
}

#[Test]
public function property_with_get_and_set_hooks() {
$class= new ClassDeclaration([], new IsValue('\\A'), null, [], [], null, null, self::LINE);
Expand Down
Loading