-
Notifications
You must be signed in to change notification settings - Fork 11.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[10.x] Add Closure Type Hinting for Query Builders #48562
Conversation
3944638
to
6c05cb0
Compare
Do any of these situations where you type-hinted a database query builder also support an Eloquent query builder because of the decorated nature of the Eloquent query builder? |
@taylorotwell The closures do not receive an Eloquent query builder even when used within that context. This is because the Eloquent query builder doesn't extend the underlying query builder class; instead, it forwards the calls. The motivation behind adding these type hints was to address the potential confusion that arose from this behavior, as we encountered issues in our own code. Typically, the closures are provided with a query from I hope this clarifies the reasoning behind the type hints. If you have any other concerns let me know. |
This unfortunately breaks phpstan in the following example: DB::table('table')
->leftJoin('other', fn (JoinClause $sub) => $sub->on('column', 'other')) Error:
The error makes sense, because the join callback should not return anything. This however prevents us from using the arrow function as syntactic sugar. What is your opinion about that @taylorotwell ? |
This reverts commit c6e4fa8.
Guess I'll revert for now. I swear we spend more time maintaining these comments than code at this point. 😬 |
I can send a follow in a little bit if you like? |
@AJenbo best that we just don't touch docblock for now. |
If I can give my opinion I would suggest we hint it as accepting both Alternativ we can also hint it as |
@driesvints fair enough, would it be something to target for 11.x instead then? Adding these hints did allow us to catch a handful of bugs in our production code so I do think there is a real value to adding them in the long run. |
@AJenbo I agree that this would be an improvement if carefully done. But I run into two more errors: One for this: $users = User::with(['posts' => function (Builder $query) {
$query->where('title', 'like', '%code%');
}])->get();
And one more here: User::query()->each(function (User $user): void {
// ...
}
|
@AJenbo no, let's just leave this be. Like Taylor says, this is giving us too much headaches lately. |
Yeah this one was a mistake I made, I was actually in the middle of testing a PR to fix this when things got reverted. Things get a bit hard to track because of the many places where calls are forwarded using
The thing here is that you can break out of @driesvints Ok, sorry for any trouble it may have caused, and thank you for the feedback. Is there any sort of roadmap/strategy for generally improving types and hints in Laravel? Maybe a better place to start would be looking at internal consistency? |
This pull request adds type hinting for closures in Laravel's query builders and Eloquent, enhancing IDE support by providing hints about expected input functions and available operations for the passed builder type.
This improvement follows the existing pattern applied to some closures, such as in
Illuminate\Database\Eloquent\Builder::withSavepointIfNeeded()
. For more information on callable types in PHPDoc, refer to phpstan.org phpdoc callables.Additionally, this PR removes obsolete
@param
annotations for parameters that have long been removed from the functions themselves.Your feedback and review are greatly appreciated.