plugin-php icon indicating copy to clipboard operation
plugin-php copied to clipboard

Chained method arguments split up unnecessarily

Open hackel opened this issue 3 years ago • 7 comments

@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');

hackel avatar Sep 23 '20 18:09 hackel

Same in latest version. Looks bad, especially in IF statements.

eldair avatar Oct 22 '20 10:10 eldair

I totally agree. Potentially, we could check how Prettier for JS handles this and port some of the logic to the plugin. cc @evilebottnawi

czosel avatar Oct 22 '20 11:10 czosel

Good idea

alexander-akait avatar Oct 22 '20 11:10 alexander-akait

Any news on this? Would be really great to only split chained methods if we're running out of space

arnoson avatar Mar 08 '23 13:03 arnoson

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.

czosel avatar Mar 08 '23 16:03 czosel

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?

arnoson avatar Apr 17 '23 13:04 arnoson

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!

slowspin59 avatar Jan 25 '24 23:01 slowspin59