Browse Source

bug: Align all the arrows inside the same array (#6590)

Vincent Langlet 2 years ago
parent
commit
ad0a87e269

+ 3 - 3
src/Fixer/Operator/BinaryOperatorSpacesFixer.php

@@ -514,7 +514,7 @@ $array = [
                 }
             }
 
-            $tokens->setCode($this->replacePlaceholders($tokensClone, $alignStrategy));
+            $tokens->setCode($this->replacePlaceholders($tokensClone, $alignStrategy, $tokenContent));
         }
     }
 
@@ -755,7 +755,7 @@ $array = [
     /**
      * Look for group of placeholders and provide vertical alignment.
      */
-    private function replacePlaceholders(Tokens $tokens, string $alignStrategy): string
+    private function replacePlaceholders(Tokens $tokens, string $alignStrategy, string $tokenContent): string
     {
         $tmpCode = $tokens->generateCode();
 
@@ -774,7 +774,7 @@ $array = [
             foreach ($lines as $index => $line) {
                 if (substr_count($line, $placeholder) > 0) {
                     $groups[$groupIndex][] = $index;
-                } else {
+                } elseif ('=>' !== $tokenContent) {
                     ++$groupIndex;
                     $groups[$groupIndex] = [];
                 }

+ 7 - 7
tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php

@@ -1711,7 +1711,7 @@ $start = (
             "foo"    => "bar",
             "foofoo" => 42,
         ]),
-        "baz" => "OK",
+        "baz"    => "OK",
     ]);',
                 '<?php
     return new JsonResponse([
@@ -1854,7 +1854,7 @@ $start = (
             1  => 2,
             22 => 3,
         ],
-        100 => [
+        100            => [
             1  => 2,
             22 => 3,
         ]
@@ -1875,8 +1875,8 @@ $start = (
             [
                 '<?php
     $a = array(
-        0  => 1,
-        10 => array(
+        0   => 1,
+        10  => array(
             1  => 2,
             22 => 3,
         ),
@@ -1936,7 +1936,7 @@ $start = (
         20    => 22,
         30    => 33,
         40
-            =>
+              =>
                 44,
     );',
                 '<?php
@@ -2005,8 +2005,8 @@ $start = (
                 '<?php
     $array = array(
         "bazab" => b(array(
-            1     => 2,
-            5     => [
+            1       => 2,
+            5       => [
                 6     => 7,
                 8     => 9,
             ],