Fixer to properly format multiline arrays #9436
stevekanger
started this conversation in
RFC
Replies: 0 comments
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
I recently created a fixer that properly formats arrays. You can view this fixer here https://github.com/stevekanger/PHP-CS-Fixer/blob/feature/multiline_array_format/src/Fixer/ArrayNotation/MultilineArrayFormatFixer.php. This fixer could potentially replace the current ArrayIndentationFixer.
My problem with the current implementation of array formatting is that its limited to just aligning indents This doesn't account for malformed arrays like so
I can go on but you get the point. I can't be the only one that deforms arrays when developing or decides to change a single line array to a multi line one. It is nice to not have to do the formatting yourself. This is a basic core function of pretty much every formatter for every language that php-cs-fixer currently doesn't have.
Performance of this fixer is fairly decent. In a page with around 7000 lines that is all arrays (also many deeply nested) the time to complete when all arrays need to be formatted on my machine is 0.421 while the current ArrayIndentationFixer is 0.362. In this case the current ArrayIndentationFixer is faster but not really noticeable to the human eye and the MultilineArrayFormatFixer has a lot more work to do. But when the arrays are already formatted the MultilineArrayFormatFixer is faster at 0.184 vs the ArrayIndentationFixer at 0.216. So it really just matters how many arrays need to be formatted. In most cases the MultilineArrayFormatFixer will be faster because it will not have to format every array on the page.
Tests are included in the feature/multiline_array_format branch of the forked repo. I think I have it working pretty well and most of the bugs are ironed out. Although I'm sure the are still some as I haven't been working on it long.
Even if your not interested I would still like some feedback on maybe how to improve performance, If you see any bugs that jump out initially, or even a feature that this doesn't have that should be implemented. I was going to try to retrofit this functionality into the existing indent fixer but it really does a similar but different thing and needed to be built on its own. Also wasn't sure if ArrayNotation is the proper place for this fixer since the ArrayIndentationFixer is in Whitespace.
Let me know if this is something that you may be interested in. If its not I will just package this as a standalone fixer for composer or something because I find it very handy.
0 votes ·
All reactions