Added missing Builder methods - count() and whereDate()

This commit is contained in:
shoxy 2021-07-29 22:14:05 +03:00
parent 183c230d33
commit 0b869cc153
4 changed files with 78 additions and 3 deletions

View File

@ -8,7 +8,7 @@ class DB extends Facade
* Create a raw database expression.
*
* @param mixed $value
* @return void
* @return \Illuminate\Database\Query\Expression
*
* @psalm-taint-sink sql $value
*/

View File

@ -73,6 +73,15 @@ class Builder
*/
public function where($column, $operator = null, $value = null, $boolean = 'and') { }
/**
* @param string $column
* @param string $operator
* @param \DateTimeInterface|string|null $value
* @param string $boolean
* @return self<TModel>
*/
public function whereDate($column, $operator, $value = null, $boolean = 'and') { }
/**
* @param \Closure|array|string $column
* @param mixed $operator
@ -347,4 +356,10 @@ class Builder
* @return TModel|null
*/
public function first($columns = ['*']) { }
/**
* @param string $columns
* @return int
*/
public function count($columns = '*') { }
}

View File

@ -23,8 +23,8 @@ Feature: DB facade alias
namespace Tests\Psalm\LaravelPlugin\Sandbox;
function test(): void {
\DB::raw(1);
function test(): \Illuminate\Database\Query\Expression {
return \DB::raw(1);
}
"""
When I run Psalm

View File

@ -225,3 +225,63 @@ Feature: Eloquent Builder types
"""
When I run Psalm
Then I see no errors
Scenario: can call whereDate with \DateTimeInterface|string|null
Given I have the following code
"""
/**
* @psalm-param Builder $builder
* @psalm-return Builder
*/
function test_whereDateWithDateTimeInterface(Builder $builder): Builder {
return $builder->whereDate('created_at', '>', new \DateTimeImmutable());
}
/**
* @psalm-param Builder $builder
* @psalm-return Builder
*/
function test_whereDateWithString(Builder $builder): Builder {
return $builder->whereDate('created_at', '>', (new \DateTimeImmutable())->format('d/m/Y'));
}
/**
* @psalm-param Builder $builder
* @psalm-return Builder
*/
function test_whereDateWithNull(Builder $builder): Builder {
return $builder->whereDate('created_at', '>', null);
}
"""
When I run Psalm
Then I see no errors
Scenario: can not call whereDate with incompatible type
Given I have the following code
"""
/**
* @psalm-param Builder $builder
* @psalm-return Builder
*/
function test_whereDateWithInt(Builder $builder): Builder {
return $builder->whereDate('created_at', '>', 1);
}
"""
When I run Psalm
Then I see these errors
| Type | Message |
| InvalidScalarArgument | Argument 3 of Illuminate\Database\Eloquent\Builder::whereDate expects DateTimeInterface\|null\|string, 1 provided |
Scenario: can call count on the builder instance
Given I have the following code
"""
/**
* @psalm-param Builder $builder
* @psalm-return int
*/
function test_whereDateWithInt(Builder $builder): int {
return $builder->count();
}
"""
When I run Psalm
Then I see no errors