Description
When refactoring classes, some imports may collide with other of the same name. A good practice is to alias an import so both can coexist.
This is common option on PHPStorm:
While, on PHPantom, there is no option
Use case
Making code more expressive since imports: you have to check which import you're using when the name is the same across many namespaces.
Getter? From PHPUnit? Pest? Filament? Livewire? Another package that declares a class of the same name?
Proposed solution
- When importing a class that collides with another import of the same name, import it as
{PreviousNamespace}Class. For example use Illuminate\Database\Eloquent\Collection → ... as EloquentCollection.
- If the class to import is abstract, may as well use
Base{ClassName}, e.g. BaseTestCase.
- If the class is an interface, may as well use
{ClassName}Interface, e.g. JsonableInterface.
- If Laravel is installed, the convention is
{ClasName}Contract, e.g. JsonableContract.
- Allow the LSP to offer the option to alias an import. The class references in the file should be all updated to the alias. If the alias collides, fail.
- Allow the LSP to inline an aliased import. If the original name collides, fail.
Alternatives considered
No response
Code example
use Illuminate\Support\Collection;
use Illuminate\Database\Eloquent\Collection as EloquentCollection // ← this, automatic.
Description
When refactoring classes, some imports may collide with other of the same name. A good practice is to alias an import so both can coexist.
This is common option on PHPStorm:
While, on PHPantom, there is no option
Use case
Making code more expressive since imports: you have to check which import you're using when the name is the same across many namespaces.
Getter? From PHPUnit? Pest? Filament? Livewire? Another package that declares a class of the same name?
Proposed solution
{PreviousNamespace}Class. For exampleuse Illuminate\Database\Eloquent\Collection→... as EloquentCollection.Base{ClassName}, e.g.BaseTestCase.{ClassName}Interface, e.g.JsonableInterface.{ClasName}Contract, e.g.JsonableContract.Alternatives considered
No response
Code example