Skip to content
Merged
24 changes: 24 additions & 0 deletions src/CodeGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,30 @@ public function import(FullyQualified | FunctionName | string $fqcnOrEnum) : str
return $alias;
}

/**
* Imports a class by importing its parent namespace and returning the relative path
*/
public function importByParent(FullyQualified | string $name) : string
{
$fqcn = FullyQualified::maybeFromString($name);

// If there's no namespace, just return the class name
if ($fqcn->namespace === null) {
return (string) $fqcn->className;
}

// Check if the full target namespace is the same as the current namespace
if ($this->namespace !== null && $fqcn->namespace->equals($this->namespace)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot

Suggested change
if ($this->namespace !== null && $fqcn->namespace->equals($this->namespace)) {
if ($this->namespace->equals($fqcn->namespace) === true) {

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated to use explicit boolean comparison === true as suggested. Fixed in commit b7827b6.

return (string) $fqcn->className;
}

// Import the namespace and return the alias with class name
$namespaceAlias = $this->findAvailableAlias($fqcn->namespace, $fqcn->namespace->lastPart);
$this->imports[$namespaceAlias] = $fqcn->namespace;

return $namespaceAlias . '\\' . (string) $fqcn->className;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@copilot use import method

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extended the import method to handle NamespaceName objects and refactored importByParent to use it instead of duplicating the alias-finding logic. This eliminates code duplication while maintaining the same functionality. Fixed in commit 247aba6.

}

/**
* Generates a PHP attribute string for the given fully qualified class name
*/
Expand Down
101 changes: 101 additions & 0 deletions tests/CodeGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,107 @@ public function testImportSameNamespace() : void
);
}

public function testImportByParentWithString() : void
{
$alias = $this->generator->importByParent('App\\Models\\User');

self::assertSame('Models\\User', $alias);

$this->assertDumpFile(
<<<'PHP'
<?php

declare(strict_types=1);

use App\Models;

PHP,
[],
);
}

public function testImportByParentWithNamespaceName() : void
{
$fqcn = new FullyQualified('App\\Services\\UserService');
$alias = $this->generator->importByParent($fqcn);

self::assertSame('Services\\UserService', $alias);

$this->assertDumpFile(
<<<'PHP'
<?php

declare(strict_types=1);

use App\Services;

PHP,
[],
);
}

public function testImportByParentWithConflict() : void
{
$alias1 = $this->generator->importByParent('App\\Models\\User');
$alias2 = $this->generator->importByParent('App\\Entities\\User');

self::assertSame('Models\\User', $alias1);
self::assertSame('Entities\\User', $alias2);

$this->assertDumpFile(
<<<'PHP'
<?php

declare(strict_types=1);

use App\Entities;
use App\Models;

PHP,
[],
);
}

public function testImportByParentSameNamespace() : void
{
$this->generator = new CodeGenerator('App\\Models');

$alias = $this->generator->importByParent('App\\Models\\User');

self::assertSame('User', $alias);

$this->assertDumpFile(
<<<'PHP'
<?php

declare(strict_types=1);

namespace App\Models;

PHP,
[],
);
}

public function testImportByParentWithThreePartNamespace() : void
{
$alias = $this->generator->importByParent('App\\Services\\Database\\Connection');

self::assertSame('Database\\Connection', $alias);

$this->assertDumpFile(
<<<'PHP'
<?php

declare(strict_types=1);

use App\Services\Database;

PHP,
[],
);
}

public function testDumpAttribute() : void
{
self::assertSame(
Expand Down
Loading