Associative array align => with PHP-CS-Fixer
Asked Answered
M

2

5

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

Milquetoast answered 12/11, 2015 at 11:39 Comment(3)
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 1Emarie
And the answer to your actual question probably depends on the coding standard that you're usingEmarie
In fact we don't care about the content, it's just a exemple take from php.net/manual/en/language.types.array.phpMilquetoast
M
5

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

For newer versions, check the other answer.

Milquetoast answered 12/11, 2015 at 11:52 Comment(0)
R
14

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

Radiobroadcast answered 31/3, 2021 at 10:56 Comment(1)
This is the current link: github.com/FriendsOfPHP/PHP-CS-Fixer/blob/master/doc/rules/…Inoculum
M
5

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

For newer versions, check the other answer.

Milquetoast answered 12/11, 2015 at 11:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.