PHPDoc Generics
Hi, in an enterprise project and lack of type information and some magic functions is starting to drive me crazy. I know PHPStorm has some extra PHPDoc functionality, so I'm wanting to see if this is possible.
EntityType::where(['id' => 1]) /*Returns \EntityType\EntityList (or EntityList<EntityType> if you prefer - each Entity type has its own EntityList class, which extends a global EntityList Class*/
->first() /*Returns EntityType. first() is called from the root EntityList class and returns and instance of EntityType. */
Is it possible to annotate where() and first() to allow for that kind of generic?
E.g
Users:where(['id' => 1]) //Returns \Users\EntityList
->first() //Returns instance of Users
Thanks
Please sign in to leave a comment.
Something like that should do:
Thank you!
The where() function is in a generic entity model, so Users::Where() and Objects::where() are the same function.
Return type EntityList<$this> doesn’t seem to work. How do you get the type from the static class calling it?
The structure is a bit more complex from digging into it, but I think that’s the issue.
EDIT: Explicitly putting <User> as the return type also doesn't work.
So the actual structure looks like this
If you are not okay with annotating
Entities::where()with/** @return EntityList<User> */, the only way is to separate the chained call with a variable, because it has to be specified what exact element the collection holds.Like this:
We have a feature request to support generics in
@method, but it hasn't been implemented yet:https://youtrack.jetbrains.com/issue/WI-62267
When I copy your code into an empty file in an empty project,
$useris expectedly detected asmixed|Users, because you have a typo in thewhere()'s@return.What PhpStorm version are you using? It should be at least 2021.2 to have all this supported.
Thanks Eugene! What was my typo there? I could not get it to return anything other than mixed when I was playing around with it today.
I’m perfectly happy to annotate where(). And with regards to getting the static class that’s calling where() should I use EntityList<$this>?
/** @return EntityList<Users> */instead of/** @return EntityList<User> */. Mind the plural Users.I am confused. Even if this was supported,
EntityList<$this>in Users would've meant thatUsers::where()returns a collection of instances of Users, while in fact, it returns an EntityList of instances of User.What am I missing?
I've added this question to my previous reply too late, so you probably didn't see it.
What is your PhpStorm version?
If it's 2021.2 or newer, please try creating a new empty project and create a single file out of your snippet there. Is it any better?
Oh. Please never mind, something in the snippet is only supported in 2021.3, 2021.2.3 doesn't have it. Please upgrade.
Thanks, I thought I updated to most recent before I tried, but I only upgraded to 2021.2.3.
On 2021.3, I can get it to work when the return type is explicity cast as EntityList<User>.
However I need to get the static calling class.
where() is a method in the parent class of Users as shown in sample above, so it can be called statically by Users or any of its siblings.
e.g
So I was hoping return type of EntityList<$this> or EntityList<self> would work, but doesn't seem to.
Just not supported at the moment? Should I make a feature request? I'm coming from F# so I'm a very big fan of having types known 😂
Oh, now I see, thank you.
For some reason, initially, I assumed that
Users::where()returnsEntityList<User>, notEntityList<Users>, and then I was ignoring all your attempts to convince me otherwise.Unfortunately, it's not. The request is already there: https://youtrack.jetbrains.com/issue/WI-63722
Please vote for it too to get notified when there's any progress.
Perfect, thanks for your help! I look forward to seeing it in a future release.