php - PSR2 - 键和值之间的多行数组缩进

标签 php psr-2

根据 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/

相关文章:

php - 如何使用 PHP 将 MySQL 表备份到转储中?

PHP:获取年份的最后一位数字(示例:2015 == 5)

php - PSR-2 缩进规则的说明

php - 我应该使用 new self 还是 new static?

php - PSR 和构造函数可见性

php - 数组 PHP 中 undefined offset (在 for 迭代中)

Javascript 验证 (gen_validatorv4.js) - 基于复选框结果

php - phpcs 的 PSR-2 编码标准和 Symfony2 编码标准之间的主要区别是什么?

php - 根据 PhpStorm 上的 PSR-2 在文件末尾添加一个空行

php - 检查 Twitter API 是否已关闭(或者整个站点是否已关闭)