diff --git a/src/Fixer/Operator/BinaryOperatorSpacesFixer.php b/src/Fixer/Operator/BinaryOperatorSpacesFixer.php index dd0e3ac52fd..940b9a94406 100644 --- a/src/Fixer/Operator/BinaryOperatorSpacesFixer.php +++ b/src/Fixer/Operator/BinaryOperatorSpacesFixer.php @@ -523,8 +523,8 @@ private function injectAlignmentPlaceholders(Tokens $tokens, int $startAt, int $ for ($index = $startAt; $index < $endAt; ++$index) { $token = $tokens[$index]; - $content = $token->getContent(); + if ( strtolower($content) === $tokenContent && $this->tokensAnalyzer->isBinaryOperator($index) @@ -543,7 +543,7 @@ private function injectAlignmentPlaceholders(Tokens $tokens, int $startAt, int $ continue; } - if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH])) { + if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH, T_ELSEIF])) { $index = $tokens->getNextMeaningfulToken($index); $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index); @@ -569,7 +569,7 @@ private function injectAlignmentPlaceholdersForArrow(Tokens $tokens, int $startA for ($index = $startAt; $index < $endAt; ++$index) { $token = $tokens[$index]; - if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH])) { + if ($token->isGivenKind([T_FOREACH, T_FOR, T_WHILE, T_IF, T_SWITCH, T_ELSEIF])) { $index = $tokens->getNextMeaningfulToken($index); $index = $tokens->findBlockEnd(Tokens::BLOCK_TYPE_PARENTHESIS_BRACE, $index); diff --git a/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php b/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php index 0a200041147..9cbc8867b8b 100644 --- a/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php +++ b/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php @@ -129,7 +129,7 @@ public function provideConfiguredCases(): array ], [ ' 2]; foreach ([ 1 => 2, 2 => 3, @@ -137,7 +137,7 @@ public function provideConfiguredCases(): array $var[] = [$i => $bar]; }', '2]; foreach ([ 1=> 2, 2 =>3, @@ -798,7 +798,7 @@ function b( for ($i = 0; $i < 10; $i++) { $aa = 2; - $a[$b] = array(); + $a[$b] = array(12); }', ' $x + 3; $f = 123; +', + ], + [ + ' 100) { + $_data = \'100+\'; +} elseif (($c = count($array)) > 0) { + $_data = \'0+\'; +} +', + ], + [ + ' 100) { + $closure = fn ($x = 1) => $x + 3; +} elseif (($c = count($array)) > 0) { + $closure = fn ($x = 1) => $x ** 3; +} ', ], ]; @@ -1973,6 +2031,26 @@ function foo() {} self::STATUS_INVALID_7 => [(2+3)=> "III", "description" => "invalid file syntax, file ignored"], ];', ], + [ + ' function() { + foreach([$a => 2] as $b) { + $bv = [ + $b => 2, + $cc => 3, + ]; + }}, 2 => 3]; +', + ' function() { + foreach([$a => 2] as $b) { + $bv = [ + $b => 2, + $cc => 3, + ]; + }}, 2 => 3]; +', + ], ]; }