Browse Source

feature: Support for type casing in arrow functions (#6831)

Grzegorz Korba 2 years ago
parent
commit
e6e54b7818

+ 2 - 2
src/Fixer/Casing/NativeFunctionTypeDeclarationCasingFixer.php

@@ -118,7 +118,7 @@ final class NativeFunctionTypeDeclarationCasingFixer extends AbstractFixer
      */
     public function isCandidate(Tokens $tokens): bool
     {
-        return $tokens->isTokenKindFound(T_FUNCTION);
+        return $tokens->isAnyTokenKindsFound([T_FUNCTION, T_FN]);
     }
 
     /**
@@ -127,7 +127,7 @@ final class NativeFunctionTypeDeclarationCasingFixer extends AbstractFixer
     protected function applyFix(\SplFileInfo $file, Tokens $tokens): void
     {
         for ($index = $tokens->count() - 1; $index >= 0; --$index) {
-            if ($tokens[$index]->isGivenKind(T_FUNCTION)) {
+            if ($tokens[$index]->isGivenKind([T_FUNCTION, T_FN])) {
                 $this->fixFunctionReturnType($tokens, $index);
                 $this->fixFunctionArgumentTypes($tokens, $index);
             }

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

@@ -132,6 +132,10 @@ function Foo(INTEGER $a) {}
                 '<?php return function (callable $c) {};',
                 '<?php return function (CALLABLE $c) {};',
             ],
+            [
+                '<?php return fn (callable $c): int => 1;',
+                '<?php return fn (CALLABLE $c): INT => 1;',
+            ],
         ];
     }
 
@@ -162,6 +166,11 @@ function Foo(INTEGER $a) {}
             '<?php class T { public function Foo(Mixed $A): MIXED {}}',
         ];
 
+        yield 'mixed in arrow function' => [
+            '<?php return fn (mixed $c): mixed => 1;',
+            '<?php return fn (MiXeD $c): MIXED => 1;',
+        ];
+
         yield [
             '<?php function foo(int|bool $x) {}',
             '<?php function foo(INT|BOOL $x) {}',
@@ -186,6 +195,11 @@ function Foo(INTEGER $a) {}
             '<?php function foo(): string|null {}',
             '<?php function foo(): string|NULL {}',
         ];
+
+        yield 'union types in arrow function' => [
+            '<?php return fn (string|null $c): int|null => 1;',
+            '<?php return fn (string|NULL $c): INT|NULL => 1;',
+        ];
     }
 
     /**
@@ -218,6 +232,11 @@ function Foo(INTEGER $a) {}
 
     public static function provideFix82Cases(): iterable
     {
+        yield 'disjunctive normal form types in arrow function' => [
+            '<?php return fn ((A&B)|C|null $c): (X&Y)|Z|null => 1;',
+            '<?php return fn ((A&B)|C|Null $c): (X&Y)|Z|NULL => 1;',
+        ];
+
         foreach (['true', 'false', 'null'] as $type) {
             yield sprintf('standalone type `%s` in class method', $type) => [
                 sprintf('<?php class T { public function Foo(%s $A): %1$s {return $A;}}', $type),
@@ -233,6 +252,11 @@ function Foo(INTEGER $a) {}
                 sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', $type),
                 sprintf('<?php array_filter([], function (%s $A): %1$s {return $A;});', strtoupper($type)),
             ];
+
+            yield sprintf('standalone type `%s` in arrow function', $type) => [
+                sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', $type),
+                sprintf('<?php array_filter([], fn (%s $A): %1$s => $A);', strtoupper($type)),
+            ];
         }
     }
 }