Skip to content

Commit 45a43b1

Browse files
committed
Fix current namespace.
1 parent 0903068 commit 45a43b1

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

src/Importing.php

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,19 @@ public function simplyFullyQualifiedName(string $fullyQualifiedName): string
184184
return $this->replace[$fullyQualifiedName] ?? $fullyQualifiedName;
185185
}
186186

187+
//--------------------------------------------------------------------------------------------------------------------
188+
/**
189+
* Returns true if and only if the a name space is the global or current namespace.
190+
*
191+
* @param string $namespace The namespace.
192+
*
193+
* @return bool
194+
*/
195+
private function isGlobalOrCurrentNamespace(string $namespace): bool
196+
{
197+
return ($namespace===$this->namespace || $namespace==='');
198+
}
199+
187200
//--------------------------------------------------------------------------------------------------------------------
188201
/**
189202
* Returns raw data about classes to import.
@@ -204,12 +217,13 @@ private function prepare0(): array
204217
'namespace' => $namespace,
205218
'name' => $name,
206219
'alias' => null,
207-
'import' => ($namespace!==$this->namespace && $namespace!=='')];
220+
'import' => !$this->isGlobalOrCurrentNamespace($namespace)];
208221
}
209222

210223
foreach ($rawImports as &$rawImport)
211224
{
212-
if (self::collision1($rawImports, $rawImport['fully_qualified_name']))
225+
if (!$this->isGlobalOrCurrentNamespace($rawImport['namespace']) &&
226+
self::collision1($rawImports, $rawImport['fully_qualified_name']))
213227
{
214228
$i = 1;
215229
do
@@ -275,7 +289,7 @@ private function prepare2(array $rawImports): void
275289
$this->replace = [];
276290
foreach ($rawImports as $rawImport)
277291
{
278-
if ($rawImport['import'])
292+
if ($rawImport['namespace']!=='')
279293
{
280294
$this->replace[$rawImport['fully_qualified_name']] = $rawImport['alias'] ?? $rawImport['name'];
281295
}

test/ImportingTest.php

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,7 @@ public function testCollision3(): void
8989
$importing->addClass('\\Foo\\Bar\\ClassTwoAlias1');
9090
$importing->addClass('\\Foo\\Bar\\ClassTwoAlias2');
9191
$importing->addClass('\\Foo\\Bar\\ClassThree');
92+
$importing->addClass('\\Throwable');
9293

9394
$importing->prepare();
9495

@@ -101,12 +102,13 @@ public function testCollision3(): void
101102

102103
self::assertSame($expected, $importing->imports());
103104

104-
$expected = ['\\Foo\\Bar\\ClassTwoAlias1' => 'ClassTwoAlias1',
105-
'\\Foo\\Bar\\ClassTwoAlias2' => 'ClassTwoAlias2',
106-
'\\Foo\\Bar\\ClassThree' => 'ClassThree',
107-
'\\Bar\\Foo\\ClassTwo' => 'ClassTwoAlias3',
108-
'\\Foo\\Bar\\ClassOne' => 'ClassOne',
109-
'\\Foo\\Bar\\ClassTwo' => 'ClassTwoAlias4'];
105+
$expected = ['\\'.__NAMESPACE__.'\\ClassTwo' => 'ClassTwo',
106+
'\\Foo\\Bar\\ClassTwoAlias1' => 'ClassTwoAlias1',
107+
'\\Foo\\Bar\\ClassTwoAlias2' => 'ClassTwoAlias2',
108+
'\\Foo\\Bar\\ClassThree' => 'ClassThree',
109+
'\\Bar\\Foo\\ClassTwo' => 'ClassTwoAlias3',
110+
'\\Foo\\Bar\\ClassOne' => 'ClassOne',
111+
'\\Foo\\Bar\\ClassTwo' => 'ClassTwoAlias4'];
110112

111113
self::assertSame($expected, $importing->replacePairs());
112114
}
@@ -125,6 +127,34 @@ public function testNoImports(): void
125127
self::assertSame([], $importing->replacePairs());
126128
}
127129

130+
//--------------------------------------------------------------------------------------------------------------------
131+
/**
132+
* Simple test with 3 distinct classes and a global name.
133+
*/
134+
public function testSameNamespace(): void
135+
{
136+
$importing = new Importing(__NAMESPACE__);
137+
$importing->addClass(__NAMESPACE__.'\\ClassOne');
138+
$importing->addClass(__NAMESPACE__.'\\ClassTwo');
139+
$importing->addClass(__NAMESPACE__.'\\ClassThree');
140+
$importing->addClass('\\Throwable');
141+
142+
$importing->prepare();
143+
144+
self::assertSame([], $importing->imports());
145+
146+
$expected = ['\\'.__NAMESPACE__.'\\ClassThree' => 'ClassThree',
147+
'\\'.__NAMESPACE__.'\\ClassOne' => 'ClassOne',
148+
'\\'.__NAMESPACE__.'\\ClassTwo' => 'ClassTwo'];
149+
150+
self::assertSame($expected, $importing->replacePairs());
151+
152+
self::assertSame('ClassOne', $importing->simplyFullyQualifiedName(__NAMESPACE__.'\\ClassOne'));
153+
self::assertSame('ClassTwo', $importing->simplyFullyQualifiedName(__NAMESPACE__.'\\ClassTwo'));
154+
self::assertSame('ClassThree', $importing->simplyFullyQualifiedName(__NAMESPACE__.'\\ClassThree'));
155+
self::assertSame('\\Throwable', $importing->simplyFullyQualifiedName('\\Throwable'));
156+
}
157+
128158
//--------------------------------------------------------------------------------------------------------------------
129159
/**
130160
* Simple test with 3 distinct classes.
@@ -158,7 +188,7 @@ public function testSimpleCase(): void
158188
/**
159189
* Simple test with 3 distinct classes and a global name.
160190
*/
161-
public function testSimpleCaseWIthGlobalName(): void
191+
public function testSimpleCaseWithGlobalName(): void
162192
{
163193
$importing = new Importing(__NAMESPACE__);
164194
$importing->addClass('\\Foo\\Bar\\ClassOne');

0 commit comments

Comments
 (0)