From 09030682fc6550380f301a6042c5565a610bf223 Mon Sep 17 00:00:00 2001 From: Paul Water Date: Fri, 24 Apr 2020 19:10:46 +0200 Subject: [PATCH] Fix global namespace and current namespace. --- src/Importing.php | 11 ++++++++--- test/ImportingTest.php | 43 +++++++++++++++++++++++++++++++++++------- 2 files changed, 44 insertions(+), 10 deletions(-) diff --git a/src/Importing.php b/src/Importing.php index 4b4bb81..bbb208e 100644 --- a/src/Importing.php +++ b/src/Importing.php @@ -179,7 +179,9 @@ public function replacePairs(): array */ public function simplyFullyQualifiedName(string $fullyQualifiedName): string { - return $this->replace[self::fullyQualify($fullyQualifiedName)]; + $fullyQualifiedName = self::fullyQualify($fullyQualifiedName); + + return $this->replace[$fullyQualifiedName] ?? $fullyQualifiedName; } //-------------------------------------------------------------------------------------------------------------------- @@ -202,7 +204,7 @@ private function prepare0(): array 'namespace' => $namespace, 'name' => $name, 'alias' => null, - 'import' => ($namespace!==$this->namespace)]; + 'import' => ($namespace!==$this->namespace && $namespace!=='')]; } foreach ($rawImports as &$rawImport) @@ -273,7 +275,10 @@ private function prepare2(array $rawImports): void $this->replace = []; foreach ($rawImports as $rawImport) { - $this->replace[$rawImport['fully_qualified_name']] = $rawImport['alias'] ?? $rawImport['name']; + if ($rawImport['import']) + { + $this->replace[$rawImport['fully_qualified_name']] = $rawImport['alias'] ?? $rawImport['name']; + } } } diff --git a/test/ImportingTest.php b/test/ImportingTest.php index 888460b..938143a 100644 --- a/test/ImportingTest.php +++ b/test/ImportingTest.php @@ -101,13 +101,12 @@ public function testCollision3(): void self::assertSame($expected, $importing->imports()); - $expected = ['\\'.__NAMESPACE__.'\\ClassTwo' => 'ClassTwoAlias5', - '\\Foo\\Bar\\ClassTwoAlias1' => 'ClassTwoAlias1', - '\\Foo\\Bar\\ClassTwoAlias2' => 'ClassTwoAlias2', - '\\Foo\\Bar\\ClassThree' => 'ClassThree', - '\\Bar\\Foo\\ClassTwo' => 'ClassTwoAlias3', - '\\Foo\\Bar\\ClassOne' => 'ClassOne', - '\\Foo\\Bar\\ClassTwo' => 'ClassTwoAlias4']; + $expected = ['\\Foo\\Bar\\ClassTwoAlias1' => 'ClassTwoAlias1', + '\\Foo\\Bar\\ClassTwoAlias2' => 'ClassTwoAlias2', + '\\Foo\\Bar\\ClassThree' => 'ClassThree', + '\\Bar\\Foo\\ClassTwo' => 'ClassTwoAlias3', + '\\Foo\\Bar\\ClassOne' => 'ClassOne', + '\\Foo\\Bar\\ClassTwo' => 'ClassTwoAlias4']; self::assertSame($expected, $importing->replacePairs()); } @@ -155,6 +154,36 @@ public function testSimpleCase(): void self::assertSame('ClassThree', $importing->simplyFullyQualifiedName('\\Foo\\Bar\\ClassThree')); } + //-------------------------------------------------------------------------------------------------------------------- + /** + * Simple test with 3 distinct classes and a global name. + */ + public function testSimpleCaseWIthGlobalName(): void + { + $importing = new Importing(__NAMESPACE__); + $importing->addClass('\\Foo\\Bar\\ClassOne'); + $importing->addClass('Foo\\Bar\\ClassTwo'); + $importing->addClass('\\Foo\\Bar\\ClassThree'); + $importing->addClass('\\Throwable'); + + $importing->prepare(); + + $expected = ['use Foo\\Bar\\ClassOne;', + 'use Foo\\Bar\\ClassThree;', + 'use Foo\\Bar\\ClassTwo;']; + + self::assertSame($expected, $importing->imports()); + + $expected = ['\\Foo\\Bar\\ClassThree' => 'ClassThree', + '\\Foo\\Bar\\ClassOne' => 'ClassOne', + '\\Foo\\Bar\\ClassTwo' => 'ClassTwo']; + + self::assertSame($expected, $importing->replacePairs()); + + self::assertSame('ClassThree', $importing->simplyFullyQualifiedName('Foo\\Bar\\ClassThree')); + self::assertSame('ClassThree', $importing->simplyFullyQualifiedName('\\Foo\\Bar\\ClassThree')); + } + //-------------------------------------------------------------------------------------------------------------------- }