diff --git a/src/main/php/lang/ast/syntax/PHP.class.php b/src/main/php/lang/ast/syntax/PHP.class.php index 5140ecd..1bc4f7c 100755 --- a/src/main/php/lang/ast/syntax/PHP.class.php +++ b/src/main/php/lang/ast/syntax/PHP.class.php @@ -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'); diff --git a/src/test/php/lang/ast/unittest/parse/MembersTest.class.php b/src/test/php/lang/ast/unittest/parse/MembersTest.class.php index 9320a31..ddc986d 100755 --- a/src/test/php/lang/ast/unittest/parse/MembersTest.class.php +++ b/src/test/php/lang/ast/unittest/parse/MembersTest.class.php @@ -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);