Просмотр исходного кода

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

Vincent Langlet 3 лет назад
Родитель
Сommit
f7750fdeab

+ 10 - 2
src/Fixer/Operator/BinaryOperatorSpacesFixer.php

@@ -531,6 +531,11 @@ $array = [
 
     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 @@ $array = [
                 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;

+ 26 - 0
tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php

@@ -1401,6 +1401,32 @@ $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;
+',
+            ],
         ];
     }