在 php-cs-fixer Netbeans 8.x 中排除数组对齐

Exclude array alignment in php-cs-fixer Netbeans 8.x

我正在使用 php-cs-fixerNetbeans 8.2。除一种情况外,它工作正常。我的数组格式如下

$array = [
    'asdf' => 'a',
    'a'    => 'b',
    'asd'  => 'c'
];

但是用php-cs-fixer修复后,都变成了

$array = [
    'asdf' => 'a',
    'a' => 'b',
    'asd' => 'c'
];

有没有办法只忽略这一段?

Is there any way to ignore this section only?

不行,没办法。查看对 PHP-CS-Fixer 问题 #3846: How to ignore specific lines?

的回复

This is a design decision that you shall not allow for exceptions per block of code. It may or may not be introduced in the future, depends on amount of ppl requesting it and one who would take a challenge to implement it.

Currently, if you are really in need, you can disable file from being fixed at all. But that would mean it would not be fixed by any other rules as well.

所以我想你可以更新现有的问题来添加你的声音,并希望他们倾听。

或者,您可以提出一个新问题,要求 PHP-CS-Fixer 中有一条在格式化数组时保留空格的新规则。在我看来,您的格式肯定比 PHP-CS-Fixer 的格式好。

为了在关联数组(旧规则align_double_arrow)中保持双箭头('=>')之后的对齐,使用 PHP-CS-Fixer 的 v2 或 v3 版本你需要 运行:

php-cs-fixer fix path \
--rules='{"binary_operator_spaces": {"operators": {"=>": "align_single_space_minimal"}}}'

您也可以对任何二元运算符执行此操作:

php-cs-fixer fix path \
--rules='{"binary_operator_spaces": {"default": "align_single_space_minimal"}}'

更多阅读: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/3.0/doc/rules/operator/binary_operator_spaces.rst