Browse Source

GlobalNamespaceImportFixer - fix for attributes imported as constants

Kuba Werłos 3 years ago
parent
commit
21d6643378

+ 4 - 0
src/Tokenizer/Analyzer/ClassyAnalyzer.php

@@ -57,6 +57,10 @@ final class ClassyAnalyzer
             return true;
         }
 
+        if (AttributeAnalyzer::isAttribute($tokens, $index)) {
+            return true;
+        }
+
         // `Foo & $bar` could be:
         //   - function reference parameter: function baz(Foo & $bar) {}
         //   - bit operator: $x = Foo & $bar;

+ 2 - 4
src/Tokenizer/TokensAnalyzer.php

@@ -14,6 +14,7 @@ declare(strict_types=1);
 
 namespace PhpCsFixer\Tokenizer;
 
+use PhpCsFixer\Tokenizer\Analyzer\AttributeAnalyzer;
 use PhpCsFixer\Tokenizer\Analyzer\GotoLabelAnalyzer;
 
 /**
@@ -357,10 +358,7 @@ final class TokensAnalyzer
         }
 
         // check for attribute: `#[Foo]`
-        if (
-            \defined('T_ATTRIBUTE') // @TODO: drop condition when PHP 8.0+ is required
-            && $this->tokens[$prevIndex]->isGivenKind(T_ATTRIBUTE)
-        ) {
+        if (AttributeAnalyzer::isAttribute($this->tokens, $index)) {
             return false;
         }
 

+ 35 - 0
tests/Fixer/Import/GlobalNamespaceImportFixerTest.php

@@ -1011,4 +1011,39 @@ namespace {
 INPUT
         ];
     }
+
+    /**
+     * @requires PHP 8.0
+     */
+    public function testAttributes(): void
+    {
+        $this->fixer->configure([
+            'import_classes' => true,
+            'import_constants' => true,
+            'import_functions' => true,
+        ]);
+        $this->doTest(
+            '<?php
+namespace Foo;
+use AnAttribute1;
+use AnAttribute2;
+use AnAttribute3;
+class Bar
+{
+    #[AnAttribute1]
+    public function f1() {}
+    #[AnAttribute2, AnAttribute3]
+    public function f2() {}
+}',
+            '<?php
+namespace Foo;
+class Bar
+{
+    #[\AnAttribute1]
+    public function f1() {}
+    #[\AnAttribute2, \AnAttribute3]
+    public function f2() {}
+}'
+        );
+    }
 }

+ 5 - 0
tests/Tokenizer/Analyzer/ClassyAnalyzerTest.php

@@ -206,6 +206,11 @@ final class ClassyAnalyzerTest extends TestCase
             '<?php function foo(): int|A|false {}',
             [3 => false, 8 => false, 10 => true, 12 => false],
         ];
+
+        yield [
+            '<?php #[AnAttribute] class Foo {}',
+            [2 => true],
+        ];
     }
 
     private static function assertClassyInvocation(string $source, array $expected): void

+ 4 - 4
tests/Tokenizer/TokensAnalyzerTest.php

@@ -980,13 +980,13 @@ $a(1,2);',
         ];
 
         yield [
-            '<?php #[Foo] function foo() {}',
-            [2 => false, 7 => false],
+            '<?php #[Foo, Bar] function foo() {}',
+            [2 => false, 5 => false, 10 => false],
         ];
 
         yield [
-            '<?php #[Foo()] function foo() {}',
-            [2 => false, 9 => false],
+            '<?php #[Foo(), Bar()] function foo() {}',
+            [2 => false, 7 => false, 14 => false],
         ];
     }