diff --git a/src/CodeGenerator.php b/src/CodeGenerator.php index b844277..465238d 100644 --- a/src/CodeGenerator.php +++ b/src/CodeGenerator.php @@ -214,8 +214,14 @@ public function import(Importable | string $name) : string // Check if the class is in the same namespace as the current namespace if ($this->namespace !== null && $fqcn->namespace !== null && $this->namespace->equals($fqcn->namespace)) { - // No import needed, just return the class name - return (string) $fqcn->className; + // No import needed, but reserve the short name so later imports from + // other namespaces are aliased instead of colliding with the local + // declaration (PHP would reject `use X\Y\Foo;` in a file that also + // declares `class Foo`). + $alias = (string) $fqcn->className; + $this->imports[$alias] ??= $fqcn; + + return $alias; } $alias = $this->findAvailableAlias($fqcn, $fqcn->className->name); diff --git a/tests/CodeGeneratorTest.php b/tests/CodeGeneratorTest.php index 03038de..4c94219 100644 --- a/tests/CodeGeneratorTest.php +++ b/tests/CodeGeneratorTest.php @@ -170,6 +170,31 @@ public function testImportClassWithConflict() : void ); } + public function testImportClassConflictsWithSameNamespaceDeclaration() : void + { + $this->generator = new CodeGenerator('App\\Models'); + + $local = $this->generator->import('App\\Models\\User'); + $external = $this->generator->import('App\\Domain\\User'); + + self::assertSame('User', $local); + self::assertSame('User2', $external); + + $this->assertDumpFile( + <<<'PHP' + generator->import(new FunctionName('array_map'));