PHP-CS-Fixer icon indicating copy to clipboard operation
PHP-CS-Fixer copied to clipboard

Enforce new line for each array item if there's already at least one new line

Open VincentLanglet opened this issue 3 years ago • 16 comments

ℹ️ Note: related to #1217, but it looks like it should be implemented as configurable strategy.

Rule request

I just encountered the issue when I was surprised that

[$foo,
$bar]

was fixed to

[$foo,
$bar, ]

by the trailing_comma_in_multiline rule.

  1. We definitely need a rule to change this code to
[
    $foo,
    $bar,
]

Any idea about the name ?

  1. Even without this rule, shouldn't trailing_comma_in_multiline have a special behavior when the ] is on the same line than the last element ? I would expect
[$foo,
$bar]

to be untouched by this rule.

WDYT ?

VincentLanglet avatar Nov 02 '22 15:11 VincentLanglet

trailing_comma_in_multiline adding a comma here is definitely a bug to me. The purpose of the comma is to reduce noise in diffs when appending a new item is the array. In this snippet, the ] defeats this purpose so the comma is superfluous.

And I'm :+1: to add a rule that enforces newlines around every entry of an array as soon as there is at least one newline.

julienfalque avatar Nov 03 '22 09:11 julienfalque

@VincentLanglet @julienfalque this ☝🏼 and maybe this then?

kubawerlos avatar Nov 03 '22 18:11 kubawerlos

The PR will fix the bug indeed.

Then we could add a rule to improve the display of multiline array (one item by line).

VincentLanglet avatar Nov 03 '22 19:11 VincentLanglet

Since this issue has not had any activity within the last 90 days, I have marked it as stale.

I will close it if no further activity occurs within the next 30 days.

github-actions[bot] avatar Aug 15 '23 12:08 github-actions[bot]

The rule is still not implemented

VincentLanglet avatar Aug 15 '23 19:08 VincentLanglet

@VincentLanglet isn't it the same feature request as in #1217?

Wirone avatar Aug 16 '23 07:08 Wirone

I would say yes and no.

The referenced issues ask to always split multi items array.

Mine ask to fully split multi items arrays IF you started to write it multi line.

VincentLanglet avatar Aug 16 '23 07:08 VincentLanglet

The new rule should be a part of @PER-CS ruleset as stated in https://www.php-fig.org/per/coding-style/

Array declarations MAY be split across multiple lines, where each subsequent line is indented once. When doing so, the first value in the array MUST be on the next line, and there MUST be only one value per line.

winiarekk avatar Oct 25 '23 18:10 winiarekk

@winiarekk it's only MAY, so enforcing it would be a little bit too opinionated IMHO. Regardless, it's not required to make such decision here, we rather modify rulesets in separate PRs.

Wirone avatar Oct 25 '23 19:10 Wirone

@winiarekk it's only MAY, so enforcing it would be a little bit too opinionated IMHO. Regardless, it's not required to make such decision here, we rather modify rulesets in separate PRs.

It MAY be split across multiple lines, but when it's split, it MUST be one value per line.

That's exactly the feature request.

I want

[$foo, $bar]

to be untouched, but

[$foo,
$bar]

fixed to

[
    $foo,
    $bar,
]

VincentLanglet avatar Oct 25 '23 19:10 VincentLanglet

You're both right, I wasn't focused enough. Tough time in private life, that's why I don't do much here recently 😥. Good to see fresh heads around 👍.

Wirone avatar Oct 25 '23 19:10 Wirone

Since this issue has not had any activity within the last 90 days, I have marked it as stale.

The purpose of this action is to enforce backlog review once in a while. This is mostly for maintainers and helps with keeping repository in good condition, because stale issues and PRs can accumulate over time and make it harder for others to find relevant information. It is also possible that some changes has been made to the repo already, and issue or PR became outdated, but wasn't closed for some reason. This action helps with periodic review and closing of such stale items in automated way.

You may let maintainers handle this or verify current relevancy by yourself, to help with re-triage. Any activity will remove stale label so it won't be automatically closed at this point.

I will close it if no further activity occurs within the next 30 days.

github-actions[bot] avatar Jan 24 '24 12:01 github-actions[bot]

Still relevant.

keithbrink avatar Jan 24 '24 12:01 keithbrink

Since this issue has not had any activity within the last 90 days, I have marked it as stale.

The purpose of this action is to enforce backlog review once in a while. This is mostly for maintainers and helps with keeping repository in good condition, because stale issues and PRs can accumulate over time and make it harder for others to find relevant information. It is also possible that some changes has been made to the repo already, and issue or PR became outdated, but wasn't closed for some reason. This action helps with periodic review and closing of such stale items in automated way.

You may let maintainers handle this or verify current relevancy by yourself, to help with re-triage. Any activity will remove stale label so it won't be automatically closed at this point.

I will close it if no further activity occurs within the next 30 days.

github-actions[bot] avatar Apr 26 '24 12:04 github-actions[bot]

Still WIp https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7813

It requires https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/pull/7901 first

VincentLanglet avatar Apr 26 '24 12:04 VincentLanglet

Since this issue has not had any activity within the last 90 days, I have marked it as stale.

The purpose of this action is to enforce backlog review once in a while. This is mostly for maintainers and helps with keeping repository in good condition, because stale issues and PRs can accumulate over time and make it harder for others to find relevant information. It is also possible that some changes has been made to the repo already, and issue or PR became outdated, but wasn't closed for some reason. This action helps with periodic review and closing of such stale items in automated way.

You may let maintainers handle this or verify current relevancy by yourself, to help with re-triage. Any activity will remove stale label so it won't be automatically closed at this point.

I will close it if no further activity occurs within the next 30 days.

github-actions[bot] avatar Jul 26 '24 12:07 github-actions[bot]