Skip to content

Commit d96c963

Browse files
authored
General code health improvements (#988)
* Remove unused parameter * Use null-safe operator instead of optional() helper Also the model is not nullable at this point, so we can safely remove the null-safe operator below the if condition. * Mark nullable parameters explicitly * Sort use() variables * Use direct empty string check to reduce cognitive load * Remove unnecessary else branch
1 parent 1818565 commit d96c963

File tree

5 files changed

+24
-24
lines changed

5 files changed

+24
-24
lines changed

src/AllowedFilter.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,56 +48,56 @@ public function filter(QueryBuilder $query, $value): void
4848
($this->filterClass)($query->getEloquentBuilder(), $valueToFilter, $this->internalName);
4949
}
5050

51-
public static function setFilterArrayValueDelimiter(string $delimiter = null): void
51+
public static function setFilterArrayValueDelimiter(?string $delimiter = null): void
5252
{
5353
if (isset($delimiter)) {
5454
QueryBuilderRequest::setFilterArrayValueDelimiter($delimiter);
5555
}
5656
}
5757

58-
public static function exact(string $name, ?string $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): static
58+
public static function exact(string $name, ?string $internalName = null, bool $addRelationConstraint = true, ?string $arrayValueDelimiter = null): static
5959
{
6060
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
6161

6262
return new static($name, new FiltersExact($addRelationConstraint), $internalName);
6363
}
6464

65-
public static function partial(string $name, $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): static
65+
public static function partial(string $name, $internalName = null, bool $addRelationConstraint = true, ?string $arrayValueDelimiter = null): static
6666
{
6767
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
6868

6969
return new static($name, new FiltersPartial($addRelationConstraint), $internalName);
7070
}
7171

72-
public static function beginsWithStrict(string $name, $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): static
72+
public static function beginsWithStrict(string $name, $internalName = null, bool $addRelationConstraint = true, ?string $arrayValueDelimiter = null): static
7373
{
7474
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
7575

7676
return new static($name, new FiltersBeginsWithStrict($addRelationConstraint), $internalName);
7777
}
7878

79-
public static function endsWithStrict(string $name, $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): static
79+
public static function endsWithStrict(string $name, $internalName = null, bool $addRelationConstraint = true, ?string $arrayValueDelimiter = null): static
8080
{
8181
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
8282

8383
return new static($name, new FiltersEndsWithStrict($addRelationConstraint), $internalName);
8484
}
8585

86-
public static function belongsTo(string $name, $internalName = null, string $arrayValueDelimiter = null): static
86+
public static function belongsTo(string $name, $internalName = null, ?string $arrayValueDelimiter = null): static
8787
{
8888
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
8989

9090
return new static($name, new FiltersBelongsTo(), $internalName);
9191
}
9292

93-
public static function scope(string $name, $internalName = null, string $arrayValueDelimiter = null): static
93+
public static function scope(string $name, $internalName = null, ?string $arrayValueDelimiter = null): static
9494
{
9595
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
9696

9797
return new static($name, new FiltersScope(), $internalName);
9898
}
9999

100-
public static function callback(string $name, $callback, $internalName = null, string $arrayValueDelimiter = null): static
100+
public static function callback(string $name, $callback, $internalName = null, ?string $arrayValueDelimiter = null): static
101101
{
102102
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
103103

@@ -109,18 +109,18 @@ public static function trashed(string $name = 'trashed', $internalName = null):
109109
return new static($name, new FiltersTrashed(), $internalName);
110110
}
111111

112-
public static function custom(string $name, Filter $filterClass, $internalName = null, string $arrayValueDelimiter = null): static
112+
public static function custom(string $name, Filter $filterClass, $internalName = null, ?string $arrayValueDelimiter = null): static
113113
{
114114
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
115115

116116
return new static($name, $filterClass, $internalName);
117117
}
118118

119-
public static function operator(string $name, FilterOperator $filterOperator, string $boolean = 'and', ?string $internalName = null, bool $addRelationConstraint = true, string $arrayValueDelimiter = null): self
119+
public static function operator(string $name, FilterOperator $filterOperator, string $boolean = 'and', ?string $internalName = null, bool $addRelationConstraint = true, ?string $arrayValueDelimiter = null): self
120120
{
121121
static::setFilterArrayValueDelimiter($arrayValueDelimiter);
122122

123-
return new static($name, new FiltersOperator($addRelationConstraint, $filterOperator, $boolean), $internalName, $filterOperator);
123+
return new static($name, new FiltersOperator($addRelationConstraint, $filterOperator, $boolean), $internalName);
124124
}
125125

126126
public function getFilterClass(): Filter

src/Filters/FiltersBelongsTo.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -70,14 +70,14 @@ protected function getModelFromRelation(Model $model, string $relation, int $lev
7070
$relationParts = explode('.', $relation);
7171
if (count($relationParts) == 1) {
7272
return $this->getRelatedModelFromRelation($model, $relation);
73-
} else {
74-
$firstRelation = $relationParts[0];
75-
$firstRelatedModel = $this->getRelatedModelFromRelation($model, $firstRelation);
76-
if (! $firstRelatedModel) {
77-
return null;
78-
}
73+
}
7974

80-
return $this->getModelFromRelation($firstRelatedModel, implode('.', array_slice($relationParts, 1)), $level + 1);
75+
$firstRelation = $relationParts[0];
76+
$firstRelatedModel = $this->getRelatedModelFromRelation($model, $firstRelation);
77+
if (! $firstRelatedModel) {
78+
return null;
8179
}
80+
81+
return $this->getModelFromRelation($firstRelatedModel, implode('.', array_slice($relationParts, 1)), $level + 1);
8282
}
8383
}

src/Filters/FiltersExact.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ protected function withRelationConstraint(Builder $query, mixed $value, string $
6666
$parts->last(),
6767
]);
6868

69-
$query->whereHas($relation, function (Builder $query) use ($value, $property) {
69+
$query->whereHas($relation, function (Builder $query) use ($property, $value) {
7070
$this->relationConstraints[] = $property = $query->qualifyColumn($property);
7171

7272
$this->__invoke($query, $value, $property);

src/Filters/FiltersPartial.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ public function __invoke(Builder $query, $value, string $property)
2525
$databaseDriver = $this->getDatabaseDriver($query);
2626

2727
if (is_array($value)) {
28-
if (count(array_filter($value, fn ($item) => strlen($item) > 0)) === 0) {
28+
if (count(array_filter($value, fn ($item) => $item != '')) === 0) {
2929
return $query;
3030
}
3131

32-
$query->where(function (Builder $query) use ($databaseDriver, $value, $wrappedProperty) {
33-
foreach (array_filter($value, fn ($item) => strlen($item) > 0) as $partialValue) {
32+
$query->where(function (Builder $query) use ($value, $wrappedProperty, $databaseDriver) {
33+
foreach (array_filter($value, fn ($item) => $item != '') as $partialValue) {
3434
[$sql, $bindings] = $this->getWhereRawParameters($partialValue, $wrappedProperty, $databaseDriver);
3535
$query->orWhereRaw($sql, $bindings);
3636
}

src/Filters/FiltersScope.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,12 +54,12 @@ protected function resolveParameters(Builder $query, $values, string $scope): ar
5454
}
5555

5656
foreach ($parameters as $parameter) {
57-
if (! optional($this->getClass($parameter))->isSubclassOf(Model::class)) {
57+
if (! $this->getClass($parameter)?->isSubclassOf(Model::class)) {
5858
continue;
5959
}
6060

6161
/** @var TModelClass $model */
62-
$model = $this->getClass($parameter)?->newInstance();
62+
$model = $this->getClass($parameter)->newInstance();
6363
$index = $parameter->getPosition() - 1;
6464
$value = $values[$index];
6565

0 commit comments

Comments
 (0)