6

Should associative array => should be align with PHP-CS-Fixer ?

$array = [
    1    => 'a',
    '1'  => 'b',
    1.5  => 'c',
    true => 'd',
];

or

$array = [
    1 => 'a',
    '1' => 'b',
    1.5 => 'c',
    true => 'd',
];

I didn't find filter for that in https://github.com/FriendsOfPHP/PHP-CS-Fixer

Bouffe
  • 770
  • 13
  • 37
  • 1
    It's an invalid array, you can only use integer or string values as array keys; and a string key containing an integer value wil be silently converted to an integer, so all these will be cast to integer 1 and value `d` will overwrite value `a` for key `1` – Mark Baker Nov 12 '15 at 11:41
  • And the answer to your actual question probably depends on the coding standard that you're using – Mark Baker Nov 12 '15 at 11:41
  • In fact we don't care about the content, it's just a exemple take from http://php.net/manual/en/language.types.array.php – Bouffe Nov 12 '15 at 11:43

2 Answers2

9

This is now a proper way to run the align_double_arrow with newer versions of PHP-CS-Fixer (v2, v3), which will align array elements after the double arrow ('=>'):

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

On a configuration file, it would be something like:

'binary_operator_spaces' => [
    'operators' => [
        '=>' => 'align_single_space_minimal',
    ]
]

More to read: https://github.com/PHP-CS-Fixer/PHP-CS-Fixer/blob/master/doc/rules/operator/binary_operator_spaces.rst

boryn
  • 726
  • 9
  • 10
  • 1
    This is the current link: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/operator/binary_operator_spaces.rst – Keith Brink Jun 13 '22 at 19:55
5

For PHP-CS-Fixer v1, it used to be the option: align_double_arrow

For newer versions, check the other answer.

Bouffe
  • 770
  • 13
  • 37