Skip to content

Commit

Permalink
Fix global namespace and current namespace.
Browse files Browse the repository at this point in the history
  • Loading branch information
prwater committed Apr 24, 2020
1 parent 8649c7f commit 0903068
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 10 deletions.
11 changes: 8 additions & 3 deletions src/Importing.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

//--------------------------------------------------------------------------------------------------------------------
Expand All @@ -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)
Expand Down Expand Up @@ -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'];
}
}
}

Expand Down
43 changes: 36 additions & 7 deletions test/ImportingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
Expand Down Expand Up @@ -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'));
}

//--------------------------------------------------------------------------------------------------------------------
}

Expand Down

0 comments on commit 0903068

Please sign in to comment.