Skip to content

Commit 8fedbe8

Browse files
Copilotruudk
andauthored
Introduce importByParent method (#3)
* Initial plan * Implement importByParent method with comprehensive tests Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> * Fix importByParent method to import parent namespace and return relative path Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> * Update importByParent method to take single argument and use FullyQualified::maybeFromString Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> * 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> * Refactor importByParent to use import method and lastPart property Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> * Refactor importByParent to use import method instead of duplicating alias logic Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> * Apply code review suggestions: use explicit boolean comparison and FullyQualified constructor Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> * Use null-safe operator in importByParent method Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> * Fix code style --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: ruudk <104180+ruudk@users.noreply.github.com> Co-authored-by: Ruud Kamphuis <ruudk@users.noreply.github.com>
1 parent 64c182f commit 8fedbe8

File tree

2 files changed

+134
-2
lines changed

2 files changed

+134
-2
lines changed

src/CodeGenerator.php

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ public function importEnum(UnitEnum $enum) : string
186186
}
187187

188188
/**
189-
* Imports a class or function and returns the alias to use in the generated code
189+
* Imports a class, namespace, or function and returns the alias to use in the generated code
190190
*/
191-
public function import(FullyQualified | FunctionName | string $fqcnOrEnum) : string
191+
public function import(FullyQualified | FunctionName | NamespaceName | string $fqcnOrEnum) : string
192192
{
193193
if ($fqcnOrEnum instanceof FunctionName) {
194194
$alias = $this->findAvailableAlias($fqcnOrEnum, $fqcnOrEnum->shortName);
@@ -197,13 +197,44 @@ public function import(FullyQualified | FunctionName | string $fqcnOrEnum) : str
197197
return $alias;
198198
}
199199

200+
if ($fqcnOrEnum instanceof NamespaceName) {
201+
$alias = $this->findAvailableAlias($fqcnOrEnum, $fqcnOrEnum->lastPart);
202+
$this->imports[$alias] = $fqcnOrEnum;
203+
204+
return $alias;
205+
}
206+
200207
$fqcn = FullyQualified::maybeFromString($fqcnOrEnum);
201208
$alias = $this->findAvailableAlias($fqcn, $fqcn->className->name);
202209
$this->imports[$alias] = $fqcn;
203210

204211
return $alias;
205212
}
206213

214+
/**
215+
* Imports a class by importing its parent namespace and returning the relative path
216+
*/
217+
public function importByParent(FullyQualified | string $name) : string
218+
{
219+
$fqcn = FullyQualified::maybeFromString($name);
220+
221+
// If there's no namespace, just return the class name
222+
if ($fqcn->namespace === null) {
223+
return (string) $fqcn->className;
224+
}
225+
226+
// Check if the full target namespace is the same as the current namespace
227+
if ($this->namespace?->equals($fqcn->namespace) === true) {
228+
return (string) $fqcn->className;
229+
}
230+
231+
// Import the namespace and return the alias with class name
232+
return (string) new FullyQualified(
233+
$this->import($fqcn->namespace),
234+
(string) $fqcn->className,
235+
);
236+
}
237+
207238
/**
208239
* Generates a PHP attribute string for the given fully qualified class name
209240
*/

tests/CodeGeneratorTest.php

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,107 @@ public function testImportSameNamespace() : void
246246
);
247247
}
248248

249+
public function testImportByParentWithString() : void
250+
{
251+
$alias = $this->generator->importByParent('App\\Models\\User');
252+
253+
self::assertSame('Models\\User', $alias);
254+
255+
$this->assertDumpFile(
256+
<<<'PHP'
257+
<?php
258+
259+
declare(strict_types=1);
260+
261+
use App\Models;
262+
263+
PHP,
264+
[],
265+
);
266+
}
267+
268+
public function testImportByParentWithNamespaceName() : void
269+
{
270+
$fqcn = new FullyQualified('App\\Services\\UserService');
271+
$alias = $this->generator->importByParent($fqcn);
272+
273+
self::assertSame('Services\\UserService', $alias);
274+
275+
$this->assertDumpFile(
276+
<<<'PHP'
277+
<?php
278+
279+
declare(strict_types=1);
280+
281+
use App\Services;
282+
283+
PHP,
284+
[],
285+
);
286+
}
287+
288+
public function testImportByParentWithConflict() : void
289+
{
290+
$alias1 = $this->generator->importByParent('App\\Models\\User');
291+
$alias2 = $this->generator->importByParent('App\\Entities\\User');
292+
293+
self::assertSame('Models\\User', $alias1);
294+
self::assertSame('Entities\\User', $alias2);
295+
296+
$this->assertDumpFile(
297+
<<<'PHP'
298+
<?php
299+
300+
declare(strict_types=1);
301+
302+
use App\Entities;
303+
use App\Models;
304+
305+
PHP,
306+
[],
307+
);
308+
}
309+
310+
public function testImportByParentSameNamespace() : void
311+
{
312+
$this->generator = new CodeGenerator('App\\Models');
313+
314+
$alias = $this->generator->importByParent('App\\Models\\User');
315+
316+
self::assertSame('User', $alias);
317+
318+
$this->assertDumpFile(
319+
<<<'PHP'
320+
<?php
321+
322+
declare(strict_types=1);
323+
324+
namespace App\Models;
325+
326+
PHP,
327+
[],
328+
);
329+
}
330+
331+
public function testImportByParentWithThreePartNamespace() : void
332+
{
333+
$alias = $this->generator->importByParent('App\\Services\\Database\\Connection');
334+
335+
self::assertSame('Database\\Connection', $alias);
336+
337+
$this->assertDumpFile(
338+
<<<'PHP'
339+
<?php
340+
341+
declare(strict_types=1);
342+
343+
use App\Services\Database;
344+
345+
PHP,
346+
[],
347+
);
348+
}
349+
249350
public function testDumpAttribute() : void
250351
{
251352
self::assertSame(

0 commit comments

Comments
 (0)