Php-cs-fixer: BinaryOperatorSpacesFixer - Alignment in anonymous function in array not working as expected

Created on 30 Aug 2018  路  4Comments  路  Source: FriendsOfPHP/PHP-CS-Fixer

See the example below. I also created a gist to reproduce the example.

If you can point me to an approach how to fix it, I could probably contribute a PR.

The PHP version you are using ($ php -v):

PHP 7.2.8 (cli) (built: Jul 19 2018 12:15:24) ( NTS )
Copyright (c) 1997-2018 The PHP Group
Zend Engine v3.2.0, Copyright (c) 1998-2018 Zend Technologies
with Zend OPcache v7.2.8, Copyright (c) 1999-2018, by Zend Technologies

PHP CS Fixer version you are using ($ php-cs-fixer -V):

PHP CS Fixer 2.13.0 Yogi's BBQ by Fabien Potencier and Dariusz Ruminski

The command you use to run PHP CS Fixer:

./vendor/bin/php-cs-fixer fix --config=config.php example.php --using-cache=false

The configuration file you are using, if any:

<?php

$finder = PhpCsFixer\Finder::create();

return PhpCsFixer\Config::create()
    ->setRules([

        'binary_operator_spaces' => [
            'operators' => [
                '=' => 'align_single_space_minimal',
                '=>' => 'align_single_space_minimal',
            ]
        ],    ])
    ->setFinder($finder)
    ;

If applicable, please provide minimum samples of PHP code (as plain text, not screenshots):

  • before running PHP CS Fixer (no changes):
<?php

$working = 1;
$working2 = 2;


$foo = [
    'bar' => function () : void {
        $broken = 'broken';
        $broken2 = 'broken2';
    },
];.
  • with unexpected changes applied when running PHP CS Fixer:
<?php

$working  = 1;
$working2 = 2;


$foo = [
    'bar' => function () : void {
        $broken = 'broken';
        $broken2 = 'broken2';
    },
];
  • with the changes you expected instead:
<?php

$working  = 1;
$working2 = 2;


$foo = [
    'bar' => function () : void {
        $broken  = 'broken';
        $broken2 = 'broken2';
    },
];

kinbug

All 4 comments

Hi @hco ,
Thanks for reporting this issue :+1:

I think "in array" in the title can be changed to "inline":

<?php

// Inside a class method
$this->hook->into(RuleSetValidator::VALIDATE_RULE_ERROR, function (string $event, Rule $rule) {
    $error = 'Invalid rule: ' . $rule->getOrigin();
    $reason = '';

    if ($rule instanceof RuleInvalid) {
        $reason = ' ' . $rule->getReason();
    }

    $this->output->writeln("<red>{$error}</red>{$reason}");
});

See above, the $error and $reason do not align. However, when I extract the inline function into a variable, they do align:

<?php

// Inside a class method
$callable = function (string $event, Rule $rule) {
    $error  = 'Invalid rule: ' . $rule->getOrigin();
    $reason = '';

    if ($rule instanceof RuleInvalid) {
        $reason = ' ' . $rule->getReason();
    }

    $this->output->writeln("<red>{$error}</red>{$reason}");
};

$this->hook->into(RuleSetValidator::VALIDATE_RULE_ERROR, $callable);

In looking into this I have also discovered some more issues:

<?php

(function () {
    $a = 1;
    $aa = 1;
});
<?php

$a[function () {
    $a = 1;
    $aa = 1;
}];

are both not aligned properly.

This is due to this bit of code: https://github.com/FriendsOfPHP/PHP-CS-Fixer/blob/0257ccae7ddcbdd5f7d51de3ae92ffac02d0e1c1/src/Fixer/Operator/BinaryOperatorSpacesFixer.php#L574-L590

For the reported issue, it's quite easy, we can take out the last if statement and everything works correctly, however the other two aren't as simple, as tokens in for ($x = 1; ;) need to be ignored also

Hi,
Do we have the same behavior, some news about this issue?
thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ro0NL picture ro0NL  路  3Comments

teohhanhui picture teohhanhui  路  3Comments

UksusoFF picture UksusoFF  路  3Comments

vitek-rostislav picture vitek-rostislav  路  3Comments

EvgenyOrekhov picture EvgenyOrekhov  路  3Comments