Browse Source

bug: BinaryOperatorSpacesFixer - fix for static in type (#6835)

Kuba Werłos 2 years ago
parent
commit
e68398d0d8

+ 1 - 1
src/Tokenizer/AbstractTypeTransformer.php

@@ -21,7 +21,7 @@ namespace PhpCsFixer\Tokenizer;
  */
 abstract class AbstractTypeTransformer extends AbstractTransformer
 {
-    private const TYPE_END_TOKENS = [')', [T_CALLABLE], [T_NS_SEPARATOR], [T_STRING], [CT::T_ARRAY_TYPEHINT]];
+    private const TYPE_END_TOKENS = [')', [T_CALLABLE], [T_NS_SEPARATOR], [T_STATIC], [T_STRING], [CT::T_ARRAY_TYPEHINT]];
 
     private const TYPE_TOKENS = [
         '|', '&', '(',

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

@@ -3110,6 +3110,7 @@ function test()
                 public function qux(
                     bool|int | string &$reference
                 ) {}
+                public function quux(): static| TypeA {}
             }'
         );
     }

+ 28 - 2
tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php

@@ -43,9 +43,9 @@ final class TypeAlternationTransformerTest extends AbstractTransformerTestCase
         );
     }
 
-    public static function provideProcessCases(): array
+    public static function provideProcessCases(): iterable
     {
-        return [
+        yield from [
             'no namespace' => [
                 '<?php try {} catch (ExceptionType1 | ExceptionType2 | ExceptionType3 $e) {}',
                 [
@@ -89,6 +89,32 @@ final class TypeAlternationTransformerTest extends AbstractTransformerTestCase
                 ',
             ],
         ];
+
+        yield 'self as type' => [
+            '<?php class Foo {
+                function f1(bool|self|int $x): void {}
+                function f2(): self|\stdClass {}
+            }',
+            [
+                12 => CT::T_TYPE_ALTERNATION,
+                14 => CT::T_TYPE_ALTERNATION,
+                34 => CT::T_TYPE_ALTERNATION,
+            ],
+        ];
+
+        yield 'static as type' => [
+            '<?php class Foo {
+                function f1(): static|TypeA {}
+                function f2(): TypeA|static|TypeB {}
+                function f3(): TypeA|static {}
+            }',
+            [
+                15 => CT::T_TYPE_ALTERNATION,
+                29 => CT::T_TYPE_ALTERNATION,
+                31 => CT::T_TYPE_ALTERNATION,
+                45 => CT::T_TYPE_ALTERNATION,
+            ],
+        ];
     }
 
     /**

+ 26 - 0
tests/Tokenizer/Transformer/TypeIntersectionTransformerTest.php

@@ -342,6 +342,32 @@ function f( #[Target(\'a\')] #[Target(\'b\')] #[Target(\'c\')] #[Target(\'d\')]
                 77 => CT::T_TYPE_INTERSECTION,
             ],
         ];
+
+        yield 'self as type' => [
+            '<?php class Foo {
+                function f1(bool&self&int $x): void {}
+                function f2(): self&\stdClass {}
+            }',
+            [
+                12 => CT::T_TYPE_INTERSECTION,
+                14 => CT::T_TYPE_INTERSECTION,
+                34 => CT::T_TYPE_INTERSECTION,
+            ],
+        ];
+
+        yield 'static as type' => [
+            '<?php class Foo {
+                function f1(): static&TypeA {}
+                function f2(): TypeA&static&TypeB {}
+                function f3(): TypeA&static {}
+            }',
+            [
+                15 => CT::T_TYPE_INTERSECTION,
+                29 => CT::T_TYPE_INTERSECTION,
+                31 => CT::T_TYPE_INTERSECTION,
+                45 => CT::T_TYPE_INTERSECTION,
+            ],
+        ];
     }
 
     /**