rfc:trailing-comma-function-args

This is an old revision of the document!


Request for Comments: How to write RFCs

Introduction

PHP has long supported trailing commas in array declarations like so:

<?php
$a = array(
       'Orange',
       'Apple',
       'Banana',
      );

This is useful for VCS (cvs, svn, git, hg, etc...) since individual lines in the list may be modified, added, or removed without having to touch unrelated lines which happen to be at the end of the list.

Function call arguments do not share this trait.

<?php
// This is an error
$fp = fopen(
        "sample.txt",
        "r+",
       );

Which means that adding additional parameters to the call forces touching both the new line and the prior one, which is bad for VCS history.

This RFC proposes to allow trailing commas in function and method call argument lists.

The patch

Could not be much simpler...

diff --git a/Zend/zend_language_parser.y b/Zend/zend_language_parser.y
index ccbc9b1..6b6de2e 100644
--- a/Zend/zend_language_parser.y
+++ b/Zend/zend_language_parser.y
@@ -542,7 +542,7 @@ optional_class_type:

 function_call_parameter_list:
                '(' ')' { Z_LVAL($$.u.constant) = 0; }
-       |       '(' non_empty_function_call_parameter_list ')'  { $$ = $2; }
+       |       '(' non_empty_function_call_parameter_list possible_comma ')'   { $$ = $2; }
        |       '(' yield_expr ')'      { Z_LVAL($$.u.constant) = 1; zend_do_pass_param(&$2, ZEND_SEND_VAL, Z_LVAL($$.u.constant) TSRMLS_CC); }
 ;

What's left out on purpose

Bonus comma in the middle of a void argument list

  • It implies two ignored args, rather than one
  • It doesn't actually help the VCS blame issue anyway
<?php
phpinfo(
    ,
    );

Trailing comma in yield() expression

  • yield only takes one value (or key=>val expression)
  • yield is not really a function call, even if it looks like one
<?php
function mygen() {
  yield(
    123,
    );
}

Tests

Yes, I'll write some.

rfc/trailing-comma-function-args.1361298017.txt.gz · Last modified: 2017/09/22 13:28 (external edit)