Tell Larastan a query builder is a specific kind using interface? #1931
Unanswered
rudiedirkx
asked this question in
Q&A
Replies: 1 comment 1 reply
-
You should take a look into custom query builders which helps with static analysis and removes a lot of the laravel magic |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
I have a list of models that I want to query using a scope that they share (in name, not logic).
and then I want to load those models:
Larastan magically thinks
$class::query()
is anEloquentBuilder
, even though it doesn't know what$class
is, but okay. ButforTimeline()
doesn't exist onEloquentBuilder
, so I have to be more specific somehow. My models implementTimelineModel
:so maybe I can use that?
No, that doesn't work, that's too complex, I admit. Add a fake static method on the interface?
Also no. The
$query
dumped type isEloquentBuilder<TimelineModel>
, but I'm not sure that's even understandable for Larastan. And PHPStan complains anyway:because how would it know TimelineModel is always a Model..?
Maybe with a specific step in between?
Also no!
Only this works, but I don't want to repeat class names:
I hope the intention is clear. I want Larastan to understand what type $query is, but by interface, not class. Is that even possible? Does that even make sense...? 🤔 What would the return type even be...? Maybe my interface approach doesn't make sense... But I can't map
$models
/$class
to specific class-strings, the process before that is too complicated, no way static analysis is going to understand that.Beta Was this translation helpful? Give feedback.
All reactions