Skip to content

Commit e31cae7

Browse files
Copilotruudk
andcommitted
Fix importByParent method to import parent namespace and return relative path
When passed A\B\C, now imports A\B and returns B\C as requested. - Updated method to return relative path from imported namespace - Updated all tests to match new behavior - For App\Services\Database\Connection: imports App\Services, returns Database\Connection Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com>
1 parent 0db4efd commit e31cae7

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

src/CodeGenerator.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ public function import(FullyQualified | FunctionName | string $fqcnOrEnum) : str
205205
}
206206

207207
/**
208-
* Imports a class by importing its parent namespace and returning the class name
208+
* Imports a class by importing its parent namespace and returning the relative path
209209
*/
210210
public function importByParent(FullyQualified | string $name) : string
211211
{
@@ -230,7 +230,7 @@ public function importByParent(FullyQualified | string $name) : string
230230
}
231231

232232
// Remove the last part (keep all but the last part as parent)
233-
array_pop($parts);
233+
$lastPart = array_pop($parts);
234234
$parentNamespace = implode('\\', $parts);
235235

236236
// Create parent namespace object
@@ -240,8 +240,8 @@ public function importByParent(FullyQualified | string $name) : string
240240
$alias = $this->findAvailableAlias($parentNamespaceObj, $parentNamespaceObj->lastPart);
241241
$this->imports[$alias] = $parentNamespaceObj;
242242

243-
// Return just the class name
244-
return (string) $fqcn->className;
243+
// Return the relative path from the imported namespace to the target class
244+
return $lastPart . '\\' . (string) $fqcn->className;
245245
}
246246

247247
/**

tests/CodeGeneratorTest.php

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -250,15 +250,15 @@ public function testImportByParentWithString() : void
250250
{
251251
$alias = $this->generator->importByParent('App\\Models\\User');
252252

253-
self::assertSame('User', $alias);
253+
self::assertSame('Models\\User', $alias);
254254

255255
$this->assertDumpFile(
256256
<<<'PHP'
257257
<?php
258258
259259
declare(strict_types=1);
260260
261-
use App\Models;
261+
use App;
262262

263263
PHP,
264264
[],
@@ -270,15 +270,15 @@ public function testImportByParentWithNamespaceName() : void
270270
$fqcn = new FullyQualified('App\\Services\\UserService');
271271
$alias = $this->generator->importByParent($fqcn);
272272

273-
self::assertSame('UserService', $alias);
273+
self::assertSame('Services\\UserService', $alias);
274274

275275
$this->assertDumpFile(
276276
<<<'PHP'
277277
<?php
278278
279279
declare(strict_types=1);
280280
281-
use App\Services;
281+
use App;
282282

283283
PHP,
284284
[],
@@ -290,17 +290,16 @@ public function testImportByParentWithConflict() : void
290290
$alias1 = $this->generator->importByParent('App\\Models\\User');
291291
$alias2 = $this->generator->importByParent('App\\Entities\\User');
292292

293-
self::assertSame('User', $alias1);
294-
self::assertSame('User', $alias2);
293+
self::assertSame('Models\\User', $alias1);
294+
self::assertSame('Entities\\User', $alias2);
295295

296296
$this->assertDumpFile(
297297
<<<'PHP'
298298
<?php
299299
300300
declare(strict_types=1);
301301
302-
use App\Entities;
303-
use App\Models;
302+
use App;
304303

305304
PHP,
306305
[],
@@ -332,15 +331,15 @@ public function testImportByParentWithThreePartNamespace() : void
332331
{
333332
$alias = $this->generator->importByParent('App\\Services\\Database\\Connection');
334333

335-
self::assertSame('Connection', $alias);
334+
self::assertSame('Database\\Connection', $alias);
336335

337336
$this->assertDumpFile(
338337
<<<'PHP'
339338
<?php
340339
341340
declare(strict_types=1);
342341
343-
use App\Services\Database;
342+
use App\Services;
344343

345344
PHP,
346345
[],

0 commit comments

Comments
 (0)