plugin icon indicating copy to clipboard operation
plugin copied to clipboard

[Bug]: Autocomplete not working on custom facades

Open yoeriboven opened this issue 3 years ago • 4 comments
trafficstars

Bug description

I have a custom facade which returns a custom collection.

Plans::all() doesn't autocomplete (since all isn't a static function) (new PlanCollection([]))->all() autocompletes fine.

/** @mixin PlanCollection */
class Plans extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'plans';
    }
}
// In AppServiceProvider
$this->app->singleton('plans', function () {
    return PlanCollection::withSites(config('plans'));
});
final class PlanCollection extends Collection
{
    public static function withPlans(array $plans): self
    {
        return new self($plans);
    }
}

I tried barryvdh/laravel-ide-helper and it also didn't work at first.

When I added the code below to config/app.php and regenerated the helper file it worked.

'aliases' => Facade::defaultAliases()->merge([
    'plans' => Plans::class,
])->toArray(),

Are you checking the aliases key for aliases?

Update

I now see that the ide-helper adds something like this:

/** @mixin PlanCollection */
class Plans extends Facade
{
    protected static function getFacadeAccessor()
    {
        return 'plans';
    }

    public function all() {

    }

    public function count() {
    
    }
}

It basically adds all methods to the facade.

Plugin version

5.1.1.213

Operating system

MacOS

Steps to reproduce

See code snippets above.

Relevant log output

No response

yoeriboven avatar Mar 07 '22 13:03 yoeriboven

Hi, Yoeri. Sorry for the very long response.

Could you please try to change /** @mixin PlanCollection */ to /** @see PlanCollection */ and then Generate Helper code in the main menu > Laravel > Generate Facade and Macro Helper code?

@mixin doesn't work well here, since facade methods are static...

adelf avatar Apr 07 '22 08:04 adelf

Oh, that worked! Thanks, Adel. :)

Any idea how I could get autocompletion on $plan?

foreach (Plans::all() as $plan) {
    $plan->somefunction();
}

How do I let PHPStorm know that Plans will return instances of Plan?

I tried @see PlanCollection<Plan> but that wasn't allowed. Using @mixin was allowed but didn't work.

It says $plan is mixed|null.

yoeriboven avatar Apr 07 '22 14:04 yoeriboven

stumbled upon this, how to implement this for vendor facade, it doesn't seems to pickup facades from https://github.com/GrahamCampbell/Laravel-GitLab ? that I need to edit vendor files and add the @see myself to make it works

fmiqbal avatar Apr 11 '23 17:04 fmiqbal

Hi, Yoeri. Sorry for the very long response.

Could you please try to change /** @mixin PlanCollection */ to /** @see PlanCollection */ and then Generate Helper code in the main menu > Laravel > Generate Facade and Macro Helper code?

@mixin doesn't work well here, since facade methods are static...

For anyone finding solution for custom facades, this is the solution. I think this should be documented somewhere.

dangnhdev avatar Oct 18 '23 16:10 dangnhdev