From 9e68f806b696d7abb8e7d013f5b1c07cfe048172 Mon Sep 17 00:00:00 2001 From: Alex Vanderbist Date: Fri, 10 May 2024 10:49:42 +0200 Subject: [PATCH] Improve phpstan --- .github/workflows/run-tests.yml | 3 --- phpstan-baseline.neon | 5 ----- src/Concerns/AddsIncludesToQuery.php | 17 +++++++--------- src/Concerns/SortsQuery.php | 30 ++++++---------------------- src/Filters/FiltersExact.php | 10 ++++------ src/Filters/FiltersPartial.php | 6 +++--- src/Filters/FiltersScope.php | 6 +----- 7 files changed, 21 insertions(+), 56 deletions(-) diff --git a/.github/workflows/run-tests.yml b/.github/workflows/run-tests.yml index 6978a60f..0f11236d 100644 --- a/.github/workflows/run-tests.yml +++ b/.github/workflows/run-tests.yml @@ -24,9 +24,6 @@ jobs: - laravel: 11.* testbench: 9.* carbon: ^2.63 - - laravel: 10.* - php: 8.0 - testbench: 8.* name: P${{ matrix.php }} - L${{ matrix.laravel }} - ${{ matrix.stability }} - ${{ matrix.os }} diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 85249926..97a7a636 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -29,8 +29,3 @@ parameters: message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\\\:\\:withoutTrashed\\(\\)\\.$#" count: 1 path: src/Filters/FiltersTrashed.php - - - - message: "#^PHPDoc tag @return with type Spatie\\\\QueryBuilder\\\\QueryBuilder is not subtype of native type static\\(Spatie\\\\QueryBuilder\\\\QueryBuilder\\)\\.$#" - count: 2 - path: src/QueryBuilder.php diff --git a/src/Concerns/AddsIncludesToQuery.php b/src/Concerns/AddsIncludesToQuery.php index 1dee0ca5..e495c432 100644 --- a/src/Concerns/AddsIncludesToQuery.php +++ b/src/Concerns/AddsIncludesToQuery.php @@ -64,9 +64,7 @@ protected function addIncludesToQuery(Collection $includes): void protected function findInclude(string $include): ?AllowedInclude { return $this->allowedIncludes - ->first(function (AllowedInclude $included) use ($include) { - return $included->isForInclude($include); - }); + ->first(fn(AllowedInclude $included) => $included->isForInclude($include)); } protected function ensureAllIncludesExist(): void @@ -77,9 +75,7 @@ protected function ensureAllIncludesExist(): void $includes = $this->request->includes(); - $allowedIncludeNames = $this->allowedIncludes?->map(function (AllowedInclude $allowedInclude) { - return $allowedInclude->getName(); - }); + $allowedIncludeNames = $this->allowedIncludes?->map(fn(AllowedInclude $allowedInclude) => $allowedInclude->getName()); $diff = $includes->diff($allowedIncludeNames); @@ -90,14 +86,15 @@ protected function ensureAllIncludesExist(): void // TODO: Check for non-existing relationships? } + /** + * @param Collection $includes + */ protected function filterNonExistingIncludes(Collection $includes): Collection { - if (config('query-builder.disable_invalid_includes_query_exception', false) == false) { + if (!config('query-builder.disable_invalid_includes_query_exception', false)) { return $includes; } - return $includes->filter(function ($include) { - return $this->findInclude($include); - }); + return $includes->filter(fn($include) => !is_null($this->findInclude($include))); } } diff --git a/src/Concerns/SortsQuery.php b/src/Concerns/SortsQuery.php index d56ebbea..f3398a1f 100644 --- a/src/Concerns/SortsQuery.php +++ b/src/Concerns/SortsQuery.php @@ -29,24 +29,14 @@ public function allowedSorts($sorts): static return $this; } - /** - * @param array|string|\Spatie\QueryBuilder\AllowedSort $sorts - * - * @return \Spatie\QueryBuilder\QueryBuilder - */ - public function defaultSort($sorts): static + public function defaultSort(AllowedSort|array|string $sorts): static { $sorts = is_array($sorts) ? $sorts : func_get_args(); return $this->defaultSorts($sorts); } - /** - * @param array|string|\Spatie\QueryBuilder\AllowedSort $sorts - * - * @return \Spatie\QueryBuilder\QueryBuilder - */ - public function defaultSorts($sorts): static + public function defaultSorts(AllowedSort|array|string $sorts): static { if ($this->request->sorts()->isNotEmpty()) { // We've got requested sorts. No need to parse defaults. @@ -64,9 +54,7 @@ public function defaultSorts($sorts): static return AllowedSort::field($sort); }) - ->each(function (AllowedSort $sort) { - $sort->sort($this); - }); + ->each(fn(AllowedSort $sort) => $sort->sort($this)); return $this; } @@ -88,9 +76,7 @@ protected function addRequestedSortsToQuery(): void protected function findSort(string $property): ?AllowedSort { return $this->allowedSorts - ->first(function (AllowedSort $sort) use ($property) { - return $sort->isSort($property); - }); + ->first(fn(AllowedSort $sort) => $sort->isSort($property)); } protected function ensureAllSortsExist(): void @@ -99,13 +85,9 @@ protected function ensureAllSortsExist(): void return; } - $requestedSortNames = $this->request->sorts()->map(function (string $sort) { - return ltrim($sort, '-'); - }); + $requestedSortNames = $this->request->sorts()->map(fn(string $sort) => ltrim($sort, '-')); - $allowedSortNames = $this->allowedSorts->map(function (AllowedSort $sort) { - return $sort->getName(); - }); + $allowedSortNames = $this->allowedSorts->map(fn(AllowedSort $sort) => $sort->getName()); $unknownSorts = $requestedSortNames->diff($allowedSortNames); diff --git a/src/Filters/FiltersExact.php b/src/Filters/FiltersExact.php index f1e7f234..0b05f09e 100644 --- a/src/Filters/FiltersExact.php +++ b/src/Filters/FiltersExact.php @@ -61,12 +61,10 @@ protected function isRelationProperty(Builder $query, string $property): bool protected function withRelationConstraint(Builder $query, mixed $value, string $property): void { [$relation, $property] = collect(explode('.', $property)) - ->pipe(function (Collection $parts) { - return [ - $parts->except(count($parts) - 1)->implode('.'), - $parts->last(), - ]; - }); + ->pipe(fn(Collection $parts) => [ + $parts->except(count($parts) - 1)->implode('.'), + $parts->last(), + ]); $query->whereHas($relation, function (Builder $query) use ($value, $property) { $this->relationConstraints[] = $property = $query->qualifyColumn($property); diff --git a/src/Filters/FiltersPartial.php b/src/Filters/FiltersPartial.php index d3baaf5f..42264e1e 100644 --- a/src/Filters/FiltersPartial.php +++ b/src/Filters/FiltersPartial.php @@ -25,12 +25,12 @@ public function __invoke(Builder $query, $value, string $property) $databaseDriver = $this->getDatabaseDriver($query); if (is_array($value)) { - if (count(array_filter($value, 'strlen')) === 0) { + if (count(array_filter($value, fn($item) => empty($item))) === 0) { return $query; } $query->where(function (Builder $query) use ($databaseDriver, $value, $wrappedProperty) { - foreach (array_filter($value, 'strlen') as $partialValue) { + foreach (array_filter($value, fn($item) => empty($item)) as $partialValue) { [$sql, $bindings] = $this->getWhereRawParameters($partialValue, $wrappedProperty, $databaseDriver); $query->orWhereRaw($sql, $bindings); } @@ -45,7 +45,7 @@ public function __invoke(Builder $query, $value, string $property) protected function getDatabaseDriver(Builder $query): string { - return $query->getConnection()->getDriverName(); + return $query->getConnection()->getDriverName(); /** @phpstan-ignore-line */ } diff --git a/src/Filters/FiltersScope.php b/src/Filters/FiltersScope.php index c2793dcc..eca6829a 100644 --- a/src/Filters/FiltersScope.php +++ b/src/Filters/FiltersScope.php @@ -20,7 +20,7 @@ class FiltersScope implements Filter { /** {@inheritdoc} */ - public function __invoke(Builder $query, $values, string $property): Builder + public function __invoke(Builder $query, mixed $values, string $property): Builder { $propertyParts = collect(explode('.', $property)); @@ -77,10 +77,6 @@ protected function resolveParameters(Builder $query, $values, string $scope): ar protected function getClass(ReflectionParameter $parameter): ?ReflectionClass { - if (version_compare(PHP_VERSION, '8.0', '<')) { - return $parameter->getClass(); - } - $type = $parameter->getType(); if (is_null($type)) {