Browse Source

fix: GlobalNamespaceImportFixer - key in PHPDoc's array shape matching class name (#7522)

Kuba Werłos 1 year ago
parent
commit
10fb65e3e8

+ 1 - 1
src/Fixer/Import/GlobalNamespaceImportFixer.php

@@ -706,7 +706,7 @@ if (count($x)) {
             foreach ($types as $i => $fullType) {
                 $newFullType = $fullType;
 
-                Preg::matchAll('/[\\\\\w]+/', $fullType, $matches, PREG_OFFSET_CAPTURE);
+                Preg::matchAll('/[\\\\\w]+(?![\\\\\w:])/', $fullType, $matches, PREG_OFFSET_CAPTURE);
 
                 foreach (array_reverse($matches[0]) as [$type, $offset]) {
                     $newType = $callback($type);

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

@@ -1094,6 +1094,43 @@ final class GlobalNamespaceImportFixerTest extends AbstractFixerTestCase
                 }
                 INPUT
         ];
+
+        yield 'key in PHPDoc\'s array shape matching class name' => [
+            '<?php
+                namespace Foo;
+                use Exception;
+                class Bar {
+                    /**
+                     * @return array{code: int, exception: \Exception}
+                     */
+                    public function f1(): array {}
+                    /**
+                     * @return array{exception: \Exception}
+                     */
+                    public function f2(): array {}
+                    /**
+                     * @return array{exceptions: array<\Exception>}
+                     */
+                    public function f3(): array {}
+                }',
+            '<?php
+                namespace Foo;
+                use Exception;
+                class Bar {
+                    /**
+                     * @return array{code: int, exception: Exception}
+                     */
+                    public function f1(): array {}
+                    /**
+                     * @return array{exception: Exception}
+                     */
+                    public function f2(): array {}
+                    /**
+                     * @return array{exceptions: array<Exception>}
+                     */
+                    public function f3(): array {}
+                }',
+        ];
     }
 
     /**