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

bug: TokensAnalyzer - fix intersection types considered as binary operator (#6414)

TokensAnalyzer - fix intersection types considered as binary operator
Jérémy DECOOL 2 лет назад
Родитель
Сommit
866715801a

+ 1 - 1
src/Tokenizer/TokensAnalyzer.php

@@ -565,7 +565,7 @@ final class TokensAnalyzer
         $tokens = $this->tokens;
         $token = $tokens[$index];
 
-        if ($token->isGivenKind(T_ENCAPSED_AND_WHITESPACE)) {
+        if ($token->isGivenKind([T_ENCAPSED_AND_WHITESPACE, CT::T_TYPE_INTERSECTION])) {
             return false;
         }
 

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

@@ -2203,4 +2203,25 @@ $b = [1 => function() {
             }'
         );
     }
+
+    /**
+     * @requires PHP 8.1
+     */
+    public function testIntersectionTypesAreNotChanged(): void
+    {
+        $this->doTest(
+            '<?php
+            class Foo
+            {
+                private TypeA&TypeB & TypeC $prop;
+                public function bar(TypeA & TypeB&TypeC $x): TypeA&TypeB & TypeC&TypeD
+                {
+                }
+                public function baz(
+                    Countable&Traversable $a,
+                    Traversable&Countable $b,
+                ) {}
+            }'
+        );
+    }
 }

+ 27 - 1
tests/Tokenizer/TokensAnalyzerTest.php

@@ -1768,7 +1768,7 @@ $b;',
         }
     }
 
-    public static function provideIsBinaryOperator80Cases(): iterable
+    public function provideIsBinaryOperator80Cases(): iterable
     {
         yield [
             [6 => false],
@@ -1791,6 +1791,32 @@ $b;',
         ];
     }
 
+    /**
+     * @dataProvider provideIsBinaryOperator81Cases
+     * @requires PHP 8.1
+     */
+    public function testIsBinaryOperator81(array $expected, string $source): void
+    {
+        $tokensAnalyzer = new TokensAnalyzer(Tokens::fromCode($source));
+
+        foreach ($expected as $index => $isBinary) {
+            static::assertSame($isBinary, $tokensAnalyzer->isBinaryOperator($index));
+
+            if ($isBinary) {
+                static::assertFalse($tokensAnalyzer->isUnarySuccessorOperator($index));
+                static::assertFalse($tokensAnalyzer->isUnaryPredecessorOperator($index));
+            }
+        }
+    }
+
+    public function provideIsBinaryOperator81Cases(): iterable
+    {
+        yield 'type intersection' => [
+            [6 => false],
+            '<?php function foo(array&string $x) {}',
+        ];
+    }
+
     /**
      * @dataProvider provideArrayExceptionsCases
      */