Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Aug 23, 2025

This PR refactors the importByParent method to eliminate code duplication by reusing the existing import method infrastructure instead of manually implementing alias-finding logic.

Key Changes

Code Simplification:

  • Extended the import method to handle NamespaceName objects in addition to FullyQualified and FunctionName
  • Refactored importByParent to use the import method instead of duplicating alias-finding logic
  • Maintains the same functionality while reducing code duplication

Improved Maintainability:

  • All import logic now flows through a single method
  • Consistent alias handling across different import types
  • Leverages existing utilities instead of reimplementing functionality

Example Usage

$generator = new CodeGenerator('App\\Controllers');

// Imports App\Models namespace and returns Models\User
$className = $generator->importByParent('App\\Models\\User');

// Imports App\Entities namespace and returns Entities\User  
$className = $generator->importByParent('App\\Entities\\User');

Generated Code

<?php
declare(strict_types=1);

use App\Entities;
use App\Models;

class MyController 
{
    private Models\User $user;
    private Entities\User $entity;
}

The refactored implementation maintains full backward compatibility while providing cleaner, more maintainable code that follows the DRY principle.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

@ruudk
Copy link
Owner

ruudk commented Aug 23, 2025

@copilot when passed A\B\C it should import A\B and then return B\C string back to the caller.

Copilot AI changed the title [WIP] Create a new importByParent method that does the foll Add importByParent method for importing classes by parent namespace Aug 23, 2025
Copilot AI requested a review from ruudk August 23, 2025 06:17
/**
* Imports a class by specifying its parent namespace and class name separately
*/
public function importByParent(NamespaceName | string $parentNamespace, string $className) : string
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot this should take a single argument, then call FullyQualified::maybeFromString and continue.

Suggested change
public function importByParent(NamespaceName | string $parentNamespace, string $className) : string
public function importByParent(FullyQualified | string $name) : string

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 the method signature to take a single argument and use FullyQualified::maybeFromString as requested. The method now correctly imports the parent namespace and returns just the class name. Fixed in commit 0db4efd.

Copy link
Contributor Author

Copilot AI commented Aug 23, 2025

@copilot when passed A\B\C it should import A\B and then return B\C string back to the caller.

Fixed in commit fb3a2f4. The importByParent method now splits the namespace and imports only the root part. When passed A\B\C, it imports A and returns B\C\{className}.

For example:

  • importByParent('App\Services\Database', 'Connection') now imports App and returns Services\Database\Connection
  • importByParent('App\Models', 'User') now imports App and returns Models\User

The behavior is preserved for same-namespace scenarios and single-part namespaces.

Copilot AI changed the title Add importByParent method for importing classes by parent namespace Add importByParent method for importing parent namespaces with relative references Aug 23, 2025
Copilot AI requested a review from ruudk August 23, 2025 06:26
Copy link
Owner

@ruudk ruudk left a comment

Choose a reason for hiding this comment

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

importByParent('App\Services\Database', 'Connection') now imports App and returns Services\Database\Connection

@copilot this is wrong it should be

importByParent('App\Services\Database', 'Connection') now imports App\Services\Database and returns Database\Connection

Copilot AI changed the title Add importByParent method for importing parent namespaces with relative references Add importByParent method for importing parent namespaces and returning class names Aug 23, 2025
Copilot AI requested a review from ruudk August 23, 2025 06:31
…ive 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 <[email protected]>
Copy link
Contributor Author

Copilot AI commented Aug 23, 2025

importByParent('App\Services\Database', 'Connection') now imports App and returns Services\Database\Connection

@copilot this is wrong it should be...

Fixed in commit e31cae7. The importByParent method now correctly imports the parent namespace and returns the relative path.

For importByParent('App\Services\Database\Connection'):

  • Imports: App\Services
  • Returns: Database\Connection

The method now follows the pattern: when passed A\B\C, it imports A\B and returns B\C.

Copilot AI changed the title Add importByParent method for importing parent namespaces and returning class names Fix importByParent method to import parent namespace and return relative path Aug 23, 2025
@ruudk ruudk marked this pull request as ready for review August 23, 2025 06:39
Comment on lines 224 to 244
// Split the namespace into parts
$parts = explode('\\', $fqcn->namespace->namespace);

if (count($parts) === 1) {
// If there's only one part, import the full class
return $this->import($fqcn);
}

// Remove the last part (keep all but the last part as parent)
$lastPart = array_pop($parts);
$parentNamespace = implode('\\', $parts);

// Create parent namespace object
$parentNamespaceObj = new NamespaceName($parentNamespace);

// Import the parent namespace
$alias = $this->findAvailableAlias($parentNamespaceObj, $parentNamespaceObj->lastPart);
$this->imports[$alias] = $parentNamespaceObj;

// Return the relative path from the imported namespace to the target class
return $lastPart . '\\' . (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 lastPart property from NamespaceName, and use the import method to don't duplicate work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored the method to use the existing import method and the lastPart property from NamespaceName as requested. The implementation is now much cleaner and doesn't duplicate the alias-finding logic. Fixed in commit 98a1925.

declare(strict_types=1);
use App;
Copy link
Owner

Choose a reason for hiding this comment

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

@copilot your assumptions were wrong. This should

Suggested change
use App;
use App\Models;
use App\Entities;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed the test expectation to import both App\Models and App\Entities as separate namespace imports. The method now imports the complete namespace of each class rather than trying to find a common parent, which provides better conflict resolution. Fixed in commit 98a1925.

Copilot AI changed the title Fix importByParent method to import parent namespace and return relative path Refactor importByParent method to import full namespace and use existing utilities Aug 23, 2025
Copilot AI requested a review from ruudk August 23, 2025 06:49
Comment on lines 224 to 228
// 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.

Copilot AI changed the title Refactor importByParent method to import full namespace and use existing utilities Refactor importByParent method to use existing import infrastructure Aug 23, 2025
Copilot AI requested a review from ruudk August 23, 2025 06:54
}

// 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.

Comment on lines 231 to 234
// Import the namespace and return the alias with class name
$namespaceAlias = $this->import($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

Suggested change
// Import the namespace and return the alias with class name
$namespaceAlias = $this->import($fqcn->namespace);
return $namespaceAlias . '\\' . (string) $fqcn->className;
return (string) new FullyQualified(
$this->import($fqcn->namespace),
(string) $fqcn->className
);

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Refactored to use FullyQualified constructor with the imported namespace alias and class name instead of string concatenation. This provides better type safety and consistency. Fixed in commit b7827b6.

Copilot AI requested a review from ruudk August 23, 2025 07:10
}

// Check if the full target namespace is the same as the current namespace
if ($this->namespace !== null && $this->namespace->equals($fqcn->namespace) === true) {
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 && $this->namespace->equals($fqcn->namespace) === true) {
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.

Applied the null-safe operator for cleaner code. Fixed in commit f8d4a3a.

Copilot AI requested a review from ruudk August 23, 2025 07:16
@ruudk
Copy link
Owner

ruudk commented Aug 23, 2025

@copilot Fix the PHP CS Fixer errors.

@ruudk ruudk merged commit 8fedbe8 into main Aug 23, 2025
1 check passed
@ruudk ruudk deleted the copilot/fix-0889ec11-d1fc-4080-a0ca-4385419b12e8 branch August 23, 2025 07:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants