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

ClassAttributesSeparationFixer - fix for properties with type alternation

Kuba Werłos 4 лет назад
Родитель
Сommit
dcf5699e08

+ 1 - 1
src/Fixer/ClassNotation/ClassAttributesSeparationFixer.php

@@ -304,7 +304,7 @@ class Sample
      */
     private function fixSpaceAboveClassElement(Tokens $tokens, $classStartIndex, $elementIndex, $spacing)
     {
-        static $methodAttr = [T_PRIVATE, T_PROTECTED, T_PUBLIC, T_ABSTRACT, T_FINAL, T_STATIC, T_STRING, T_NS_SEPARATOR, T_VAR, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT];
+        static $methodAttr = [T_PRIVATE, T_PROTECTED, T_PUBLIC, T_ABSTRACT, T_FINAL, T_STATIC, T_STRING, T_NS_SEPARATOR, T_VAR, CT::T_NULLABLE_TYPE, CT::T_ARRAY_TYPEHINT, CT::T_TYPE_ALTERNATION];
 
         $nonWhiteAbove = null;
 

+ 3 - 3
src/Tokenizer/Transformer/TypeAlternationTransformer.php

@@ -74,9 +74,9 @@ final class TypeAlternationTransformer extends AbstractTransformer
         $prevToken = $tokens[$prevIndex];
 
         if ($prevToken->isGivenKind([
-            CT::T_TYPE_COLON, // `|` is part of a function return type union `foo(): A|B`
-            CT::T_TYPE_ALTERNATION, // `|` is part of a union (chain) `| X | Y`
-            T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE, // `|` is part of class property `var X|Y $a;`
+            CT::T_TYPE_COLON, // `:` is part of a function return type `foo(): A`
+            CT::T_TYPE_ALTERNATION, // `|` is part of a union (chain) `X | Y`
+            T_STATIC, T_VAR, T_PUBLIC, T_PROTECTED, T_PRIVATE, // `var $a;`, `private $a` or `public static $a`
         ])) {
             $this->replaceToken($tokens, $index);
 

+ 21 - 1
tests/Fixer/ClassNotation/ClassAttributesSeparationFixerTest.php

@@ -1416,7 +1416,7 @@ class User3
 }',
         ];
 
-        yield [
+        yield 'constructor property promotion' => [
             '<?php
             class Foo {
                 private array $foo;
@@ -1440,5 +1440,25 @@ class User3
                 ) {}
             }',
         ];
+
+        yield 'typed properties' => [
+            '<?php
+            class Foo {
+                private static int | float | null $a;
+
+                private static int | float | null $b;
+
+                private int | float | null $c;
+
+                private int | float | null $d;
+            }',
+            '<?php
+            class Foo {
+                private static int | float | null $a;
+                private static int | float | null $b;
+                private int | float | null $c;
+                private int | float | null $d;
+            }',
+        ];
     }
 }

+ 15 - 0
tests/Tokenizer/Transformer/TypeAlternationTransformerTest.php

@@ -211,5 +211,20 @@ class Number
                 73 => CT::T_TYPE_ALTERNATION,
             ],
         ];
+
+        yield 'typed static properties' => [
+            '<?php
+            class Foo {
+                private static int | null $bar;
+
+                private static int | float | string | null $baz;
+            }',
+            [
+                14 => CT::T_TYPE_ALTERNATION,
+                27 => CT::T_TYPE_ALTERNATION,
+                31 => CT::T_TYPE_ALTERNATION,
+                35 => CT::T_TYPE_ALTERNATION,
+            ],
+        ];
     }
 }