5

I'm currently attempting to configure PhpStorm to produce fully-PSR-2-compliant code, however its formatter is tripping up on long lines which contain functions with multiple parameters.

When I run the formatter, it converts this:

return ($thisIsALongLine || functionCall($arg1, $arg2));

into this:

return ($thisIsALongLine || functionCall(
        $arg1,
        $arg2
    ));

However, what I want is this:

return ($thisIsALongLine || functionCall(
    $arg1,
    $arg2
));

Does anyone know which formatter option tells it to further indent multi-line function calls in this instance?

Note: Usually, I'd format the above as this:

return ($thisIsALongLine
    || functionCall($arg1, $arg2));

However, that just bypasses the extra indentation issue, which I'd still need to fix for other situations.

Edit: This is the state of Wrapping and Braces, as requested by @LazyOne below:

Wrapping and Braces, part 1 Wrapping and Braces, part 2

Edit 2: Examples of two different types of line which PhpStorm's formatter isn't handling correctly. (Disclaimer: This is old code from a legacy system.)

Firstly, this:

if ($validateBudget && $this->getFinancialPeriodService()->validateBudget($formModel->action, $formModel->estimatedBudget, $formModel->startDate, $formModel->endDate, $formModel->isFirstPeriod)) {

becomes this:

if ($validateBudget && $this->getFinancialPeriodService()
    ->validateBudget($formModel->action, $formModel->estimatedBudget,
        $formModel->startDate, $formModel->endDate, $formModel->isFirstPeriod)) {

when I would expect this based on the settings above:

if ($validateBudget && $this->getFinancialPeriodService()
    ->validateBudget(
        $formModel->action,
        $formModel->estimatedBudget,
        $formModel->startDate,
        $formModel->endDate,
        $formModel->isFirstPeriod
    )
) {

Secondly, if you enable alignment on chained methods, then this:

if ($evaluation->getExpert() != NULL && ($evaluation->getExpert()->getStatusId() == Evaluation::STATUS_ASSIGNED || $evaluation->getEvaluationStage() == Evaluation::STAGE_PROPOSED && CoreDateUtils::dateIsPast($proposal->getCalendar()->getStage1StartDate()) == false || $evaluation->getEvaluationStage() == Evaluation::STAGE_IN_PROGRESS && CoreDateUtils::dateIsPast($proposal->getCalendar()->getStage2StartDate()) == false)) {

is reformatted to this:

if ($evaluation->getExpert() != null && ($evaluation->getExpert()
                                                    ->getStatusId() == Evaluation::STATUS_ASSIGNED || $evaluation->getEvaluationStage() == Evaluation::STAGE_PROPOSED && CoreDateUtils::dateIsPast($proposal->getCalendar()
                                                                                                                                                                                                            ->getStage1StartDate()) == false || $evaluation->getEvaluationStage() == Evaluation::STAGE_IN_PROGRESS && CoreDateUtils::dateIsPast($proposal->getCalendar()
                                                                                                                                                                                                                                                                                                                                                            ->getStage2StartDate()) == false)) {

when I would expect this:

if ($evaluation->getExpert() != null
    && ($evaluation->getExpert()->getStatusId() == Evaluation::STATUS_ASSIGNED
    || $evaluation->getEvaluationStage() == Evaluation::STAGE_PROPOSED
    && CoreDateUtils::dateIsPast($proposal->getCalendar()->getStage1StartDate()) == false
    || $evaluation->getEvaluationStage() == Evaluation::STAGE_IN_PROGRESS
    && CoreDateUtils::dateIsPast($proposal->getCalendar()->getStage2StartDate()) == false)
) {

To be honest, at this point I suspect a bug in the formatter, so will open a ticket with JetBrains, however I'll leave this open in case anyone does know why it over-/underformats things.

Benjamin Nolan
  • 1,170
  • 1
  • 10
  • 20
  • Opinion based comment: I find all of those formattings not good. Keep return lines short and rather introduce an extra variable. I think a return should fit in a single line, I'd put the `functionCall()` in an extra variable. – Daniel W. Nov 21 '17 at 17:19
  • Whilst I agree, it also does it for long `if-statements` as well, which is what actually caused me to post the question, however it was a return statement I was looking at which it kept de-formatting whilst writing the post. Either way, it's still irritating and coding around it isn't really the fix I'm after in this particular instance, even if it is what I would usually do. :) (This is reformatting other people's code during a refactor.) – Benjamin Nolan Nov 21 '17 at 17:26
  • 1) Have you tried just importing built-in PSR-1/2 settings? In theory they should have it all setup by default. 2) Show your Code Style settings -- "Wrapping and braces" tab. – LazyOne Nov 21 '17 at 17:47
  • @LazyOne Yeah, was the first thing I tried, and unfortunately it didn't change anything. :/ I'm back home now, however I'll add a screenshot in the morning, assuming this hasn't been closed between now and then. – Benjamin Nolan Nov 21 '17 at 18:50
  • I also keep my `if` statements on a single line, especially boolean values which I prefix with `$is` like `if ($isValid && $isConfirmed)`. Anyways, it is no direct solution, good luck with the settings :) It might also help to contact intellij directly. – Daniel W. Nov 22 '17 at 09:11
  • @LazyOne: I've added screenshots of the Wrapping and Braces panel to the question. If you spot anything odd, please let me know! – Benjamin Nolan Nov 23 '17 at 09:24
  • @BenjaminNolan The options I thought of are OK. Can you please: 1) share the sample code that reformats wrongly 2) also export and share the actual Code Style . Right now in my tests the return line does not change at all -- it stays as is. – LazyOne Nov 23 '17 at 09:45
  • @LazyOne: If you change your maximum line length to 50 characters, you should see this behaviour. The line on which that was based (which I now can't find--it's a huge project!) was over our 120 character hard limit, which triggers PhpStorm's formatter. I've also added some examples where it's most definitely not doing what it should be doing. – Benjamin Nolan Nov 23 '17 at 17:45
  • @BenjaminNolan **1)** *"Firstly, this:"* -- as per my understanding it's expected behaviour -- see this ticket https://youtrack.jetbrains.com/issue/WI-38781 **2)** *"Secondly,"..."* Not sure about this at all. I mean -- I absolutely agree with expected formatting. Feel free to submit tickets to https://youtrack.jetbrains.com/issues/WI (but check for existing first) – LazyOne Nov 23 '17 at 18:04
  • @LazyOne Hmm… The first result conflicts with what I'd expect with the multi-line function options, though, which phpcs does indeed flag up on that line. Will search / submit a ticket re: case 2 though, and mention case 1 in passing. TY for the link. :) – Benjamin Nolan Nov 23 '17 at 19:40

0 Answers0