Skip to content

[RFC] Allow a trailing comma in function and method calls #2

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 1 commit 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
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Leading commas in function calls is not allowed
--FILE--
<?php
foo(,$foo);
?>
--EXPECTF--
Parse error: syntax error, unexpected ',' in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Multiple inner commas in function calls is not allowed
--FILE--
<?php
foo($foo,,$bar);
?>
--EXPECTF--
Parse error: syntax error, unexpected ',', expecting ')' in %s on line %d
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Multiple trailing commas in function calls is not allowed
--FILE--
<?php
foo($foo,,);
?>
--EXPECTF--
Parse error: syntax error, unexpected ',', expecting ')' in %s on line %d
8 changes: 8 additions & 0 deletions Zend/tests/function_arguments/call_with_only_comma_error.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
--TEST--
Single comma in function calls is not allowed
--FILE--
<?php
foo(,);
?>
--EXPECTF--
Parse error: syntax error, unexpected ',' in %s on line %d
97 changes: 97 additions & 0 deletions Zend/tests/function_arguments/call_with_trailing_comma_basic.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
--TEST--
Allow trailing commas in function and method calls
--FILE--
<?php
function foo(...$args) {
echo __FUNCTION__ . "\n";
var_dump($args);
}
foo(
'function',
'bar',
);

class Foo
{
public function __construct(...$args) {
echo __FUNCTION__ . "\n";
var_dump($args);
}

public function bar(...$args) {
echo __FUNCTION__ . "\n";
var_dump($args);
}

public function __invoke(...$args) {
echo __FUNCTION__ . "\n";
var_dump($args);
}
}

$foo = new Foo(
'constructor',
'bar',
);

$foo->bar(
'method',
'bar',
);

$foo(
'invoke',
'bar',
);

$bar = function(...$args) {
echo __FUNCTION__ . "\n";
var_dump($args);
};

$bar(
'closure',
'bar',
);

# Make sure to hit the "not really a function" language constructs
unset($foo, $bar,);
var_dump(isset($foo, $bar,));
?>
--EXPECT--
foo
array(2) {
[0]=>
string(8) "function"
[1]=>
string(3) "bar"
}
__construct
array(2) {
[0]=>
string(11) "constructor"
[1]=>
string(3) "bar"
}
bar
array(2) {
[0]=>
string(6) "method"
[1]=>
string(3) "bar"
}
__invoke
array(2) {
[0]=>
string(6) "invoke"
[1]=>
string(3) "bar"
}
{closure}
array(2) {
[0]=>
string(7) "closure"
[1]=>
string(3) "bar"
}
bool(false)
6 changes: 3 additions & 3 deletions Zend/zend_language_parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ statement:
| T_ECHO echo_expr_list ';' { $$ = $2; }
| T_INLINE_HTML { $$ = zend_ast_create(ZEND_AST_ECHO, $1); }
| expr ';' { $$ = $1; }
| T_UNSET '(' unset_variables ')' ';' { $$ = $3; }
| T_UNSET '(' unset_variables possible_comma ')' ';' { $$ = $3; }
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
{ $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $5, NULL, $7); }
| T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')'
Expand Down Expand Up @@ -670,7 +670,7 @@ return_type:

argument_list:
'(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
| '(' non_empty_argument_list ')' { $$ = $2; }
| '(' non_empty_argument_list possible_comma ')' { $$ = $2; }
;

non_empty_argument_list:
Expand Down Expand Up @@ -1260,7 +1260,7 @@ encaps_var_offset:


internal_functions_in_yacc:
T_ISSET '(' isset_variables ')' { $$ = $3; }
T_ISSET '(' isset_variables possible_comma ')' { $$ = $3; }
| T_EMPTY '(' expr ')' { $$ = zend_ast_create(ZEND_AST_EMPTY, $3); }
| T_INCLUDE expr
{ $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_INCLUDE, $2); }
Expand Down