Table of Contents

PHP RFC: Allow trailing comma in parameter list

Introduction

This RFC proposes to allow an optional trailing comma in parameter lists. This is already supported for argument lists.

Consider the constructor of the League\Uri\Uri class:

class Uri {
    private function __construct(
        ?string $scheme,
        ?string $user,
        ?string $pass,
        ?string $host,
        ?int $port,
        string $path,
        ?string $query,
        ?string $fragment // <-- ARGH!
    ) {
        ...
    }
}

This constructor has too many parameters to place all of them on one line, at least under conventional coding standards. In such cases PSR-12 recommends to use the formatting above instead.

Unfortunately, it is currently not possible to place a trailing comma in the parameter list. This breaks uniformity, and results in larger diffs when a new optional parameter is added. Additionally, it is inconsistent with call-sites, which do allow a trailing comma:

new Uri(
    $scheme,
    $user,
    $pass,
    $host,
    $port,
    $path,
    $query,
    $fragment, // <-- Huh, this is allowed!
);

At this point, I'm used to always adding a trailing comma to multi-line lists, regardless of what kind of element they contain. I write the comma by default, and then have to go back to remove it upon receiving an error. We should avoid having this kind of arbitrary restriction.

Proposal

Allow a single optional trailing comma in parameter lists. This includes parameter lists for functions, methods and closures.

Prior Art

Allow a trailing comma in function calls added support for trailing commas in calls.

Trailing Commas In List Syntax was a previous proposal to support optional trailing commas in all list-like structures.

Nothing has substantially changed since those RFCs in technical terms, but I think the aforementioned style for breaking up large parameter lists has become more accepted in the meantime, and features like Constructor Property Promotion are going to increase its use further.

Backward Incompatible Changes

None.

Vote

Voting opened 2020-04-14 and closes 2020-04-28.

Allow trailing comma in parameter lists?
Real name Yes No
ajf (ajf)  
alcaeus (alcaeus)  
as (as)  
asgrim (asgrim)  
ashnazg (ashnazg)  
beberlei (beberlei)  
bmajdak (bmajdak)  
brzuchal (brzuchal)  
bwoebi (bwoebi)  
carusogabriel (carusogabriel)  
cmb (cmb)  
colinodell (colinodell)  
danack (danack)  
daverandom (daverandom)  
derick (derick)  
duncan3dc (duncan3dc)  
ekin (ekin)  
galvao (galvao)  
girgias (girgias)  
jasny (jasny)  
jbnahan (jbnahan)  
jhdxr (jhdxr)  
kalle (kalle)  
kelunik (kelunik)  
kguest (kguest)  
klaussilveira (klaussilveira)  
kocsismate (kocsismate)  
krakjoe (krakjoe)  
malukenho (malukenho)  
marcio (marcio)  
mariano (mariano)  
mcmic (mcmic)  
mfonda (mfonda)  
mike (mike)  
nicolasgrekas (nicolasgrekas)  
nikic (nikic)  
ocramius (ocramius)  
peehaa (peehaa)  
petk (petk)  
pmjones (pmjones)  
pmmaga (pmmaga)  
pollita (pollita)  
ramsey (ramsey)  
reywob (reywob)  
royopa (royopa)  
ruudboon (ruudboon)  
salathe (salathe)  
sammyk (sammyk)  
sebastian (sebastian)  
sergey (sergey)  
sirsnyder (sirsnyder)  
stas (stas)  
svpernova09 (svpernova09)  
tandre (tandre)  
tiffany (tiffany)  
trowski (trowski)  
villfa (villfa)  
wyrihaximus (wyrihaximus)  
yunosh (yunosh)  
Final result: 58 1
This poll has been closed.