Skip to content

Add support for single-expression methods #58

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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)];
$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