Skip to content

Commit

Permalink
Merge pull request #194 from shoxy/master
Browse files Browse the repository at this point in the history
Added missing Builder methods - count() and whereDate()
  • Loading branch information
mr-feek authored Aug 2, 2021
2 parents 183c230 + 0b869cc commit 76745c9
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 3 deletions.
2 changes: 1 addition & 1 deletion stubs/DBFacade.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -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
*/
Expand Down
15 changes: 15 additions & 0 deletions stubs/EloquentBuilder.stubphp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -347,4 +356,10 @@ class Builder
* @return TModel|null
*/
public function first($columns = ['*']) { }

/**
* @param string $columns
* @return int
*/
public function count($columns = '*') { }
}
4 changes: 2 additions & 2 deletions tests/acceptance/DBFacadeAlias.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
60 changes: 60 additions & 0 deletions tests/acceptance/EloquentBuilderTypes.feature
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit 76745c9

Please sign in to comment.