简体   繁体   中英

PSR2 - multiline array indentation between key and value

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM