Browse Source

TokensAnalyzer - fix for union types

Kuba Werłos 3 years ago
parent
commit
f3ac3970fa

+ 0 - 1
src/Tokenizer/TokensAnalyzer.php

@@ -570,7 +570,6 @@ final class TokensAnalyzer
                 T_SR => true,                   // >>
                 T_SR_EQUAL => true,             // >>=
                 T_XOR_EQUAL => true,            // ^=
-                CT::T_TYPE_ALTERNATION => true, // |
             ];
 
             // @TODO: drop condition when PHP 7.0+ is required

+ 18 - 2
tests/Fixer/Operator/BinaryOperatorSpacesFixerTest.php

@@ -2118,12 +2118,11 @@ $a = $ae?? $b;
                 ['operators' => ['=>' => BinaryOperatorSpacesFixer::ALIGN_SINGLE_SPACE_MINIMAL]],
             ],
             'multiple exceptions catch, default config' => [
-                '<?php try {} catch (A | B $e) {}',
                 '<?php try {} catch (A   |     B $e) {}',
             ],
             'multiple exceptions catch, no space config' => [
-                '<?php try {} catch (A|B $e) {}',
                 '<?php try {} catch (A   |     B $e) {}',
+                null,
                 ['operators' => ['|' => BinaryOperatorSpacesFixer::NO_SPACE]],
             ],
         ];
@@ -2167,4 +2166,21 @@ $a = $ae?? $b;
             ],
         ];
     }
+
+    /**
+     * @requires PHP 8.0
+     */
+    public function testUnionTypesAreNotChanged()
+    {
+        $this->doTest(
+            '<?php
+            class Foo
+            {
+                private bool|int | string $prop;
+                public function bar(TypeA | TypeB|TypeC $x): TypeA|TypeB | TypeC|TypeD
+                {
+                }
+            }'
+        );
+    }
 }

+ 4 - 4
tests/Fixtures/Integration/misc/PHP8_0.test

@@ -57,14 +57,14 @@ $object = new #[ExampleAttribute] class()
 // https://wiki.php.net/rfc/union_types_v2
 class Number
 {
-    private int | float | null $number;
+    private int|float|null $number;
 
     public function setNumber(int | float $number): void
     {
         $this->number = $number;
     }
 
-    public function getNumber(): int | float | null
+    public function getNumber(): int|float | null
     {
         return $this->number;
     }
@@ -234,12 +234,12 @@ class Number
 {
     private int|float|null $number;
 
-    public function setNumber(int|float $number): void
+    public function setNumber(int | float $number): void
     {
         $this->number = $number;
     }
 
-    public function getNumber(): int|float|null
+    public function getNumber(): int|float | null
     {
         return $this->number;
     }

+ 1 - 1
tests/Tokenizer/TokensAnalyzerTest.php

@@ -1510,7 +1510,7 @@ $b;',
         return [
             [
                 '<?php try {} catch (A | B $e) {}',
-                [11 => true],
+                [11 => false],
             ],
         ];
     }