Skip to content
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

Chained method arguments split up unnecessarily #1545

Closed
hackel opened this issue Sep 23, 2020 · 7 comments
Closed

Chained method arguments split up unnecessarily #1545

hackel opened this issue Sep 23, 2020 · 7 comments

Comments

@hackel
Copy link
Contributor

hackel commented Sep 23, 2020

@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');
@eldair
Copy link
Contributor

eldair commented Oct 22, 2020

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

@czosel
Copy link
Collaborator

czosel commented Oct 22, 2020

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

@alexander-akait
Copy link
Member

Good idea

@arnoson
Copy link

arnoson commented Mar 8, 2023

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

@czosel
Copy link
Collaborator

czosel commented Mar 8, 2023

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.

@arnoson
Copy link

arnoson commented Apr 17, 2023

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?

@slowspin59
Copy link

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

6 participants