Skip to content

Commit

Permalink
Merge pull request #781 from kayw-geek/bugfix/fix-phpstan-generics
Browse files Browse the repository at this point in the history
Fix Laravel 9 PHPStan generic check for  `__invoke()` method of Filter
  • Loading branch information
freekmurze authored Jul 29, 2022
2 parents 6f76441 + 7180511 commit 5c6e514
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 1 deletion.
5 changes: 4 additions & 1 deletion src/Filters/Filter.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,13 @@

use Illuminate\Database\Eloquent\Builder;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
*/
interface Filter
{
/**
* @param \Illuminate\Database\Eloquent\Builder<\Illuminate\Database\Eloquent\Model> $query
* @param \Illuminate\Database\Eloquent\Builder<TModelClass> $query
* @param mixed $value
* @param string $property
*
Expand Down
4 changes: 4 additions & 0 deletions src/Filters/FiltersCallback.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Illuminate\Database\Eloquent\Builder;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass>
*/
class FiltersCallback implements Filter
{
/**
Expand Down
5 changes: 5 additions & 0 deletions src/Filters/FiltersExact.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
use Illuminate\Support\Collection;
use Illuminate\Support\Str;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass>
*/
class FiltersExact implements Filter
{
protected $relationConstraints = [];
Expand All @@ -19,6 +23,7 @@ public function __construct(bool $addRelationConstraint = true)
$this->addRelationConstraint = $addRelationConstraint;
}

/** {@inheritdoc} */
public function __invoke(Builder $query, $value, string $property)
{
if ($this->addRelationConstraint) {
Expand Down
5 changes: 5 additions & 0 deletions src/Filters/FiltersPartial.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,13 @@

use Illuminate\Database\Eloquent\Builder;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass>
*/
class FiltersPartial extends FiltersExact implements Filter
{
/** {@inheritdoc} */
public function __invoke(Builder $query, $value, string $property)
{
if ($this->addRelationConstraint) {
Expand Down
5 changes: 5 additions & 0 deletions src/Filters/FiltersScope.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,13 @@
use ReflectionUnionType;
use Spatie\QueryBuilder\Exceptions\InvalidFilterValue;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass>
*/
class FiltersScope implements Filter
{
/** {@inheritdoc} */
public function __invoke(Builder $query, $values, string $property): Builder
{
$propertyParts = collect(explode('.', $property));
Expand Down
4 changes: 4 additions & 0 deletions src/Filters/FiltersTrashed.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@

use Illuminate\Database\Eloquent\Builder;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass>
*/
class FiltersTrashed implements Filter
{
/** {@inheritdoc} */
Expand Down
5 changes: 5 additions & 0 deletions tests/TestClasses/Filters/FiltersTestModels.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,13 @@
use Illuminate\Database\Eloquent\Builder;
use Spatie\QueryBuilder\Filters\Filter;

/**
* @template TModelClass of \Illuminate\Database\Eloquent\Model
* @template-implements \Spatie\QueryBuilder\Filters\Filter<TModelClass>
*/
class FiltersTestModels implements Filter
{
/** {@inheritdoc} */
public function __invoke(Builder $query, $value, string $property): Builder
{
return $query->where($property, $value);
Expand Down

0 comments on commit 5c6e514

Please sign in to comment.