Skip to content

Add import class as alias, live aliasing and inlining #344

Description

@DarkGhostHunter

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:

Image

While, on PHPantom, there is no option

Image

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

  1. 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.
  2. 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.
  3. 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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions