Skip to content

Commit da81c80

Browse files
authored
Merge pull request #234 from xHeaven/patch-1
Codebase health checkup
2 parents 04cb83a + e550867 commit da81c80

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+268
-450
lines changed

.github/workflows/back-end.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ jobs:
8181
-
8282
name: "Tests have failed: upload logs"
8383
if: "${{ failure() }}"
84-
uses: "actions/upload-artifact@v3"
84+
uses: "actions/upload-artifact@v4"
8585
with:
8686
path: "storage/logs/"
8787
name: "laravel-logs-${{ matrix.php-version }}-${{ matrix.dependencies }}"

src/Actions/Action.php

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ abstract class Action implements Arrayable, Form, JsonSerializable
4949
protected string $template = 'root::actions.action';
5050

5151
/**
52-
* Indicates if the action is descrtuctive.
52+
* Indicates if the action is destructive.
5353
*/
5454
protected bool $destructive = false;
5555

@@ -134,14 +134,10 @@ protected function resolveField(Request $request, Field $field): void
134134
{
135135
$field->setAttribute('form', $this->getKey());
136136
$field->id($this->getKey().'-'.$field->getAttribute('id'));
137-
$field->resolveErrorsUsing(function (Request $request): MessageBag {
138-
return $this->errors($request);
139-
});
137+
$field->resolveErrorsUsing(fn (Request $request): MessageBag => $this->errors($request));
140138

141139
if ($field instanceof Relation) {
142-
$field->resolveRouteKeyNameUsing(function () use ($field): string {
143-
return Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value();
144-
});
140+
$field->resolveRouteKeyNameUsing(fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value());
145141
}
146142
}
147143

@@ -282,7 +278,7 @@ public function routes(Router $router): void
282278
/**
283279
* Convert the element to a JSON serializable format.
284280
*/
285-
public function jsonSerialize(): mixed
281+
public function jsonSerialize(): string|false
286282
{
287283
return json_encode($this->toArray());
288284
}

src/Actions/Actions.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ public function visible(string|array $context): static
4444
*/
4545
public function standalone(bool $value = true): static
4646
{
47-
return $this->filter(static function (Action $action) use ($value): bool {
48-
return $value ? $action->isStandalone() : ! $action->isStandalone();
49-
});
47+
return $this->filter(static fn (Action $action): bool => $value ? $action->isStandalone() : ! $action->isStandalone());
5048
}
5149

5250
/**

src/Actions/SendVerificationNotification.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,7 @@ class SendVerificationNotification extends Action
1313
*/
1414
public function handle(Request $request, Collection $models): void
1515
{
16-
$models->reject(static function (User $user): bool {
17-
return $user->hasVerifiedEmail();
18-
})->each(static function (User $user): void {
16+
$models->reject(static fn (User $user): bool => $user->hasVerifiedEmail())->each(static function (User $user): void {
1917
$user->sendEmailVerificationNotification();
2018
});
2119
}

src/Casts/MetaValue.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
2323
*
2424
* @param array<string, mixed> $attributes
2525
*/
26-
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
26+
public function set(Model $model, string $key, mixed $value, array $attributes): string|false|null
2727
{
2828
return match (true) {
2929
is_null($value) => null,

src/Console/Commands/WidgetMake.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ protected function getView(): string
102102

103103
$name = str_replace('\\', '/', $this->getNameInput());
104104

105-
return 'widgets.'.implode('.', array_map([Str::class, 'kebab'], explode('/', $name)));
105+
return 'widgets.'.implode('.', array_map(Str::kebab(...), explode('/', $name)));
106106
}
107107

108108
/**

src/Conversion/Image.php

Lines changed: 14 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -162,45 +162,27 @@ public function resize(?int $width = null, ?int $height = null, bool $crop = fal
162162
*/
163163
public function save(): void
164164
{
165-
switch ($this->type) {
166-
case IMAGETYPE_GIF:
167-
imagegif($this->resource, $this->path);
168-
break;
169-
case IMAGETYPE_JPEG:
170-
imagejpeg($this->resource, $this->path, $this->attributes['quality']);
171-
break;
172-
case IMAGETYPE_PNG:
173-
imagepng($this->resource, $this->path, 1);
174-
break;
175-
case IMAGETYPE_WEBP:
176-
imagewebp($this->resource, $this->path, $this->attributes['quality']);
177-
break;
178-
default:
179-
throw new Exception("The file type [{$this->type}] is not supported.");
180-
}
165+
match ($this->type) {
166+
IMAGETYPE_GIF => imagegif($this->resource, $this->path),
167+
IMAGETYPE_JPEG => imagejpeg($this->resource, $this->path, $this->attributes['quality']),
168+
IMAGETYPE_PNG => imagepng($this->resource, $this->path, 1),
169+
IMAGETYPE_WEBP => imagewebp($this->resource, $this->path, $this->attributes['quality']),
170+
default => throw new Exception("The file type [{$this->type}] is not supported."),
171+
};
181172
}
182173

183174
/**
184175
* Create the resource.
185176
*/
186177
protected function create(): void
187178
{
188-
switch ($this->type) {
189-
case IMAGETYPE_GIF:
190-
$this->resource = imagecreatefromgif($this->medium->getAbsolutePath());
191-
break;
192-
case IMAGETYPE_JPEG:
193-
$this->resource = imagecreatefromjpeg($this->medium->getAbsolutePath());
194-
break;
195-
case IMAGETYPE_PNG:
196-
$this->resource = imagecreatefrompng($this->medium->getAbsolutePath());
197-
break;
198-
case IMAGETYPE_WEBP:
199-
$this->resource = imagecreatefromwebp($this->medium->getAbsolutePath());
200-
break;
201-
default:
202-
throw new Exception("The file type [{$this->type}] is not supported.");
203-
}
179+
$this->resource = match ($this->type) {
180+
IMAGETYPE_GIF => imagecreatefromgif($this->medium->getAbsolutePath()),
181+
IMAGETYPE_JPEG => imagecreatefromjpeg($this->medium->getAbsolutePath()),
182+
IMAGETYPE_PNG => imagecreatefrompng($this->medium->getAbsolutePath()),
183+
IMAGETYPE_WEBP => imagecreatefromwebp($this->medium->getAbsolutePath()),
184+
default => throw new Exception("The file type [{$this->type}] is not supported."),
185+
};
204186
}
205187

206188
/**

src/Fields/BelongsToMany.php

Lines changed: 21 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -67,29 +67,22 @@ public function getRelation(Model $model): EloquentRelation
6767
public function fields(Request $request): array
6868
{
6969
return [
70-
BelongsTo::make($this->getRelatedName(), 'related', static function (Pivot $model): BelongsToRelation {
71-
return $model->belongsTo(
72-
get_class($model->getRelation('related')),
73-
$model->getRelatedKey(),
74-
$model->getForeignKey(),
75-
'related'
76-
)->withDefault();
77-
})->withRelatableQuery(function (Request $request, Builder $query, Pivot $model): Builder {
78-
return $this->resolveRelatableQuery($request, $model->pivotParent)
79-
->unless($this->allowDuplicateRelations, function (Builder $query) use ($model): Builder {
80-
return $query->whereNotIn(
81-
$query->getModel()->getQualifiedKeyName(),
82-
$this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName())
70+
BelongsTo::make($this->getRelatedName(), 'related', static fn (Pivot $model): BelongsToRelation => $model->belongsTo(
71+
$model->getRelation('related')::class,
72+
$model->getRelatedKey(),
73+
$model->getForeignKey(),
74+
'related'
75+
)->withDefault())
76+
->withRelatableQuery(fn (Request $request, Builder $query, Pivot $model): Builder => $this->resolveRelatableQuery($request, $model->pivotParent)
77+
->unless($this->allowDuplicateRelations, fn (Builder $query): Builder => $query->whereNotIn(
78+
$query->getModel()->getQualifiedKeyName(),
79+
$this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName())
80+
)))->hydrate(function (Request $request, Pivot $model, mixed $value): void {
81+
$model->setAttribute(
82+
$this->getRelation($model->pivotParent)->getRelatedPivotKeyName(),
83+
$value
8384
);
84-
});
85-
})->hydrate(function (Request $request, Pivot $model, mixed $value): void {
86-
$model->setAttribute(
87-
$this->getRelation($model->pivotParent)->getRelatedPivotKeyName(),
88-
$value
89-
);
90-
})->display(function (Model $model): ?string {
91-
return $this->resolveDisplay($model);
92-
}),
85+
})->display(fn (Model $model): ?string => $this->resolveDisplay($model)),
9386
];
9487
}
9588

@@ -115,9 +108,9 @@ protected function resolveField(Request $request, Field $field): void
115108
}
116109

117110
if ($field instanceof Relation) {
118-
$field->resolveRouteKeyNameUsing(function () use ($field): string {
119-
return Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value();
120-
});
111+
$field->resolveRouteKeyNameUsing(
112+
fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value()
113+
);
121114
}
122115

123116
parent::resolveField($request, $field);
@@ -146,9 +139,7 @@ public function withPivotFields(Closure $callback): static
146139
$field->setModelAttribute($attribute)
147140
->name($attribute)
148141
->id($attribute)
149-
->value(function () use ($model, $related, $key): mixed {
150-
return $related->getRelation($this->getRelation($model)->getPivotAccessor())->getAttribute($key);
151-
});
142+
->value(fn (): mixed => $related->getRelation($this->getRelation($model)->getPivotAccessor())->getAttribute($key));
152143
});
153144

154145
return $fields;
@@ -170,13 +161,11 @@ public function getValueForHydrate(Request $request): mixed
170161
/**
171162
* Merge the pivot values.
172163
*/
173-
public function mergePivotValues(array $value): mixed
164+
public function mergePivotValues(array $value): array
174165
{
175166
$value = array_is_list($value) ? array_fill_keys($value, []) : $value;
176167

177-
return array_map(function (array $pivot): array {
178-
return array_merge($this->pivotValues, $pivot);
179-
}, $value);
168+
return array_map(fn (array $pivot): array => array_merge($this->pivotValues, $pivot), $value);
180169
}
181170

182171
/**

src/Fields/Boolean.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
2727
/**
2828
* {@inheritdoc}
2929
*/
30-
public function getValueForHydrate(Request $request): mixed
30+
public function getValueForHydrate(Request $request): bool
3131
{
3232
return $request->boolean($this->getRequestKey());
3333
}
@@ -60,13 +60,11 @@ public function resolveValue(Request $request, Model $model): bool
6060
public function resolveFormat(Request $request, Model $model): ?string
6161
{
6262
if (is_null($this->formatResolver)) {
63-
$this->formatResolver = static function (Request $request, Model $model, ?bool $value): string {
64-
return sprintf(
65-
'<span class="status %s">%s</span>',
66-
$value ? 'status--success' : 'status--danger',
67-
$value ? __('Yes') : __('No')
68-
);
69-
};
63+
$this->formatResolver = static fn (Request $request, Model $model, ?bool $value): string => sprintf(
64+
'<span class="status %s">%s</span>',
65+
$value ? 'status--success' : 'status--danger',
66+
$value ? __('Yes') : __('No')
67+
);
7068
}
7169

7270
return parent::resolveFormat($request, $model);

src/Fields/Date.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,7 @@ public function getValue(Model $model): mixed
132132
public function resolveFormat(Request $request, Model $model): ?string
133133
{
134134
if (is_null($this->formatResolver)) {
135-
$this->formatResolver = function (Request $request, Model $model, mixed $value): ?string {
136-
return is_null($value) ? $value : $value->format($this->format);
137-
};
135+
$this->formatResolver = fn (Request $request, Model $model, mixed $value): ?string => is_null($value) ? $value : $value->format($this->format);
138136
}
139137

140138
return parent::resolveFormat($request, $model);

0 commit comments

Comments
 (0)