plugin-php
plugin-php copied to clipboard
Chained method arguments split up unnecessarily
@prettier/plugin-php v0.14.3 Playground link
Input:
<?php
$query
->selectRaw('count(*) as `count`, date_format(`created_at`, "%Y-%m-01") as `date`')
->from('quote_searches')
->where('account_id', $accountId)
->where('created_at', '>=', Carbon::now()->startOfMonth()->subMonths($months))
->groupBy('date');
Output:
<?php
$query
->selectRaw('count(*) as `count`, date_format(`created_at`, "%Y-%m-01") as `date`')
->from('quote_searches')
->where('account_id', $accountId)
->where(
'created_at',
'>=',
Carbon::now()
->startOfMonth()
->subMonths($months),
)
->groupBy('date');
I'm specifically referring to the second where
call. Prettier leaves this alone, so I assume it must be caused by the php plugin. It's behaving as if the print-width is much less. (I use 120, but this even fits in 80.)
Prettier 2.1.2 Playground link
--arrow-parens avoid
--parser babel
--print-width 120
--single-quote
--tab-width 4
--trailing-comma all
Input:
$query
.selectRaw('count(*) as `count`, date_format(`created_at`, "%Y-%m-01") as `date`')
.from('quote_searches')
.where('account_id', $accountId)
.where('created_at', '>=', Carbon.now().startOfMonth().subMonths($months))
.groupBy('date');
Output:
$query
.selectRaw('count(*) as `count`, date_format(`created_at`, "%Y-%m-01") as `date`')
.from('quote_searches')
.where('account_id', $accountId)
.where('created_at', '>=', Carbon.now().startOfMonth().subMonths($months))
.groupBy('date');
Same in latest version. Looks bad, especially in IF statements.
I totally agree. Potentially, we could check how Prettier for JS handles this and port some of the logic to the plugin. cc @evilebottnawi
Good idea
Any news on this? Would be really great to only split chained methods if we're running out of space
Hi @arnoson, I agree it would be nice to get this fixed. I currently don’t have time to work on this it myself, but I’d do my best to help anyone who’d be interested in taking this on.
Thanks for the reply @czosel , unfortunanetly I'm also busy at the moment, might take a look into this if I find some time. Can you show me where exactly the linebreaks get inserted?
Also hoping for an update on this issue. We recently began using Prettier for our codebase, both our PHP and JS code. Successive chained method calls on the JS side get wrapped to one method call per line only if the full line exceeds the line length limit, but the same thing occurs on the PHP side with >2 chained method calls, regardless of how long the line is. It's had an adverse affect on our codebase. It would be great if the PHP behavior could be consistent with the JS behavior. Thanks for your consideration!