30

PHP: rfc:trailing_comma_in_parameter_list

 4 years ago
source link: https://wiki.php.net/rfc/trailing_comma_in_parameter_list
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

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.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK