Skip to content

Commit

Permalink
fixed a bunch of phpstan issues
Browse files Browse the repository at this point in the history
  • Loading branch information
Nielsvanpach committed Nov 17, 2023
1 parent 3ca0580 commit 3e1582d
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 218 deletions.
31 changes: 10 additions & 21 deletions src/AllowedFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,39 +14,28 @@

class AllowedFilter
{
/** @var \Spatie\QueryBuilder\Filters\Filter */
protected $filterClass;
protected string $internalName;

/** @var string */
protected $name;

/** @var string */
protected $internalName;

/** @var \Illuminate\Support\Collection */
protected $ignored;
protected Collection $ignored;

/** @var mixed */
protected $default;

/** @var bool */
protected $hasDefault = false;

/** @var bool */
protected $nullable = false;

public function __construct(string $name, Filter $filterClass, ?string $internalName = null)
{
$this->name = $name;
protected bool $hasDefault = false;

$this->filterClass = $filterClass;
protected bool $nullable = false;

public function __construct(
protected string $name,
protected Filter $filterClass,
?string $internalName = null
) {
$this->ignored = Collection::make();

$this->internalName = $internalName ?? $name;
}

public function filter(QueryBuilder $query, $value)
public function filter(QueryBuilder $query, $value): void
{
$valueToFilter = $this->resolveValueForFiltering($value);

Expand Down
18 changes: 6 additions & 12 deletions src/AllowedInclude.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,13 @@

class AllowedInclude
{
/** @var string */
protected $name;
protected string $internalName;

/** @var IncludeInterface */
protected $includeClass;

/** @var string|null */
protected $internalName;

public function __construct(string $name, IncludeInterface $includeClass, ?string $internalName = null)
{
$this->name = $name;
$this->includeClass = $includeClass;
public function __construct(
protected string $name,
protected IncludeInterface $includeClass,
?string $internalName = null
) {
$this->internalName = $internalName ?? $this->name;
}

Expand Down
18 changes: 4 additions & 14 deletions src/AllowedSort.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,14 @@

class AllowedSort
{
/** @var \Spatie\QueryBuilder\Sorts\Sort */
protected $sortClass;
protected string $defaultDirection;

/** @var string */
protected $name;
protected string $internalName;

/** @var string */
protected $defaultDirection;

/** @var string */
protected $internalName;

public function __construct(string $name, Sort $sortClass, ?string $internalName = null)
public function __construct(protected string $name, protected Sort $sortClass, ?string $internalName = null)
{
$this->name = ltrim($name, '-');

$this->sortClass = $sortClass;

$this->defaultDirection = static::parseSortDirection($name);

$this->internalName = $internalName ?? $this->name;
Expand Down Expand Up @@ -75,7 +65,7 @@ public function getInternalName(): string
return $this->internalName;
}

public function defaultDirection(string $defaultDirection)
public function defaultDirection(string $defaultDirection): static
{
if (! in_array($defaultDirection, [
SortDirection::ASCENDING,
Expand Down
4 changes: 2 additions & 2 deletions src/Concerns/AddsFieldsToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function allowedFields($fields): static
return $this;
}

protected function addRequestedModelFieldsToQuery()
protected function addRequestedModelFieldsToQuery(): void
{
$modelTableName = $this->getModel()->getTable();

Expand Down Expand Up @@ -70,7 +70,7 @@ public function getRequestedFieldsForRelatedTable(string $relation): array
return $fields;
}

protected function ensureAllFieldsExist()
protected function ensureAllFieldsExist(): void
{
$modelTable = $this->getModel()->getTable();

Expand Down
8 changes: 4 additions & 4 deletions src/Concerns/AddsIncludesToQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,12 @@ public function allowedIncludes($includes): static
return $this;
}

protected function addIncludesToQuery(Collection $includes)
protected function addIncludesToQuery(Collection $includes): void
{
$includes->each(function ($include) {
$include = $this->findInclude($include);

$include->include($this);
$include?->include($this);
});
}

Expand All @@ -67,11 +67,11 @@ protected function findInclude(string $include): ?AllowedInclude
});
}

protected function ensureAllIncludesExist()
protected function ensureAllIncludesExist(): void
{
$includes = $this->request->includes();

$allowedIncludeNames = $this->allowedIncludes->map(function (AllowedInclude $allowedInclude) {
$allowedIncludeNames = $this->allowedIncludes?->map(function (AllowedInclude $allowedInclude) {
return $allowedInclude->getName();
});

Expand Down
10 changes: 4 additions & 6 deletions src/Concerns/FiltersQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Spatie\QueryBuilder\Concerns;

use Illuminate\Support\Collection;
use Spatie\QueryBuilder\AllowedFilter;
use Spatie\QueryBuilder\Exceptions\InvalidFilterQuery;

trait FiltersQuery
{
/** @var \Illuminate\Support\Collection */
protected $allowedFilters;
protected Collection$allowedFilters;

public function allowedFilters($filters): static
{
Expand All @@ -29,7 +29,7 @@ public function allowedFilters($filters): static
return $this;
}

protected function addFiltersToQuery()
protected function addFiltersToQuery(): void
{
$this->allowedFilters->each(function (AllowedFilter $filter) {
if ($this->isFilterRequested($filter)) {
Expand All @@ -41,8 +41,6 @@ protected function addFiltersToQuery()

if ($filter->hasDefault()) {
$filter->filter($this, $filter->getDefault());

return;
}
});
}
Expand All @@ -60,7 +58,7 @@ protected function isFilterRequested(AllowedFilter $allowedFilter): bool
return $this->request->filters()->has($allowedFilter->getName());
}

protected function ensureAllFiltersExist()
protected function ensureAllFiltersExist(): void
{
if (config('query-builder.disable_invalid_filter_query_exception', false)) {
return;
Expand Down
6 changes: 3 additions & 3 deletions src/Concerns/SortsQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

namespace Spatie\QueryBuilder\Concerns;

use Illuminate\Support\Collection;
use Spatie\QueryBuilder\AllowedSort;
use Spatie\QueryBuilder\Exceptions\InvalidSortQuery;

trait SortsQuery
{
/** @var \Illuminate\Support\Collection */
protected $allowedSorts;
protected Collection $allowedSorts;

public function allowedSorts($sorts): static
{
Expand Down Expand Up @@ -69,7 +69,7 @@ public function defaultSorts($sorts): static
return $this;
}

protected function addRequestedSortsToQuery()
protected function addRequestedSortsToQuery(): void
{
$this->request->sorts()
->each(function (string $property) {
Expand Down
17 changes: 5 additions & 12 deletions src/Exceptions/InvalidAppendQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@

class InvalidAppendQuery extends InvalidQuery
{
/** @var \Illuminate\Support\Collection */
public $appendsNotAllowed;

/** @var \Illuminate\Support\Collection */
public $allowedAppends;

public function __construct(Collection $appendsNotAllowed, Collection $allowedAppends)
{
$this->appendsNotAllowed = $appendsNotAllowed;
$this->allowedAppends = $allowedAppends;

public function __construct(
public Collection $appendsNotAllowed,
public Collection $allowedAppends
) {
$appendsNotAllowed = $appendsNotAllowed->implode(', ');
$allowedAppends = $allowedAppends->implode(', ');
$message = "Requested append(s) `{$appendsNotAllowed}` are not allowed. Allowed append(s) are `{$allowedAppends}`.";

parent::__construct(Response::HTTP_BAD_REQUEST, $message);
}

public static function appendsNotAllowed(Collection $appendsNotAllowed, Collection $allowedAppends)
public static function appendsNotAllowed(Collection $appendsNotAllowed, Collection $allowedAppends): static
{
return new static(...func_get_args());
}
Expand Down
17 changes: 5 additions & 12 deletions src/Exceptions/InvalidFieldQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@

class InvalidFieldQuery extends InvalidQuery
{
/** @var \Illuminate\Support\Collection */
public $unknownFields;

/** @var \Illuminate\Support\Collection */
public $allowedFields;

public function __construct(Collection $unknownFields, Collection $allowedFields)
{
$this->unknownFields = $unknownFields;
$this->allowedFields = $allowedFields;

public function __construct(
public Collection $unknownFields,
public Collection $allowedFields
) {
$unknownFields = $unknownFields->implode(', ');
$allowedFields = $allowedFields->implode(', ');
$message = "Requested field(s) `{$unknownFields}` are not allowed. Allowed field(s) are `{$allowedFields}`.";

parent::__construct(Response::HTTP_BAD_REQUEST, $message);
}

public static function fieldsNotAllowed(Collection $unknownFields, Collection $allowedFields)
public static function fieldsNotAllowed(Collection $unknownFields, Collection $allowedFields): static
{
return new static(...func_get_args());
}
Expand Down
17 changes: 5 additions & 12 deletions src/Exceptions/InvalidFilterQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,18 @@

class InvalidFilterQuery extends InvalidQuery
{
/** @var \Illuminate\Support\Collection */
public $unknownFilters;

/** @var \Illuminate\Support\Collection */
public $allowedFilters;

public function __construct(Collection $unknownFilters, Collection $allowedFilters)
{
$this->unknownFilters = $unknownFilters;
$this->allowedFilters = $allowedFilters;

public function __construct(
public Collection $unknownFilters,
public Collection $allowedFilters
) {
$unknownFilters = $this->unknownFilters->implode(', ');
$allowedFilters = $this->allowedFilters->implode(', ');
$message = "Requested filter(s) `{$unknownFilters}` are not allowed. Allowed filter(s) are `{$allowedFilters}`.";

parent::__construct(Response::HTTP_BAD_REQUEST, $message);
}

public static function filtersNotAllowed(Collection $unknownFilters, Collection $allowedFilters)
public static function filtersNotAllowed(Collection $unknownFilters, Collection $allowedFilters): static
{
return new static(...func_get_args());
}
Expand Down
2 changes: 1 addition & 1 deletion src/Exceptions/InvalidFilterValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class InvalidFilterValue extends Exception
{
public static function make($value)
public static function make($value): static
{
return new static("Filter value `{$value}` is invalid.");
}
Expand Down
17 changes: 5 additions & 12 deletions src/Exceptions/InvalidIncludeQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,10 @@

class InvalidIncludeQuery extends InvalidQuery
{
/** @var \Illuminate\Support\Collection */
public $unknownIncludes;

/** @var \Illuminate\Support\Collection */
public $allowedIncludes;

public function __construct(Collection $unknownIncludes, Collection $allowedIncludes)
{
$this->unknownIncludes = $unknownIncludes;
$this->allowedIncludes = $allowedIncludes;

public function __construct(
public Collection $unknownIncludes,
public Collection $allowedIncludes
) {
$unknownIncludes = $unknownIncludes->implode(', ');

$message = "Requested include(s) `{$unknownIncludes}` are not allowed. ";
Expand All @@ -32,7 +25,7 @@ public function __construct(Collection $unknownIncludes, Collection $allowedIncl
parent::__construct(Response::HTTP_BAD_REQUEST, $message);
}

public static function includesNotAllowed(Collection $unknownIncludes, Collection $allowedIncludes)
public static function includesNotAllowed(Collection $unknownIncludes, Collection $allowedIncludes): static
{
return new static(...func_get_args());
}
Expand Down
18 changes: 6 additions & 12 deletions src/Exceptions/InvalidSortQuery.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,25 +7,19 @@

class InvalidSortQuery extends InvalidQuery
{
/** @var \Illuminate\Support\Collection */
public $unknownSorts;

/** @var \Illuminate\Support\Collection */
public $allowedSorts;

public function __construct(Collection $unknownSorts, Collection $allowedSorts)
{
$this->unknownSorts = $unknownSorts;
$this->allowedSorts = $allowedSorts;

public function __construct(
public Collection $unknownSorts,
public Collection $allowedSorts
) {
$allowedSorts = $allowedSorts->implode(', ');
$unknownSorts = $unknownSorts->implode(', ');

$message = "Requested sort(s) `{$unknownSorts}` is not allowed. Allowed sort(s) are `{$allowedSorts}`.";

parent::__construct(Response::HTTP_BAD_REQUEST, $message);
}

public static function sortsNotAllowed(Collection $unknownSorts, Collection $allowedSorts)
public static function sortsNotAllowed(Collection $unknownSorts, Collection $allowedSorts): static
{
return new static(...func_get_args());
}
Expand Down
Loading

0 comments on commit 3e1582d

Please sign in to comment.