Skip to content

Multi-line arrays / Trailing commas in (multi-line) arrays #14

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
TimWolla opened this issue May 18, 2022 · 5 comments
Closed

Multi-line arrays / Trailing commas in (multi-line) arrays #14

TimWolla opened this issue May 18, 2022 · 5 comments
Labels
core enhancement New feature or request

Comments

@TimWolla
Copy link
Contributor

PHP supports trailing commas in arrays since 7.2. As of now the existing formatting standards of PHP-FIG do not specify whether or not (or when) trailing commas in Arrays are acceptable or required. More generally the formatting of multi-line arrays is left unspecified, whereas for other constructs it is clearly specified how they need to be split across multiple lines.

This is related to #3, since PHP 7.3+ introduces additional constructs where trailing commas are allowed and PHP 8.0 introduces named parameters which are syntactically very close to an associative array literal.

@TimWolla TimWolla changed the title Trailing commas in (multi-line) arrays Multi-line arrays / Trailing commas in (multi-line) arrays May 18, 2022
@Crell
Copy link
Collaborator

Crell commented May 20, 2022

I know at least some coding standards mandate the trailing comma if multi-lining an array.

If we do something here, it should apply to all multi-line-with-comma situations. Eg, function calls, function definitions, arrays, etc.

@samdark
Copy link
Member

samdark commented May 22, 2022

There are objective advantages always having a comma:

  1. It allows you to move lines around in multi-line arrays w/o tracking commas.
  2. Diff is better in case of appending lines in multi-line arrays.

I think it may sense to at least softly suggest it in form of SHOULD.

@samdark samdark added the enhancement New feature or request label May 22, 2022
@KorvinSzanto
Copy link
Contributor

I wouldn't say that adding trailing commas contributes to readability but I would be okay with adding a "SHOULD" with the added info of it being helpful when diffing code changes. I don't think SHOULD's or optional requirements in general benefit this spec much since they can't really be enforced with tools without custom configuration, in this case where it is beneficial in many cases I think it's worth mentioning.


I wonder if there'd be a better way we could organize these sorts of soft optional requirements where at least consistency is a MUST. Something like:

Implementors MUST decide on rules for the following as hard requirements:

1. **Trailing Commas In Multiline Arrays / Arguments**
  A. Required: The last element of a multiline array, or multiline function call/declaration arguments MUST be followed by a trailing comma
  B. Forbidden: Multiline arrays or function call/declaration arguments MUST NOT be followed by an array

But this might make things more painful than SHOULDs would on their own.

@samdark samdark added the core label May 24, 2022
@TimWolla
Copy link
Contributor Author

TimWolla commented Jun 2, 2022

Possible PR: #18

@KorvinSzanto
Copy link
Contributor

Yep #18 merged and specifies when a trailing comma must or must not be used so I think this is settled.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
core enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

4 participants