-
-
Notifications
You must be signed in to change notification settings - Fork 1.6k
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
feat: multiline long array #6930
base: master
Are you sure you want to change the base?
feat: multiline long array #6930
Conversation
Fixed most of the problems with the CI but I can't seem to find how to test |
@bachinblack hi, thank you for the contribution. From top of my head I can't tell right now if we already had such a fixer, but I'm on vacation now and can look deeper later (Thursday+). Anyway, I accepted workflow so you'll get CI feedback. In terms of testing priority you need integration test like this. PS. Please don't cry if it doesn't get merged, it's nice knowledge anyway (what you have learned during the process) 😊. |
Hey ! Thanks for your help, I managed to add integration tests for my fixer. |
0b3b2d6
to
9085f61
Compare
/** | ||
* {@inheritdoc} | ||
* | ||
* Must run before ArrayIndentationFixer, NoTrailingCommaInSinglelineFixer, TrailingCommaInMultilineFixer, WhitespaceAfterCommaInArrayFixer. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixer should also come before binary_operator_spaces
.
9fadc78
to
dd39c01
Compare
Looks great @bachinblack 🎉 Thanks for your time 👍 Any news on this feature? |
|
||
public function isCandidate(Tokens $tokens): bool | ||
{ | ||
return $tokens->isAnyTokenKindsFound([T_ARRAY, CT::T_ARRAY_SQUARE_BRACE_OPEN]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe fixer should react also to CT::T_DESTRUCTURING_SQUARE_BRACE_OPEN
and handle code like:
// Before
[$foo, $bar, $baz] = $arr;
// After
[
$foo,
$bar,
$baz
] = $arr;
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Makes sense. I added the logic and I made a test for it, does it work for you ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add such case to tests/Fixtures/Integration/priority/multiline_long_array,array_indentation.test
too, so we can be sure that it works as expected 🙂.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will try to do it when I find some time this week
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just added the test but I'm not sure what it was supposed to do.
I was expecting :
// from
[$foo, $bar, $baz] = $arr;
// to
[
$foo,
$bar,
$baz
] = $arr;
But I got :
[
$foo,
$bar,
$baz
] = $arr;
After looking around, it seems like ArrayIndentation
does not work with destructuring, right ?
So is this what was expected ? If not, is the test still relevant ?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It'll work when #7405 is merged, so we need to update integration test here when it's done 🙂.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got you 👍 I just added the test
5a6c6c7
to
3531659
Compare
@bachinblack in general it looks OK for me, but we had discussion about it with @kubawerlos and we think @SpacePossum's approach in #6387 was interesting, to have 2 separate conditions for triggering rule and strategy to choose (at least one, both). Your PR is focused on array length, but can be extended later with new options - are you interested in this? If yes, would you prefer to do it in this PR or later in second one? Of course we can discuss everything before so the outcome is as expected. |
Hey @Wirone ! I wouldn't use this rule with an elements count myself because I don't think it's as reliable as a characters count but I guess it would be normal to give users the choice and it doesn't seem that hard to implement (though I believe there will be many more tests). For the param names, I can get closer to @SpacePossum 's naming I think I'd be more comfortable merging it as it is now (after having renamed the current param to |
As I proposed earlier, I rename I understand fixers' default configuration can't just do nothing and there has to be a default config, so I chose to keep the default behaviour to Now, should we add |
c25b029
to
2629652
Compare
2629652
to
e151f4e
Compare
e151f4e
to
2a8967b
Compare
Re-review required, previous comments were addressed
64c6b1c
to
989f411
Compare
Hey ! I've caught up with master so it should be all good now, would anyone be so kind as to review my PR once more ? (hopefully it will be the last time haha) |
This looks great seems like just what I'm looking for! Hoping it gets merged into release. 🤞 |
I'm hoping this gets merged too as the current array formatting capabilities prevent me from fully integrating php-cs-fixer into my project. |
Mhh, I guess that's subjective then, thanks for answering anyway ! |
97e37c1
to
142e337
Compare
142e337
to
ada42e9
Compare
Fixes #1217
This is my very first PR on an open source project so please be patient 😄
Also, it's possible that my PR is a duplicate of an already existing fixer, in which case I'll probably cry.
This PR is a possible solution to #4485 (issue I stumbled upon while looking for such a fixer).
It formats arrays according to the
max_length
argument (which is the maximum authorized length for a single line array).Why I think it's good.
I recently used rector to upgrade my code and it turned my attributes into gigantic one-liners. Definitely not fixing them by hand.
Apart from that, I could use some logic on my decision-making regarding array formats.
Scope
The
max_length
argument is defined in characters and excludes whitespaces. Another possible option would be to count the number of items (not for me, though).In the issue (Single line array to multiple line array fixer #4485) it is also proposed to make an array multiline if it contains key/value pairs. To me it's not a great option as the type of an array hardly relates to its size but it's only fair to mention it.
It only works with arrays but it could also be useful for function arguments and chained method calls.
Possibly in the same fixer, given extra configuration (the code would be very close), possibly in separate fixers.
Fixer priority (50)
This fixer leans on
ArrayIndentationFixer
,WhitespaceAfterCommaInArrayFixer
,NoWhitespaceBeforeCommaInArrayFixer
,TrailingCommaInMultilineFixer
andNoTrailingCommaInSinglelineFixer
to output nicely formatted code. It contains no logic at all regarding indentation or commas, is it okay ? Or should I also add this kind of logic to it ? Feels like duplication so I guess not.Thanks for your time !