根据 PSR2,在多行数组的键和值之间留有空格的正确方法是什么。
$result = [
'key1' => 'value1',
'another_key' => 'value2',
'some_other_key' => 'value3'
];
对比
$result = [
'key1' => 'value1',
'another_key' => 'value2',
'some_other_key' => 'value3'
];
看起来第一个更具可读性,但我们有额外的空间。
最佳答案
根据:- 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.
注意:-
此线程中也陈述了同样的事情:- 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.
即使您检查此线程:- Coding Style Guide Additions
它只说明了关于添加 ,
没有关于添加空格的任何内容。
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.
关于php - PSR2 - 键和值之间的多行数组缩进,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47075076/