Skip to content

Commit b591c32

Browse files
SammyKkrakjoe
authored andcommitted
Allow trailing commas in function and method calls
1 parent 4da0bfc commit b591c32

6 files changed

+132
-3
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Leading commas in function calls is not allowed
3+
--FILE--
4+
<?php
5+
foo(,$foo);
6+
?>
7+
--EXPECTF--
8+
Parse error: syntax error, unexpected ',' in %s on line %d
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Multiple inner commas in function calls is not allowed
3+
--FILE--
4+
<?php
5+
foo($foo,,$bar);
6+
?>
7+
--EXPECTF--
8+
Parse error: syntax error, unexpected ',', expecting ')' in %s on line %d
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Multiple trailing commas in function calls is not allowed
3+
--FILE--
4+
<?php
5+
foo($foo,,);
6+
?>
7+
--EXPECTF--
8+
Parse error: syntax error, unexpected ',', expecting ')' in %s on line %d
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
--TEST--
2+
Single comma in function calls is not allowed
3+
--FILE--
4+
<?php
5+
foo(,);
6+
?>
7+
--EXPECTF--
8+
Parse error: syntax error, unexpected ',' in %s on line %d
Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,97 @@
1+
--TEST--
2+
Allow trailing commas in function and method calls
3+
--FILE--
4+
<?php
5+
function foo(...$args) {
6+
echo __FUNCTION__ . "\n";
7+
var_dump($args);
8+
}
9+
foo(
10+
'function',
11+
'bar',
12+
);
13+
14+
class Foo
15+
{
16+
public function __construct(...$args) {
17+
echo __FUNCTION__ . "\n";
18+
var_dump($args);
19+
}
20+
21+
public function bar(...$args) {
22+
echo __FUNCTION__ . "\n";
23+
var_dump($args);
24+
}
25+
26+
public function __invoke(...$args) {
27+
echo __FUNCTION__ . "\n";
28+
var_dump($args);
29+
}
30+
}
31+
32+
$foo = new Foo(
33+
'constructor',
34+
'bar',
35+
);
36+
37+
$foo->bar(
38+
'method',
39+
'bar',
40+
);
41+
42+
$foo(
43+
'invoke',
44+
'bar',
45+
);
46+
47+
$bar = function(...$args) {
48+
echo __FUNCTION__ . "\n";
49+
var_dump($args);
50+
};
51+
52+
$bar(
53+
'closure',
54+
'bar',
55+
);
56+
57+
# Make sure to hit the "not really a function" language constructs
58+
unset($foo, $bar,);
59+
var_dump(isset($foo, $bar,));
60+
?>
61+
--EXPECT--
62+
foo
63+
array(2) {
64+
[0]=>
65+
string(8) "function"
66+
[1]=>
67+
string(3) "bar"
68+
}
69+
__construct
70+
array(2) {
71+
[0]=>
72+
string(11) "constructor"
73+
[1]=>
74+
string(3) "bar"
75+
}
76+
bar
77+
array(2) {
78+
[0]=>
79+
string(6) "method"
80+
[1]=>
81+
string(3) "bar"
82+
}
83+
__invoke
84+
array(2) {
85+
[0]=>
86+
string(6) "invoke"
87+
[1]=>
88+
string(3) "bar"
89+
}
90+
{closure}
91+
array(2) {
92+
[0]=>
93+
string(7) "closure"
94+
[1]=>
95+
string(3) "bar"
96+
}
97+
bool(false)

Zend/zend_language_parser.y

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -439,7 +439,7 @@ statement:
439439
| T_ECHO echo_expr_list ';' { $$ = $2; }
440440
| T_INLINE_HTML { $$ = zend_ast_create(ZEND_AST_ECHO, $1); }
441441
| expr ';' { $$ = $1; }
442-
| T_UNSET '(' unset_variables ')' ';' { $$ = $3; }
442+
| T_UNSET '(' unset_variables possible_comma ')' ';' { $$ = $3; }
443443
| T_FOREACH '(' expr T_AS foreach_variable ')' foreach_statement
444444
{ $$ = zend_ast_create(ZEND_AST_FOREACH, $3, $5, NULL, $7); }
445445
| T_FOREACH '(' expr T_AS foreach_variable T_DOUBLE_ARROW foreach_variable ')'
@@ -670,7 +670,7 @@ return_type:
670670

671671
argument_list:
672672
'(' ')' { $$ = zend_ast_create_list(0, ZEND_AST_ARG_LIST); }
673-
| '(' non_empty_argument_list ')' { $$ = $2; }
673+
| '(' non_empty_argument_list possible_comma ')' { $$ = $2; }
674674
;
675675

676676
non_empty_argument_list:
@@ -1260,7 +1260,7 @@ encaps_var_offset:
12601260

12611261

12621262
internal_functions_in_yacc:
1263-
T_ISSET '(' isset_variables ')' { $$ = $3; }
1263+
T_ISSET '(' isset_variables possible_comma ')' { $$ = $3; }
12641264
| T_EMPTY '(' expr ')' { $$ = zend_ast_create(ZEND_AST_EMPTY, $3); }
12651265
| T_INCLUDE expr
12661266
{ $$ = zend_ast_create_ex(ZEND_AST_INCLUDE_OR_EVAL, ZEND_INCLUDE, $2); }

0 commit comments

Comments
 (0)