Skip to content

Shorter functions #6379

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

Closed
wants to merge 7 commits into from
Closed
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
23 changes: 23 additions & 0 deletions Zend/tests/short_functions/short-func-match.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Short functions with match statements.
--FILE--
<?php

fn pick_one(int $a) => match($a) {
1 => 'One',
2 => 'Two',
3 => 'Three',
default => 'More',
};

print pick_one(1) . PHP_EOL;
print pick_one(2) . PHP_EOL;
print pick_one(3) . PHP_EOL;
print pick_one(4) . PHP_EOL;

?>
--EXPECT--
One
Two
Three
More
12 changes: 12 additions & 0 deletions Zend/tests/short_functions/short-func-no-statement.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
--TEST--
Non-expression statements parse error in short functions.
--FILE--
<?php

fn test(array $a) => foreach ($a as $v) print $v;

test([1, 2, 3]);

?>
--EXPECTF--
Parse error: syntax error, unexpected token "foreach" in %s on line %d
16 changes: 16 additions & 0 deletions Zend/tests/short_functions/short-func.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
--TEST--
Basic short functions return values.
--FILE--
<?php

fn test(int $a) => $a + 1;

fn test2(int $b): int => $b + 1;

print test(5) . PHP_EOL;
print test2(5) . PHP_EOL;

?>
--EXPECT--
6
6
19 changes: 19 additions & 0 deletions Zend/tests/short_functions/short-method-no-statement.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
--TEST--
Short methods.
--FILE--
<?php

class Test {

public function __construct(private int $b) {}

public fn out(array $a) => foreach ($a as $v) print $v;
}

$t = new Test(1);

print $t->out([1, 2, 3]) . PHP_EOL;

?>
--EXPECTF--
Parse error: syntax error, unexpected token "foreach" in %s on line %d
23 changes: 23 additions & 0 deletions Zend/tests/short_functions/short-method.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
--TEST--
Short methods.
--FILE--
<?php

class Test {

public function __construct(private int $b) {}

public fn addUp(int $a) => $a + $this->b;

public fn addUp2(int $a): int => $a + $this->b;
}

$t = new Test(1);

print $t->addUp(5) . PHP_EOL;
print $t->addUp2(5) . PHP_EOL;

?>
--EXPECT--
6
6
10 changes: 10 additions & 0 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,10 @@ function_declaration_statement:
backup_fn_flags '{' inner_statement_list '}' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2 | $13, $1, $4,
zend_ast_get_str($3), $6, NULL, $11, $8, NULL); CG(extra_fn_flags) = $9; }
| fn returns_ref T_STRING backup_doc_comment '(' parameter_list ')' return_type
backup_fn_flags T_DOUBLE_ARROW expr ';' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_FUNC_DECL, $2, $1, $4,
zend_ast_get_str($3), $6, NULL, zend_ast_create(ZEND_AST_RETURN, $11), $8, NULL); CG(extra_fn_flags) = $9; }
;

is_reference:
Expand Down Expand Up @@ -859,6 +863,11 @@ attributed_class_statement:
return_type backup_fn_flags method_body backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_METHOD, $3 | $1 | $12, $2, $5,
zend_ast_get_str($4), $7, NULL, $11, $9, NULL); CG(extra_fn_flags) = $10; }
| method_modifiers fn returns_ref identifier backup_doc_comment '(' parameter_list ')'
return_type backup_fn_flags T_DOUBLE_ARROW expr ';' backup_fn_flags
{ $$ = zend_ast_create_decl(ZEND_AST_METHOD, $3 | $1 | $14, $2, $5,
zend_ast_get_str($4), $7, NULL, zend_ast_create(ZEND_AST_RETURN, $12), $9, NULL);
CG(extra_fn_flags) = $10; }
;

class_statement:
Expand Down Expand Up @@ -923,6 +932,7 @@ absolute_trait_method_reference:
method_body:
';' /* abstract method */ { $$ = NULL; }
| '{' inner_statement_list '}' { $$ = $2; }
| T_DOUBLE_ARROW expr ';' { $$ = zend_ast_create(ZEND_AST_RETURN, $2); }
;

variable_modifiers:
Expand Down