Skip to content

Commit

Permalink
bug #6112 [BinaryOperatorSpacesFixer] Fix align of = inside calls o…
Browse files Browse the repository at this point in the history
…f methods (VincentLanglet)

This PR was squashed before being merged into the master branch (closes #6112).

Discussion
----------

[BinaryOperatorSpacesFixer] Fix align of `=` inside calls of methods

Hi. I found a bug when using align/align_with_space_minimal for `=`.

Before, any time a `(` was found, the check was skipped until the `)`.

Now, I do this only for `(` for function definition and for/foreach/if/while/...

So
```
$this->foo(function () {
$a = 1;
$aa = 2;
})
```
aligns the `=`.

It fix my personal issue (I added a test), and I think it will fix the issue #5667

Commits
-------

f7750fd [BinaryOperatorSpacesFixer] Fix align of `=` inside calls of methods
  • Loading branch information
SpacePossum committed Feb 14, 2022
2 parents 37e2cbd + f7750fd commit ae42466
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/Fixer/Operator/BinaryOperatorSpacesFixer.php
Expand Up @@ -531,6 +531,11 @@ private function fixAlignment(Tokens $tokens, array $toAlign): void

private function injectAlignmentPlaceholders(Tokens $tokens, int $startAt, int $endAt, string $tokenContent): void
{
$functionKind = [T_FUNCTION];
if (\PHP_VERSION_ID >= 70400) {
$functionKind[] = T_FN;
}

for ($index = $startAt; $index < $endAt; ++$index) {
$token = $tokens[$index];

Expand All @@ -545,13 +550,16 @@ private function injectAlignmentPlaceholders(Tokens $tokens, int $startAt, int $
continue;
}

if ($token->isGivenKind(T_FUNCTION)) {
if ($token->isGivenKind($functionKind)) {
++$this->deepestLevel;
$index = $tokens->getNextTokenOfKind($index, ['(']);
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);

continue;
}

if ($token->equals('(')) {
if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH])) {
$index = $tokens->getNextMeaningfulToken($index);
$index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index);

continue;
Expand Down
26 changes: 26 additions & 0 deletions tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
Expand Up @@ -1401,6 +1401,32 @@ function b(
$a[$b] = array();
}',
],
[
'<?php
m(
function ()
{
$d["a"] = 1;
$d["abc"] = 2;
}
);
',
'<?php
m(
function ()
{
$d["a"] = 1;
$d["abc"] = 2;
}
);
',
],
[
'<?php
fn ($x = 1) => $x + 3;
$f = 123;
',
],
];
}

Expand Down

0 comments on commit ae42466

Please sign in to comment.