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
Non-generic type when type hinting builder on Eloquent where*
methods
#926
Comments
Closures cannot be typehinted for PHPStan, so cannot say Wait a little bit and it will be possible. |
This works without errors, so it works fine type hinting the correct thing: <?php
use App\Models\User;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Builder;
/**
* @template TModel of Model
*/
class MockBuilder
{
/**
* @param \Closure(Builder<TModel>): void $closure
*/
public function where(\Closure $closure)
{
$closure($this);
}
}
/** @var MockBuilder<User> $builder */
$builder = new MockBuilder;
$builder->where(function (Builder $query) {
$query->withTrashed();
}); PhpStan cannot find errors in the example above. |
The closure/anonymous function itself has the problem |
For the /**
* Add a basic where clause to the query.
*
- * @param \Closure|array<model-property<TModelClass>|int, mixed>|\Illuminate\Database\Query\Expression $column
+ * @param \Closure(static<TModelClass>): void|\Illuminate\Database\Query\Expression $column
* @param mixed $operator
* @param mixed $value
* @param string $boolean
* @return static
*/
public function where($column, $operator = null, $value = null, $boolean = 'and'); For some reason <?php
use App\Models\User;
use Illuminate\Database\Eloquent\Builder;
User::query()->where(function (Builder $query) {
$query->withTrashed();
}); So at some level it could be fixed and is working... |
Does this only happen with |
|
--level
used: 2Description
For PhpStorm we like to type hint the builder on
where*
methods when building an Eloquent query for that aawesome autocompletion PhpStorm an offer.However I'm seeing something that might be totally expected but I found unexpected.
When type hinting the (Eloquent)
Builder
on the closure Larastan uses that over the (what I can only assume being injected from stub) generic variant of the builder. I would expect Larastan to also inject the generic version of the builder when I type hint the builder myself since that is just a more specific type of the builder just like I would use a native/naked type hint and use the docblocks to expand on that type hint with generics.Laravel code where the issue was found
(
withTrashed
andvalidated
are both scopes on the models being queried)The text was updated successfully, but these errors were encountered: