From f7750fdeab7a4ef0bd60a23eee7aa89dccc3aea2 Mon Sep 17 00:00:00 2001 From: Vincent Langlet Date: Tue, 16 Nov 2021 10:37:03 +0000 Subject: [PATCH] [BinaryOperatorSpacesFixer] Fix align of `=` inside calls of methods --- .../Operator/BinaryOperatorSpacesFixer.php | 12 +++++++-- .../BinaryOperatorSpacesFixerTest.php | 26 +++++++++++++++++++ 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/src/Fixer/Operator/BinaryOperatorSpacesFixer.php b/src/Fixer/Operator/BinaryOperatorSpacesFixer.php index a93df84522f..1da5fa8ad07 100644 --- a/src/Fixer/Operator/BinaryOperatorSpacesFixer.php +++ b/src/Fixer/Operator/BinaryOperatorSpacesFixer.php @@ -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]; @@ -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; diff --git a/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php b/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php index 6e83aa569d2..0a200041147 100644 --- a/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php +++ b/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php @@ -1401,6 +1401,32 @@ function b( $a[$b] = array(); }', ], + [ + ' $x + 3; +$f = 123; +', + ], ]; }