diff --git a/src/AllowedFilter.php b/src/AllowedFilter.php index cddf759b..f222ff57 100644 --- a/src/AllowedFilter.php +++ b/src/AllowedFilter.php @@ -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); diff --git a/src/AllowedInclude.php b/src/AllowedInclude.php index cce9b06b..df07303a 100644 --- a/src/AllowedInclude.php +++ b/src/AllowedInclude.php @@ -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; } diff --git a/src/AllowedSort.php b/src/AllowedSort.php index d78c05ea..a94f71e7 100644 --- a/src/AllowedSort.php +++ b/src/AllowedSort.php @@ -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; @@ -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, diff --git a/src/Concerns/AddsFieldsToQuery.php b/src/Concerns/AddsFieldsToQuery.php index 80fc2964..70c50d28 100644 --- a/src/Concerns/AddsFieldsToQuery.php +++ b/src/Concerns/AddsFieldsToQuery.php @@ -32,7 +32,7 @@ public function allowedFields($fields): static return $this; } - protected function addRequestedModelFieldsToQuery() + protected function addRequestedModelFieldsToQuery(): void { $modelTableName = $this->getModel()->getTable(); @@ -70,7 +70,7 @@ public function getRequestedFieldsForRelatedTable(string $relation): array return $fields; } - protected function ensureAllFieldsExist() + protected function ensureAllFieldsExist(): void { $modelTable = $this->getModel()->getTable(); diff --git a/src/Concerns/AddsIncludesToQuery.php b/src/Concerns/AddsIncludesToQuery.php index a3b5a9fa..c221bd79 100644 --- a/src/Concerns/AddsIncludesToQuery.php +++ b/src/Concerns/AddsIncludesToQuery.php @@ -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); }); } @@ -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(); }); diff --git a/src/Concerns/FiltersQuery.php b/src/Concerns/FiltersQuery.php index 58183421..141b0e05 100644 --- a/src/Concerns/FiltersQuery.php +++ b/src/Concerns/FiltersQuery.php @@ -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 { @@ -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)) { @@ -41,8 +41,6 @@ protected function addFiltersToQuery() if ($filter->hasDefault()) { $filter->filter($this, $filter->getDefault()); - - return; } }); } @@ -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; diff --git a/src/Concerns/SortsQuery.php b/src/Concerns/SortsQuery.php index 469177ed..79d0f9f2 100644 --- a/src/Concerns/SortsQuery.php +++ b/src/Concerns/SortsQuery.php @@ -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 { @@ -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) { diff --git a/src/Exceptions/InvalidAppendQuery.php b/src/Exceptions/InvalidAppendQuery.php index 04082953..6a512644 100644 --- a/src/Exceptions/InvalidAppendQuery.php +++ b/src/Exceptions/InvalidAppendQuery.php @@ -7,17 +7,10 @@ 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}`."; @@ -25,7 +18,7 @@ public function __construct(Collection $appendsNotAllowed, Collection $allowedAp 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()); } diff --git a/src/Exceptions/InvalidFieldQuery.php b/src/Exceptions/InvalidFieldQuery.php index bc89d6cc..77d22866 100644 --- a/src/Exceptions/InvalidFieldQuery.php +++ b/src/Exceptions/InvalidFieldQuery.php @@ -7,17 +7,10 @@ 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}`."; @@ -25,7 +18,7 @@ public function __construct(Collection $unknownFields, Collection $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()); } diff --git a/src/Exceptions/InvalidFilterQuery.php b/src/Exceptions/InvalidFilterQuery.php index 8e6c8766..9281bd8f 100644 --- a/src/Exceptions/InvalidFilterQuery.php +++ b/src/Exceptions/InvalidFilterQuery.php @@ -7,17 +7,10 @@ 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}`."; @@ -25,7 +18,7 @@ public function __construct(Collection $unknownFilters, Collection $allowedFilte 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()); } diff --git a/src/Exceptions/InvalidFilterValue.php b/src/Exceptions/InvalidFilterValue.php index 473568a6..a01d636e 100644 --- a/src/Exceptions/InvalidFilterValue.php +++ b/src/Exceptions/InvalidFilterValue.php @@ -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."); } diff --git a/src/Exceptions/InvalidIncludeQuery.php b/src/Exceptions/InvalidIncludeQuery.php index c214ae21..4540e8f1 100644 --- a/src/Exceptions/InvalidIncludeQuery.php +++ b/src/Exceptions/InvalidIncludeQuery.php @@ -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. "; @@ -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()); } diff --git a/src/Exceptions/InvalidSortQuery.php b/src/Exceptions/InvalidSortQuery.php index ae4219ed..dc952961 100644 --- a/src/Exceptions/InvalidSortQuery.php +++ b/src/Exceptions/InvalidSortQuery.php @@ -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()); } diff --git a/src/Exceptions/InvalidSubject.php b/src/Exceptions/InvalidSubject.php deleted file mode 100644 index 3cbdfddd..00000000 --- a/src/Exceptions/InvalidSubject.php +++ /dev/null @@ -1,20 +0,0 @@ -addRelationConstraint = $addRelationConstraint; } /** {@inheritdoc} */ @@ -62,7 +58,7 @@ protected function isRelationProperty(Builder $query, string $property): bool return is_a($query->getModel()->{$firstRelationship}(), Relation::class); } - protected function withRelationConstraint(Builder $query, $value, string $property) + protected function withRelationConstraint(Builder $query, mixed $value, string $property): void { [$relation, $property] = collect(explode('.', $property)) ->pipe(function (Collection $parts) { diff --git a/src/Filters/FiltersPartial.php b/src/Filters/FiltersPartial.php index e0106549..0276611a 100644 --- a/src/Filters/FiltersPartial.php +++ b/src/Filters/FiltersPartial.php @@ -42,7 +42,7 @@ public function __invoke(Builder $query, $value, string $property) $query->whereRaw($sql, $bindings); } - protected function getWhereRawParameters($value, string $property): array + protected function getWhereRawParameters(mixed $value, string $property): array { $value = mb_strtolower((string) $value, 'UTF8'); diff --git a/src/Filters/FiltersScope.php b/src/Filters/FiltersScope.php index 25d9471f..c2793dcc 100644 --- a/src/Filters/FiltersScope.php +++ b/src/Filters/FiltersScope.php @@ -58,7 +58,8 @@ protected function resolveParameters(Builder $query, $values, string $scope): ar continue; } - $model = $this->getClass($parameter)->newInstance(); + /** @var TModelClass $model */ + $model = $this->getClass($parameter)?->newInstance(); $index = $parameter->getPosition() - 1; $value = $values[$index]; diff --git a/src/QueryBuilder.php b/src/QueryBuilder.php index 93e9a702..73f9051f 100755 --- a/src/QueryBuilder.php +++ b/src/QueryBuilder.php @@ -25,46 +25,15 @@ class QueryBuilder implements ArrayAccess use AddsFieldsToQuery; use ForwardsCalls; - /** @var \Spatie\QueryBuilder\QueryBuilderRequest */ - protected $request; + protected QueryBuilderRequest $request; - /** @var \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation */ - protected $subject; - - /** - * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $subject - * @param null|\Illuminate\Http\Request $request - */ - public function __construct($subject, ?Request $request = null) - { - $this->initializeSubject($subject) - ->initializeRequest($request ?? app(Request::class)); - } - - /** - * @param \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Relations\Relation $subject - * - * @return $this - */ - protected function initializeSubject($subject): static - { - throw_unless( - $subject instanceof EloquentBuilder || $subject instanceof Relation, - InvalidSubject::make($subject) - ); - - $this->subject = $subject; - - return $this; - } - - protected function initializeRequest(?Request $request = null): static + public function __construct( + protected EloquentBuilder|Relation $subject, + ?Request $request = null) { $this->request = $request ? QueryBuilderRequest::fromRequest($request) : app(QueryBuilderRequest::class); - - return $this; } public function getEloquentBuilder(): EloquentBuilder @@ -73,26 +42,18 @@ public function getEloquentBuilder(): EloquentBuilder return $this->subject; } - if ($this->subject instanceof Relation) { - return $this->subject->getQuery(); - } - - throw InvalidSubject::make($this->subject); + return $this->subject->getQuery(); } - public function getSubject() + public function getSubject(): Relation|EloquentBuilder { return $this->subject; } - /** - * @param EloquentBuilder|Relation|string $subject - * @param Request|null $request - * - * @return static - */ - public static function for($subject, ?Request $request = null): static - { + public static function for( + EloquentBuilder|Relation|string $subject, + ?Request $request = null + ): static { if (is_subclass_of($subject, Model::class)) { $subject = $subject::query(); } @@ -115,7 +76,7 @@ public function __call($name, $arguments) return $result; } - public function clone() + public function clone(): static { return clone $this; } diff --git a/src/QueryBuilderRequest.php b/src/QueryBuilderRequest.php index dfac1bbf..e2f30e14 100644 --- a/src/QueryBuilderRequest.php +++ b/src/QueryBuilderRequest.php @@ -123,12 +123,8 @@ public function filters(): Collection }); } - /** - * @param $value - * - * @return array|bool|null - */ - protected function getFilterValue($value) + /** @return array|float|int|string|bool|null */ + protected function getFilterValue(mixed $value): mixed { if (empty($value)) { return $value; diff --git a/src/QueryBuilderServiceProvider.php b/src/QueryBuilderServiceProvider.php index c7e7b199..da8b9313 100644 --- a/src/QueryBuilderServiceProvider.php +++ b/src/QueryBuilderServiceProvider.php @@ -14,14 +14,14 @@ public function configurePackage(Package $package): void ->hasConfigFile(); } - public function registeringPackage() + public function registeringPackage(): void { $this->app->bind(QueryBuilderRequest::class, function ($app) { return QueryBuilderRequest::fromRequest($app['request']); }); } - public function provides() + public function provides(): array { return [ QueryBuilderRequest::class, diff --git a/tests/QueryBuilderTest.php b/tests/QueryBuilderTest.php index f926fd71..3584dca6 100644 --- a/tests/QueryBuilderTest.php +++ b/tests/QueryBuilderTest.php @@ -2,8 +2,6 @@ use Illuminate\Database\Eloquent\Builder; use Illuminate\Http\Request; -use PHPUnit\Util\Test; -use Spatie\QueryBuilder\Exceptions\InvalidSubject; use Spatie\QueryBuilder\QueryBuilder; use Spatie\QueryBuilder\QueryBuilderRequest; use Spatie\QueryBuilder\Sorts\Sort; @@ -70,17 +68,13 @@ }); it('can not be given a string that is not a class name', function () { - $this->expectException(InvalidSubject::class); - - $this->expectExceptionMessage('Subject type `string` is invalid.'); + $this->expectException(TypeError::class); QueryBuilder::for('not a class name'); }); it('can not be given an object that is neither relation nor eloquent builder', function () { - $this->expectException(InvalidSubject::class); - - $this->expectExceptionMessage(sprintf('Subject class `%s` is invalid.', self::class)); + $this->expectException(TypeError::class); QueryBuilder::for($this); });