Php-cs-fixer: binary_operator_spaces not working in closure type argument

Created on 17 May 2018  ·  4Comments  ·  Source: FriendsOfPHP/PHP-CS-Fixer

PHP => 7.2.4
PHP-CS-FIXER=> 2.11.1

Command

php-cs-fixer fix /path/to/file.php

Config

<?php

return PhpCsFixer\Config::create()
                        ->setRiskyAllowed(true)
                        ->setRules(
                            [
                                '@Symfony' => true,
                                '@Symfony:risky' => false,
                                '@PHP71Migration' => true,
                                'array_syntax' => ['syntax' => 'short'],
                                'array_indentation' => true,
                                'dir_constant' => true,
                                'heredoc_to_nowdoc' => true,
                                'linebreak_after_opening_tag' => true,
                                'modernize_types_casting' => true,
                                'no_multiline_whitespace_before_semicolons' => true,
                                'no_unreachable_default_argument_value' => true,
                                'no_useless_else' => true,
                                'no_useless_return' => true,
                                'ordered_class_elements' => true,
                                'ordered_imports' => true,
                                'phpdoc_add_missing_param_annotation' => ['only_untyped' => false],
                                'phpdoc_order' => true,
                                'doctrine_annotation_braces' => true,
                                'doctrine_annotation_indentation' => true,
                                'doctrine_annotation_spaces' => true,
                                'psr4' => true,
                                'no_php4_constructor' => true,
                                'no_short_echo_tag' => true,
                                'declare_equal_normalize' => [
                                    'space' => 'single'
                                ],
                                'binary_operator_spaces' => [
                                    'default' => 'align_single_space_minimal'
                                ],
                                'concat_space' => [
                                    'spacing' => 'one'
                                ],
                                'no_trailing_whitespace' => true,
                                'no_whitespace_in_blank_line' => true,
                                'semicolon_after_instruction' => true,
                                'align_multiline_comment' => true,
                                'doctrine_annotation_array_assignment' => true,
                                'general_phpdoc_annotation_remove' => ['annotations' => ["author", "package"]],
                                'list_syntax' => ["syntax" => "short"],
                                'phpdoc_types_order' => ['null_adjustment'=> 'always_last'],
                                'single_line_comment_style' => true,
                            ]
                        )
                        ->setCacheFile(__DIR__.'/.php_cs.cache')
                        ->setFinder(
                            PhpCsFixer\Finder::create()
                                             ->in(__DIR__)
                        );

Needed rule

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

First example (when it works in the closure)

// before
$foo = function () {
    $bar['foo']['key_1'] = 1;
    $barFoo['foofoo']['key_1'] = 1;
};
// after fix
$foo = function () {
    $bar['foo']['key_1']       = 1;
    $barFoo['foofoo']['key_1'] = 1;
};

And not work in this case

// before and after will be equal
foo(function () {
    $bar['foo']['key_1'] = 1;
    $barFoo['foofoo']['key_1'] = 1;
});
kinquestion

All 4 comments

Hi @xenmayer :)

I did a little test and what you see is actually expected. The option 'align_single_space_minimal' is meant to still align the statements but when doing so using the minimal amount of spaces needed to do so.
If you want to unalign the statements but still keep one space, as your samples show, you can use the option 'single_space'

PS. The docs might be a bit minimal for this rule as I'm not the best in writing those, so please let me know if you've suggestions for improvements.

@SpacePossum Thanks for the speedy reaction) I did tests with all the behaviors of binary_operator_spaces.
You did not understand what I mean)
I expect the same fixing behavior for my two examples.

I will try to explain more simply.
I have two code snippets:

$foo = function () {
    $bar['foo']['key_1'] = 1;
    $barFoo['foofoo']['key_1'] = 1;
};
foo(function () {
    $bar['foo']['key_1'] = 1;
    $barFoo['foofoo']['key_1'] = 1;
});

and after fixing I got:

$foo = function () {
    $bar['foo']['key_1']       = 1;
    $barFoo['foofoo']['key_1'] = 1;
};
foo(function () {
    $bar['foo']['key_1'] = 1;
    $barFoo['foofoo']['key_1'] = 1;
});

but expect

$foo = function () {
    $bar['foo']['key_1']       = 1;
    $barFoo['foofoo']['key_1'] = 1;
};
foo(function () {
    $bar['foo']['key_1']       = 1;
    $barFoo['foofoo']['key_1'] = 1;
});

Thank you!

Thanks @xenmayer for the additional cases, I indeed misread your report! sorry for that.
I've formalized the tests into unit test cases here; https://github.com/FriendsOfPHP/PHP-CS-Fixer/pull/3762

This PR however does not provide a fix (yet).

@SpacePossum thank you! I hope somebody fix it soon)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

grachevko picture grachevko  ·  3Comments

EvgenyOrekhov picture EvgenyOrekhov  ·  3Comments

UksusoFF picture UksusoFF  ·  3Comments

kcloze picture kcloze  ·  3Comments

ro0NL picture ro0NL  ·  3Comments