12

What is the correct way according to PSR2 for having spaces between keys and values of multiline array.

$result = [
    'key1'           => 'value1',
    'another_key'    => 'value2',
    'some_other_key' => 'value3'
];

vs

$result = [
    'key1' => 'value1',
    'another_key' => 'value2',
    'some_other_key' => 'value3'
];

It looks that the first one is more readable, but we are having extra spaces.

dav
  • 8,931
  • 15
  • 76
  • 140

1 Answers1

6

According to:-PSR-2 multiline array indentation missing

PSR-2 doesn't define how arrays should be indented, or if they should even be indented at all, so PHPCS can't enforce any rules in this area.

When I have asked about arrays in other areas of the standard, the response was to treat them like a single object, and ignore them. So many PHPCS checks for PSR-2 actually skip right over arrays and don't do any checking inside them either.

So this isn't a bug, and it's also not something I can add to PHPCS because PSR-2 doesn't define an array formatting standard.

Note:-

The same thing is stated in this thread too:- Question on proper Array syntax

There's no correct answer, beacuse the PSR is silent on the matter, but I think your assumption is reasonable. It's certainly my practice.

Even if you check this thread:- Coding Style Guide Additions

It only states about adding , not about anything regarding adding spaces.

Arrays that span across multiple lines can have a trailing comma to make sure that adding new rows does not change the previous row, as well.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98