satanik
satanik

Reputation: 602

How to return dynamic type with @method PHPDoc tag in PHPStorm for static methods

I have tried to solve this for some time now, because it would help a lot in developing if stuff like this would be autocompleted and recognised.

So, I'm using Laravel and Eloquent uses a lot of magic methods to use all the Query magic directly on Models as well.

What I did so far, was preparing a base class BaseModel extending it from Eloquent-model and add the @method PHPDoc tags to specify that my inheriting models are having these functions as well.

Now there is this keyword which is supposedly used for fluent methods $this to indicate that the same instance is returned. The whole thing works when I use it that way e.g.

/**
 * @method $this find(int $id)
 */
class BaseModel extends Model

But what if I want to call them statically? Like this

User::find(1)->email;

I can change the line to this

 @method static BaseModel find(int $id)

Now it works as long as I chain some query methods, but it doesn't find the email it gives a

Field not found in BaseModel

This also applies to $this and self and I haven't encountered another method.

Is there actually a way to solve this? I could paste the same tag to every model and return the respective class, but that would be a little overkill just for convenience.

Thank you for you help in advance

Upvotes: 1

Views: 1921

Answers (1)

Tomáš Staník
Tomáš Staník

Reputation: 1437

If you want to extend Model class to recognize Eloquent methods just add above class PHPDoc comment

@mixin Eloquent

Example

<?php namespace App;

/**
 * User
 *
 * @mixin Eloquent
 */
class User extends Model {

Upvotes: 2

Related Questions