Repository: FriendsOfPHP/PHP-CS-Fixer (php)

Issue:
v3.7.0 binary_operator_spaces and elseif
## Bug report

PHP CS Fixer 3.7.0 #StandWithUkraine️ by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.0.15

```diff
$ tools/php-cs-fixer/vendor/bin/php-cs-fixer fix src --diff -vvv
PHP CS Fixer 3.7.0 #StandWithUkraine️ by Fabien Potencier and Dariusz Ruminski.
PHP runtime: 8.0.15
Loaded config default from "/Users/kenji/work/tmp/php-cs-fixer-test/.php-cs-fixer.php".
Using cache file ".php-cs-fixer.cache".
Paths from configuration file have been overridden by paths provided as command arguments.
F                                                                                                                      1 / 1 (100%)
Legend: ?-unknown, I-invalid file syntax (file ignored), S-skipped (cached or empty file), .-no changes, F-fixed, E-error
   1) src/test.php (blank_line_after_opening_tag, binary_operator_spaces)
      ---------- begin diff ----------
--- /Users/kenji/work/tmp/php-cs-fixer-test/src/test.php
+++ /Users/kenji/work/tmp/php-cs-fixer-test/src/test.php
@@ -1,7 +1,8 @@
 <?php
+
 $array = [];
 if (($c = count($array)) > 100) {
-    $_data = '100+';
+    $_data    = '100+';
 } elseif (($c = count($array)) > 0) {
-    $_data = '0+';
+    $_data    = '0+';
 }

      ----------- end diff -----------


Fixed all files in 0.046 seconds, 12.000 MB memory used
```

```php
<?php
$finder = PhpCsFixer\Finder::create()
    ->exclude('somedir')
    ->notPath('src/Symfony/Component/Translation/Tests/fixtures/resources.php')
    ->in(__DIR__)
;
$config = new PhpCsFixer\Config();
return $config->setRules([
        '@PSR12' => true,
        'binary_operator_spaces' => [
            'operators' => [
                '='  => 'align_single_space_minimal',
            ],
        ],
    ])
    ->setFinder($finder)
;
```

### Code snippet that reproduces the problem

```php
<?php
$array = [];
if (($c = count($array)) > 100) {
    $_data = '100+';
} elseif (($c = count($array)) > 0) {
    $_data = '0+';
}
```


Pull request:
BinaryOperatorSpacesFixer - Fix for alignment in `elseif`
Caused by #6112 
Fixes #6329 

Patch:
diff --git a/src/Fixer/Operator/BinaryOperatorSpacesFixer.php b/src/Fixer/Operator/BinaryOperatorSpacesFixer.php
index dd0e3ac52..940b9a944 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);
 


Test patch:
diff --git a/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php b/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
index 0a2000411..9cbc8867b 100644
--- a/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
+++ b/tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php
@@ -129,7 +129,7 @@ public function provideConfiguredCases(): array
             ],
             [
                 '<?php
-    $var = [];
+    $var = [1 => 2];
     foreach ([
                 1 => 2,
                 2 => 3,
@@ -137,7 +137,7 @@ public function provideConfiguredCases(): array
         $var[] = [$i => $bar];
     }',
                 '<?php
-    $var = [];
+    $var = [1=>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);
     }',
                 '<?php
     $ccc = 1;
@@ -845,7 +845,7 @@ function b(
 
     for ($i = 0; $i < 10; $i++) {
         $aa    = 2;
-        $a[$b] = array();
+        $a[$b] = array(12);
     }',
             ],
         ];
@@ -1344,7 +1344,7 @@ function b(
         $abc[$bcd = 1] = 1;
     }
 
-    while (false) {
+    while ($i = 1) {
         $aa    = 2;
         $a[$b] = array();
     }
@@ -1352,7 +1352,27 @@ function b(
     for ($i = 0; $i < 10; $i++) {
         $aa    = 2;
         $a[$b] = array();
-    }',
+    }
+
+    $z = 1;
+    switch($a = 0) {
+        case 1:
+            $b  = 1;
+            $cc = 3;
+        break;
+    }
+
+    foreach ($a as $b) {
+        $aa    = 2;
+        $a[$b] = array();
+    }
+
+    do {
+        $aa    = 23;
+        $a[$b] = array(66);
+    } while ($i = 1);
+    $a = 3;
+    ',
                 '<?php
     $ccc = 1;
     $bb = 1;
@@ -1391,7 +1411,7 @@ function b(
         $abc[$bcd = 1] = 1;
     }
 
-    while (false) {
+    while ($i = 1) {
         $aa = 2;
         $a[$b] = array();
     }
@@ -1399,7 +1419,27 @@ function b(
     for ($i = 0; $i < 10; $i++) {
         $aa = 2;
         $a[$b] = array();
-    }',
+    }
+
+    $z = 1;
+    switch($a = 0) {
+        case 1:
+            $b = 1;
+            $cc = 3;
+        break;
+    }
+
+    foreach ($a as $b) {
+        $aa    = 2;
+        $a[$b] = array();
+    }
+
+    do {
+        $aa = 23;
+        $a[$b] = array(66);
+    } while ($i = 1);
+    $a = 3;
+    ',
             ],
             [
                 '<?php
@@ -1425,6 +1465,24 @@ function ()
                 '<?php
 fn ($x = 1) => $x + 3;
 $f = 123;
+',
+            ],
+            [
+                '<?php
+if (($c = count($array)) > 100) {
+    $_data = \'100+\';
+} elseif (($c = count($array)) > 0) {
+    $_data = \'0+\';
+}
+',
+            ],
+            [
+                '<?php
+if (($c = count($array)) > 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"],
     ];',
             ],
+            [
+                '<?php
+$b = [1 => function() {
+    foreach([$a => 2] as $b) {
+        $bv = [
+            $b  => 2,
+            $cc => 3,
+        ];
+    }}, 2 => 3];
+',
+                '<?php
+$b = [1 => function() {
+    foreach([$a => 2] as $b) {
+        $bv = [
+            $b => 2,
+            $cc => 3,
+        ];
+    }}, 2 => 3];
+',
+            ],
         ];
     }