Add Importable interface to enable direct passing of importable objects #6
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This PR introduces an
Importableinterface that allows passing importable objects directly to methods likeFullyQualified::maybeFromString()without explicit string casting.Problem
Previously, when working with importable objects, you had to cast them to strings before passing them to methods:
This was verbose and reduced type safety, especially when dealing with multiple importable types.
Solution
This PR adds an
Importableinterface that:Stringableto maintain string conversion capabilityequals(object $other): boolandcompare(object $other): intClassName,FullyQualified,FunctionName,NamespaceName, andAliasChanges
New Interface
Importable- Common interface for all importable typesUpdated Classes
All five importable classes now implement
Importable:#[Override]attributes for interface method implementationsmaybeFromString()methods to acceptstring|Importableinstead of juststringEnhanced CodeGenerator
Importablewhere appropriate:import(Importable | string $fqcnOrEnum)importByParent(Importable | string $name)findAvailableAlias(Importable $type, ...)Benefits
✅ Cleaner API: No more explicit string casting required
✅ Better type safety: Interface-based typing instead of union types
✅ Backward compatible: All existing code continues to work
✅ Consistent: All importable types follow the same pattern
Usage Example
Testing
Added comprehensive test suite (
ImportableTest) with 8 test methods covering:maybeFromString()usageAll existing tests continue to pass, ensuring no regressions.
💡 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.