12

What is allowed between these two:

$value = 'value';
$user = 'John';
$timestamp = 1480927909;
$day = date('Y-m-d', $timestamp);

or

$value     = 'value';
$user      = 'John';
$timestamp = 1480927909;
$day       = date('Y-m-d', $timestamp);
  • 1
    as far as I know it, it doesn't require it. the only thing that must be aligned vertically are the phpdoc tags, but that's outside the psr-2 specification – Federkun Dec 05 '16 at 09:04
  • 5
    Imo, it drastically improves readability so I always do this kind of aligning. – Daniel W. Dec 05 '16 at 10:07
  • 6
    @DanFromGermany - it only improves readability in cases where the names are roughly the same length. As soon as you need to do it with a dataset where one or two of the elements have names that are much longer than the others, then it can actually start decreasing readability. It can also screw with your diffs if you need to adjust the offset of the `=` sign at some point in the future. For these reasons, I like use this kind of aligning sometimes, but certainly not always. – Simba Dec 05 '16 at 10:14
  • Thanks guys, I noticed that Laravel 5.1, 5.2 and 5.3 uses PSR-2, but vertical alignment is removed from last version 5.3, so it seems it is not specified by PSR-2 standard. – Marko Milivojevic Dec 05 '16 at 10:26
  • 4
    The readability of these is highly subjective. For me, the aligned version is much less readable than the one indented by a single space character. Not even regarding the similar principle for function arguments and parameters.. – helvete Apr 13 '18 at 11:34
  • 1
    Robert "Uncle Bob" Martin argues persuasively against this sort of horizontal alignment in _Clean Code_: "... you are tempted to read down the list of variable names without looking at [the values]." I find this to be true. I read the unaligned example left-to-right, top-to-bottom, but the aligned example top-to-bottom first, then left-to-right. It's like `[$value, $user, $timestamp, $day] = ['value', 'John', 1480927909, date('Y-m-d', $timestamp)]`, which is hard to understand. – CJ Dennis Nov 29 '18 at 23:29

1 Answers1

14

PSR-2 does not have specific rules for this kind of inter-line alignment:

From the PSR-2 Conclusion:

There are many elements of style and practice intentionally omitted by this guide. These include but are not limited to:

  • Declaration of global variables and global constants
  • Declaration of functions
  • Operators and assignment
  • Inter-line alignment
  • Comments and documentation blocks
  • Class name prefixes and suffixes
  • Best practices

Future recommendations MAY revise and extend this guide to address those or other elements of style and practice.

For what it's worth, inter-line alignment was discussed for PSR-1 by the PHP-FIG Group, but was removed from the final version:

The way-back original long-form PSR-1 covered inter-line alignment, globals, ternaries, assignment, and lots of other things. Those ended up getting removed for various reasons; their epitaph is at the conclusion of PSR-2.

Community
  • 1
  • 1
Kirk Beard
  • 9,569
  • 12
  • 43
  • 47