Browse Source

NativeFunctionTypeDeclarationCasingFixer - fix for union types

Kuba Werłos 3 years ago
parent
commit
b0fd42cdf5

+ 10 - 14
src/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixer.php

@@ -169,22 +169,18 @@ final class NativeFunctionTypeDeclarationCasingFixer extends AbstractFixer
             return;
         }
 
-        $argumentStartIndex = $type->getStartIndex();
-        $argumentExpectedEndIndex = $type->isNullable()
-            ? $tokens->getNextMeaningfulToken($argumentStartIndex)
-            : $argumentStartIndex
-        ;
-
-        if ($argumentExpectedEndIndex !== $type->getEndIndex()) {
-            return; // the type to fix is always unqualified and so is always composed of one token and possible a nullable '?' one
-        }
+        for ($index = $type->getStartIndex(); $index <= $type->getEndIndex(); ++$index) {
+            if ($tokens[$tokens->getNextMeaningfulToken($index)]->isGivenKind(T_NS_SEPARATOR)) {
+                continue;
+            }
 
-        $lowerCasedName = strtolower($type->getName());
+            $lowerCasedName = strtolower($tokens[$index]->getContent());
 
-        if (!isset($this->hints[$lowerCasedName])) {
-            return; // check of type is of interest based on name (slower check than previous index based)
-        }
+            if (!isset($this->hints[$lowerCasedName])) {
+                continue;
+            }
 
-        $tokens[$argumentExpectedEndIndex] = new Token([$tokens[$argumentExpectedEndIndex]->getId(), $lowerCasedName]);
+            $tokens[$index] = new Token([$tokens[$index]->getId(), $lowerCasedName]);
+        }
     }
 }

+ 21 - 0
tests/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixerTest.php

@@ -94,6 +94,12 @@ class Bar { function Foo(ARRAY $a, CALLABLE $b, Self $c) {} }
 function Foo(INTEGER $a) {}
                 ',
             ],
+            [
+                '<?php function Foo(
+                    String\A $x,
+                    B\String\C $y
+                ) {}',
+            ],
         ];
     }
 
@@ -230,5 +236,20 @@ function Foo(INTEGER $a) {}
             '<?php class T { public function Foo(mixed $A): mixed {}}',
             '<?php class T { public function Foo(Mixed $A): MIXED {}}',
         ];
+
+        yield [
+            '<?php function foo(int|bool $x) {}',
+            '<?php function foo(INT|BOOL $x) {}',
+        ];
+
+        yield [
+            '<?php function foo(int | bool $x) {}',
+            '<?php function foo(INT | BOOL $x) {}',
+        ];
+
+        yield [
+            '<?php function foo(): int|bool {}',
+            '<?php function foo(): INT|BOOL {}',
+        ];
     }
 }