Skip to content

Commit

Permalink
Improve phpstan
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexVanderbist committed May 10, 2024
1 parent 64a94ff commit 9e68f80
Show file tree
Hide file tree
Showing 7 changed files with 21 additions and 56 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/run-tests.yml
Expand Up @@ -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 }}

Expand Down
5 changes: 0 additions & 5 deletions phpstan-baseline.neon
Expand Up @@ -29,8 +29,3 @@ parameters:
message: "#^Call to an undefined method Illuminate\\\\Database\\\\Eloquent\\\\Builder\\<TModelClass of Illuminate\\\\Database\\\\Eloquent\\\\Model\\>\\:\\: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
17 changes: 7 additions & 10 deletions src/Concerns/AddsIncludesToQuery.php
Expand Up @@ -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
Expand All @@ -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);

Expand All @@ -90,14 +86,15 @@ protected function ensureAllIncludesExist(): void
// TODO: Check for non-existing relationships?
}

/**
* @param Collection<null|AllowedInclude> $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)));
}
}
30 changes: 6 additions & 24 deletions src/Concerns/SortsQuery.php
Expand Up @@ -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.
Expand All @@ -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;
}
Expand All @@ -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
Expand All @@ -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);

Expand Down
10 changes: 4 additions & 6 deletions src/Filters/FiltersExact.php
Expand Up @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions src/Filters/FiltersPartial.php
Expand Up @@ -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);
}
Expand All @@ -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 */
}


Expand Down
6 changes: 1 addition & 5 deletions src/Filters/FiltersScope.php
Expand Up @@ -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));

Expand Down Expand Up @@ -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)) {
Expand Down

0 comments on commit 9e68f80

Please sign in to comment.