PHPStorm : Code completion in PHP - FQN is returned rather than import

Hello,

I'm currently working on a Laravel/Livewire/Filament project and whenever I edit my Filament resource file and I try to use any component, it offers completion correctly but when selected, it uses fully qualified name (FQN) rather than an import.

I tried to alter my auto import settings to no avail. It is still completing as FQN.

If I enter manually the class name then make my static call of the make method, I can import through the Alt+Enter classic option and then it will import the class.

Is it possible to make it import on the fly rather than always using the FQN? Is there a shortcut I'm not aware of?

I thank you for your help.

0

Hi there,

I tried to alter my auto import settings to no avail. It is still completing as FQN.

Please show your settings then.

As well as an example of such a file (a small one, so it can show the whole context on a screenshot).

0

Hello there,

Andriy Bazanov 

My settings are as follow : 

My issue can be characterized by this. When I type, the correct Class name, PHP Storm suggest me the right choice : 

But then, when I select and press enter, here what it come with :

And then when I hit enter:

It insert the FQN rather than add add an import.

If I type manually the Class name, and alt enter on the missing import, it works as intended:

Anyway, thank you for your kind help.
 

0

No idea TBH. I've checked all places that I could think of right now… and it still works fine/the same to me – it uses auto import (be it in global or some random namespace).

Please try placing a few simple classes in a brand new project – will it work there?

This may somehow depend on your current file content (it should not, but who knows).

P.S. I have seen similar issues/tickets before .. but cannot find them right now and do not remember what the issue was there… Will try finding them again later. Only WI-74251 so far with no answer from the OP.

 

BTW: when you complete the class with FQN, if you then place a caret on a class there and press Alt+Enter – will the IDE offer you to simplify FQN?

0

Andriy Bazanov  Yes it does : 

Strange it doesn't do it directly.

0

1. So .. does it work in a brand new project with just a few files?

2. If it does – does it work if you make a brand new Laravel project with Filament package included?

3. If still nothing (or you do not want to try #1 and #2) – please disable all custom (not bundled by default) plugins and restart the IDE. Any better?

4. If still not good -- also  try “File | Invalidates Caches…” and restart the IDE and wait until it finishes indexing.

5. Try “File | Repair IDE” and follow the steps there.

0

Andriy Bazanov 
1. Yes it does, it even does work in Laravel standard models.

2. In the new project, everything is fine in any standard Laravel project file except Filament Resources.

3. Did it, nothing changes.

4. Already tried that before, did nothing

5. Did that as well and changed nothing.

I looked into the file that Filament create when making new resources and I noticed the default calls use FQN as well :

Could it be something in the library itself that enforce this? When editing the Create/Edit/List class that Filament create as well, the auto-complete works fine and import. It's just for those call inside of those arrays in resource file it uses FQN.

0

I looked into the file that Filament create when making new resources and I noticed the default calls use FQN as well

Is this the same file where you are trying to have the import working... or a different one?

 

Could it be something in the library itself that enforce this?

Library itself – no. A specific file – could be. If this file is generated by Filament (e.g. artisan make xxx kind of stuff) then it's up to what they have in their stub files for that.

 

Anyway: based on what I see on that last screenshot… it looks to me that this file may have a “partial import” (I mean: use Filament\Tables; – just like that). See, all classes start with Tables\… while as I understand from other screenshots the FQN should be Filament\Tables\…. As I see it this should explain such a behaviour (makes good sense to me).

P.S. This is why I've asked for the whole file to get the big picture and not just two lines of who-knows-how-long file…

0

Andriy Bazanov 
You are right, there's partial imports in the imports. I didn't check that, there's the whole Filament\Tables as it's used as well. I understand a bit more now, sorry for this long post for nothing then. 

I was sure I did something wrong, not that it was just the way the file was constructed.

Yes, the file I used on the fresh install was a new fresh class coming from artisan make:filament-resource User as I was trying to do things properly. And in fact, there's a bunch of default imports including partial imports (I tend to forget that PHP Storm minimize those).

Thank you for helping me there. The fact is those partial imports being used, sow may I circumvent this? If its possible? As it makes the completion a bit useless.

Thank you for your help.

P.S. Here's the file :

<?php

namespace App\Filament\Resources;

use App\Filament\Resources\CategoryResource\Pages;
use App\Filament\Resources\CategoryResource\RelationManagers;
use App\Models\Category;
use Filament\Forms;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Form;
use Filament\Forms\Set;
use Filament\Resources\Resource;
use Filament\Tables;
use Filament\Tables\Columns\TextColumn;
use Filament\Tables\Table;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\SoftDeletingScope;
use Illuminate\Support\Str;

class CategoryResource extends Resource
{
    protected static ?string $model = Category::class;

    protected static ?string $navigationIcon = 'heroicon-o-rectangle-stack';

    public static function form(Form $form): Form
    {
        return $form
            ->schema([
                TextInput::make('title')
                         ->live()
                         ->debounce()
                         ->required()
                         ->minLength(1)
                         ->maxLength(150)
                         ->afterStateUpdated(function (string $operation, $state, Set $set) {
                             if ($operation === 'edit') {
                                 return;
                             }
                             $set('slug', Str::slug($state));
                         }),
                TextInput::make('slug')->required()->minLength(1)->unique(ignoreRecord: true),
                TextInput::make('text_color')->nullable(),
                TextInput::make('bg_color')->nullable(),
            ]);
    }

    public static function table(Table $table): Table
    {
        return $table
            ->columns([
                TextColumn::make('title')->sortable()->searchable(),
                TextColumn::make('slug')->sortable()->searchable(),
                TextColumn::make('text_color'),
                TextColumn::make('bg_color'),
            ])
            ->filters([
                //
            ])
            ->actions([
                Tables\Actions\EditAction::make(),
            ])
            ->bulkActions([
                Tables\Actions\BulkActionGroup::make([
                    Tables\Actions\DeleteBulkAction::make(),
                ]),
            ])
            ->emptyStateActions([
                Tables\Actions\CreateAction::make(),
            ]);
    }

    public static function getRelations(): array
    {
        return [
            //
        ];
    }

    public static function getPages(): array
    {
        return [
            'index'  => Pages\ListCategories::route('/'),
            'create' => Pages\CreateCategory::route('/create'),
            'edit'   => Pages\EditCategory::route('/{record}/edit'),
        ];
    }
}
0

The fact is those partial imports being used, sow may I circumvent this? If its possible? As it makes the completion a bit useless.

Accordingly to the file content the IDE seems to behave correctly: it inserts “class names” as per your existing imports.

And it's not just Tables but Pages (in the getPages() function) and possibly even Forms as well.

It could be done for a reason (has sense to me) – maybe you do not need to have that many individual imports for not-so-important classes (interface building actions) and instead focus on more relevant stuff (your own code).

 

TBH I do not know what the IDE can do here to automatically refactor this code: “expand” all such imports and use individual class imports only… (never had the need for this myself so never looked into).

The most obvious approach right now is to manually edit the generated file after it was just created to get rid of such imports and only then start working on the actual new code. Inconvenient for sure.

Can try overriding the stub file that Filament uses, but this most likely would also require overriding the make action (as it may contain the references there as well). I would ask at Filament Issue Tracker first about that though.

 

Wait and see a day or two; maybe PhpStorm Support Team member has anything better to suggest here. If you get no response here after a few days, just file a Support Ticket (the big “Submit a Request” button at the top of the page).

0

Hi Folks,

I have quickly checked this thread and if I got it correctly, the problem is caused by partial imports, right? 
Just to illustrate, when we have a namespace imported already, IDE uses FQN in completion:

Otherwise, an import is used:

I believe that this is an expected IDE behaviour and apparently, it was included after this old ticket:
https://youtrack.jetbrains.com/issue/WI-14998/Auto-import-Please-respect-existing-partial-imports

The only quick workaround that I may think of at the moment is to use “Simplify FQN” as suggested above (not a brilliant solution, I agree). 
Also, I think, we may create a new YouTrack report to make this behaviour controllable.

 

0

I have quickly checked this thread and if I got it correctly, the problem is caused by partial imports, right? 

Yes.

I believe that this is an expected IDE behaviour and apparently, it was included after this old ticket:

And this makes sense – the partial import is there for a reason already, so why not use it.

But as you can see this can be confusing at first (and even undesirable) if you have not seen such a usage before or for a while.

Also, I think, we may create a new YouTrack report to make this behaviour controllable.

Not for me to decide (since I'm not the OP), but right now I think that:

  • the current behaviour is OK (makes sense as per that aforementioned ticket);
  • I would rather see an Intention/Quick Fix that allows converting such Tables\Actions\BulkActionGroup::make() into a "full class " import:
use Filament\Tables\Actions\BulkActionGroup;
…
BulkActionGroup::make()
0

I would rather see an Intention/Quick Fix that allows converting such Tables\Actions\BulkActionGroup::make() into a "full class " import:

I think what would be nicer is auto-complete offers a way to do it on the fly, like a new shortcut with a modifier to force the import.

This being configurable could be nice too.

0

Thanks!
 

I would rather see an Intention/Quick Fix that allows converting such Tables\Actions\BulkActionGroup::make() into a "full class " import:

Correct me if I am wrong, I might be missing something, but would not “Simplify FQN” work here? The only drawback I can see is that it may make a partial import unused.

This being configurable could be nice too.

There is a tendency to avoid adding additional configuration settings (even though I suggested it before, huh) but I think I will submit a YouTrack report anyway.

0

Correct me if I am wrong, I might be missing something, but would not “Simplify FQN” work here?

TBH I have not tried it for such a scenario. And I'm a bit too lazy to make a test code to test it right now… If it works already like that -- great. @Francois can you try and confirm that?

The only drawback I can see is that it may make a partial import unused.

So it will need to be removed if there are no more uses for it, manually if needed.

0

I have not tried the original scenario as well but, I guess, it should work. Here is a simple test from my example above:

0

I want to thank you all for your help in this matter. I must admit I have never been really paying attention before at this behaviour before using Filament because I never had as many things to import at once (for better code readability) with a partial import interfering.

I didn't notice the partial import at first to be honest.

Now I don't find this behaviour most useful in this specific case and maybe should I suggest this to be included as an option from Laravel Idea module which support a lot of Laravel libraries rather than making it a PHPStorm issue? But as JetBrains supports Livewire and Filament is an adjacent project to it, I thought I would ask here.

Anyway, thanks again for assisting me.

As I pointed out before, the option to simplify does work, but would make it tiresome to do on every line when you have complex forms:

0

As I pointed out before, the option to simplify does work, but would make it tiresome to do on every line when you have complex forms:

 

I have just remembered that you may assign a shortcut to the specific intention:
Hope it will make the aforementioned workaround a bit smoother.

0

请先登录再写评论。