1

To align or not to align...

Pros and cons of the following approaches.

I like to use first case. Because I think it's optimal, but someone else thinks otherwise.

  • when adding a new constant to the file - diff is 1 string, not more
  • clearer, more beautiful
  • chain call (-> -> -> ->) - is clearer

What do you think? May be there is PSR about it?

Case style 1

class A {

    const AB = 1;
    const PAGE = 2;
    const PP_SOMETHING_VERY_LONG = 3;

    public function testLoooooooooong()
    {
        $a = 1;
        $somethingWeryLong = 2;
        $c = 3;
        $res = $a + $c;

        $b = 1;
        $n = 100;

        return 0;
    }

    public function test2($objectttttttttttttttLooooooong) 
    {
        $objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
            ->call(
                $this->testLoooooooooong(),
                $this->testLoooooooooong2(),
                $this->testLoooooooooong3()
            )
            ->call(
                $this->testLoooooooooong(),
                $this->testLoooooooooong2(),
                $this->testLoooooooooong3()
            );
    }

}

Case style 2

class A {

    const AB                     = 1;
    const PAGE                   = 2;
    const PP_SOMETHING_VERY_LONG = 3;

    public function testLoooooooooong() {
        $a                 = 1;
        $somethingWeryLong = 2;
        $c                 = 3;
        $res               = $a + $c;

        $b = 1;
        $n = 100;

        return 0;
    }

    public function test2($objectttttttttttttttLooooooong) {
        $objectttttttttttttttLooooooong->callSooooooooomethingLooooooong()
                                       ->call(
                                           $this->testLoooooooooong(),
                                           $this->testLoooooooooong2(),
                                           $this->testLoooooooooong3()
                                       )
                                       ->call(
                                           $this->testLoooooooooong(),
                                           $this->testLoooooooooong2(),
                                           $this->testLoooooooooong3()
                                       );
    }

}
Blackbam
  • 17,496
  • 26
  • 97
  • 150
Ivan
  • 625
  • 1
  • 5
  • 18
  • I prefer Case 2. Often I align those '='. But not always. But often. About indenting I always use only 4 spaces to indent the code when it should be indented. This goes for example for content inside IF. I do NOT use TAB for indent. It is not reommended. – halojoy Dec 29 '17 at 12:44
  • Without actually testing it, example 1 seems to match PSR2 – Mark Baker Dec 29 '17 at 13:00
  • I have vote to close this question, because this is primarily a quesiton of style and totally opinion based. The only thing you have to do is make it consistent across your files – Philipp Dec 29 '17 at 13:01
  • I personally align the first row containing `->` in the next line, when I call multiple on the same instance. – Markus Zeller Mar 13 '23 at 08:39

1 Answers1

1

Official answer: There is PSR-2 about styling PHP code, you can find it here: http://www.php-fig.org/psr/psr-2/

Beside of those recommendations, it is totally up to the coder how you like to style your code. This is only about conventions.

Within modern IDEs (IntelliJ, Netbeans, Eclipse, PHPStorm etc.) you can reformat code your code automatically to the way you prefer (e.g. with a shortcut like ALT+L).

The opinionated part of the answer: I would prefer the = style as well as the -> style of Case 1, because it is annoying to handle all those spaces and tabs from Case 2.

However I prefer

public function testLoooooooooong() {

}

from case 2 over

public function testLoooooooooong() 
{

}

from case 1 for the same reason. I do not want to write extra tabs and I do not think that it makes the code more readable. Anyhow this is all a matter of taste and many would also prefer case 1 here.

Blackbam
  • 17,496
  • 26
  • 97
  • 150