Skip to content

Commit

Permalink
Merge pull request #234 from xHeaven/patch-1
Browse files Browse the repository at this point in the history
Codebase health checkup
  • Loading branch information
iamgergo authored Feb 1, 2025
2 parents 04cb83a + e550867 commit da81c80
Show file tree
Hide file tree
Showing 55 changed files with 268 additions and 450 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/back-end.yml
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ jobs:
-
name: "Tests have failed: upload logs"
if: "${{ failure() }}"
uses: "actions/upload-artifact@v3"
uses: "actions/upload-artifact@v4"
with:
path: "storage/logs/"
name: "laravel-logs-${{ matrix.php-version }}-${{ matrix.dependencies }}"
Expand Down
12 changes: 4 additions & 8 deletions src/Actions/Action.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ abstract class Action implements Arrayable, Form, JsonSerializable
protected string $template = 'root::actions.action';

/**
* Indicates if the action is descrtuctive.
* Indicates if the action is destructive.
*/
protected bool $destructive = false;

Expand Down Expand Up @@ -134,14 +134,10 @@ protected function resolveField(Request $request, Field $field): void
{
$field->setAttribute('form', $this->getKey());
$field->id($this->getKey().'-'.$field->getAttribute('id'));
$field->resolveErrorsUsing(function (Request $request): MessageBag {
return $this->errors($request);
});
$field->resolveErrorsUsing(fn (Request $request): MessageBag => $this->errors($request));

Check failure on line 137 in src/Actions/Action.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Anonymous function should return Illuminate\Support\MessageBag but returns Illuminate\Contracts\Support\MessageBag.

if ($field instanceof Relation) {
$field->resolveRouteKeyNameUsing(function () use ($field): string {
return Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value();
});
$field->resolveRouteKeyNameUsing(fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value());
}
}

Expand Down Expand Up @@ -282,7 +278,7 @@ public function routes(Router $router): void
/**
* Convert the element to a JSON serializable format.
*/
public function jsonSerialize(): mixed
public function jsonSerialize(): string|false
{
return json_encode($this->toArray());
}
Expand Down
4 changes: 1 addition & 3 deletions src/Actions/Actions.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,7 @@ public function visible(string|array $context): static
*/
public function standalone(bool $value = true): static
{
return $this->filter(static function (Action $action) use ($value): bool {
return $value ? $action->isStandalone() : ! $action->isStandalone();
});
return $this->filter(static fn (Action $action): bool => $value ? $action->isStandalone() : ! $action->isStandalone());
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Actions/SendVerificationNotification.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@ class SendVerificationNotification extends Action
*/
public function handle(Request $request, Collection $models): void
{
$models->reject(static function (User $user): bool {
return $user->hasVerifiedEmail();
})->each(static function (User $user): void {
$models->reject(static fn (User $user): bool => $user->hasVerifiedEmail())->each(static function (User $user): void {

Check failure on line 16 in src/Actions/SendVerificationNotification.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Parameter #1 $callback of method Illuminate\Support\Collection<(int|string),Illuminate\Database\Eloquent\Model>::each() expects callable(Illuminate\Database\Eloquent\Model, int|string): mixed, Closure(Cone\Root\Models\User): void given.

Check failure on line 16 in src/Actions/SendVerificationNotification.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Parameter #1 $callback of method Illuminate\Support\Collection<(int|string),Illuminate\Database\Eloquent\Model>::reject() expects bool|(callable(Illuminate\Database\Eloquent\Model, int|string): bool)|Illuminate\Database\Eloquent\Model, Closure(Cone\Root\Models\User): bool given.
$user->sendEmailVerificationNotification();
});
}
Expand Down
2 changes: 1 addition & 1 deletion src/Casts/MetaValue.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes):
*
* @param array<string, mixed> $attributes
*/
public function set(Model $model, string $key, mixed $value, array $attributes): mixed
public function set(Model $model, string $key, mixed $value, array $attributes): string|false|null
{
return match (true) {
is_null($value) => null,
Expand Down
2 changes: 1 addition & 1 deletion src/Console/Commands/WidgetMake.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ protected function getView(): string

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

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

/**
Expand Down
46 changes: 14 additions & 32 deletions src/Conversion/Image.php
Original file line number Diff line number Diff line change
Expand Up @@ -162,45 +162,27 @@ public function resize(?int $width = null, ?int $height = null, bool $crop = fal
*/
public function save(): void
{
switch ($this->type) {
case IMAGETYPE_GIF:
imagegif($this->resource, $this->path);
break;
case IMAGETYPE_JPEG:
imagejpeg($this->resource, $this->path, $this->attributes['quality']);
break;
case IMAGETYPE_PNG:
imagepng($this->resource, $this->path, 1);
break;
case IMAGETYPE_WEBP:
imagewebp($this->resource, $this->path, $this->attributes['quality']);
break;
default:
throw new Exception("The file type [{$this->type}] is not supported.");
}
match ($this->type) {
IMAGETYPE_GIF => imagegif($this->resource, $this->path),
IMAGETYPE_JPEG => imagejpeg($this->resource, $this->path, $this->attributes['quality']),
IMAGETYPE_PNG => imagepng($this->resource, $this->path, 1),
IMAGETYPE_WEBP => imagewebp($this->resource, $this->path, $this->attributes['quality']),
default => throw new Exception("The file type [{$this->type}] is not supported."),
};
}

/**
* Create the resource.
*/
protected function create(): void
{
switch ($this->type) {
case IMAGETYPE_GIF:
$this->resource = imagecreatefromgif($this->medium->getAbsolutePath());
break;
case IMAGETYPE_JPEG:
$this->resource = imagecreatefromjpeg($this->medium->getAbsolutePath());
break;
case IMAGETYPE_PNG:
$this->resource = imagecreatefrompng($this->medium->getAbsolutePath());
break;
case IMAGETYPE_WEBP:
$this->resource = imagecreatefromwebp($this->medium->getAbsolutePath());
break;
default:
throw new Exception("The file type [{$this->type}] is not supported.");
}
$this->resource = match ($this->type) {
IMAGETYPE_GIF => imagecreatefromgif($this->medium->getAbsolutePath()),
IMAGETYPE_JPEG => imagecreatefromjpeg($this->medium->getAbsolutePath()),
IMAGETYPE_PNG => imagecreatefrompng($this->medium->getAbsolutePath()),
IMAGETYPE_WEBP => imagecreatefromwebp($this->medium->getAbsolutePath()),
default => throw new Exception("The file type [{$this->type}] is not supported."),
};
}

/**
Expand Down
53 changes: 21 additions & 32 deletions src/Fields/BelongsToMany.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,29 +67,22 @@ public function getRelation(Model $model): EloquentRelation
public function fields(Request $request): array
{
return [
BelongsTo::make($this->getRelatedName(), 'related', static function (Pivot $model): BelongsToRelation {
return $model->belongsTo(
get_class($model->getRelation('related')),
$model->getRelatedKey(),
$model->getForeignKey(),
'related'
)->withDefault();
})->withRelatableQuery(function (Request $request, Builder $query, Pivot $model): Builder {
return $this->resolveRelatableQuery($request, $model->pivotParent)
->unless($this->allowDuplicateRelations, function (Builder $query) use ($model): Builder {
return $query->whereNotIn(
$query->getModel()->getQualifiedKeyName(),
$this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName())
BelongsTo::make($this->getRelatedName(), 'related', static fn (Pivot $model): BelongsToRelation => $model->belongsTo(
$model->getRelation('related')::class,
$model->getRelatedKey(),
$model->getForeignKey(),
'related'
)->withDefault())
->withRelatableQuery(fn (Request $request, Builder $query, Pivot $model): Builder => $this->resolveRelatableQuery($request, $model->pivotParent)
->unless($this->allowDuplicateRelations, fn (Builder $query): Builder => $query->whereNotIn(
$query->getModel()->getQualifiedKeyName(),
$this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName())
)))->hydrate(function (Request $request, Pivot $model, mixed $value): void {
$model->setAttribute(
$this->getRelation($model->pivotParent)->getRelatedPivotKeyName(),
$value
);
});
})->hydrate(function (Request $request, Pivot $model, mixed $value): void {
$model->setAttribute(
$this->getRelation($model->pivotParent)->getRelatedPivotKeyName(),
$value
);
})->display(function (Model $model): ?string {
return $this->resolveDisplay($model);
}),
})->display(fn (Model $model): ?string => $this->resolveDisplay($model)),
];
}

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

if ($field instanceof Relation) {
$field->resolveRouteKeyNameUsing(function () use ($field): string {
return Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value();
});
$field->resolveRouteKeyNameUsing(
fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value()
);
}

parent::resolveField($request, $field);
Expand Down Expand Up @@ -146,9 +139,7 @@ public function withPivotFields(Closure $callback): static
$field->setModelAttribute($attribute)
->name($attribute)
->id($attribute)
->value(function () use ($model, $related, $key): mixed {
return $related->getRelation($this->getRelation($model)->getPivotAccessor())->getAttribute($key);
});
->value(fn (): mixed => $related->getRelation($this->getRelation($model)->getPivotAccessor())->getAttribute($key));
});

return $fields;
Expand All @@ -170,13 +161,11 @@ public function getValueForHydrate(Request $request): mixed
/**
* Merge the pivot values.
*/
public function mergePivotValues(array $value): mixed
public function mergePivotValues(array $value): array
{
$value = array_is_list($value) ? array_fill_keys($value, []) : $value;

return array_map(function (array $pivot): array {
return array_merge($this->pivotValues, $pivot);
}, $value);
return array_map(fn (array $pivot): array => array_merge($this->pivotValues, $pivot), $value);
}

/**
Expand Down
14 changes: 6 additions & 8 deletions src/Fields/Boolean.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public function __construct(string $label, Closure|string|null $modelAttribute =
/**
* {@inheritdoc}
*/
public function getValueForHydrate(Request $request): mixed
public function getValueForHydrate(Request $request): bool
{
return $request->boolean($this->getRequestKey());
}
Expand Down Expand Up @@ -60,13 +60,11 @@ public function resolveValue(Request $request, Model $model): bool
public function resolveFormat(Request $request, Model $model): ?string
{
if (is_null($this->formatResolver)) {
$this->formatResolver = static function (Request $request, Model $model, ?bool $value): string {
return sprintf(
'<span class="status %s">%s</span>',
$value ? 'status--success' : 'status--danger',
$value ? __('Yes') : __('No')
);
};
$this->formatResolver = static fn (Request $request, Model $model, ?bool $value): string => sprintf(
'<span class="status %s">%s</span>',
$value ? 'status--success' : 'status--danger',
$value ? __('Yes') : __('No')
);
}

return parent::resolveFormat($request, $model);
Expand Down
4 changes: 1 addition & 3 deletions src/Fields/Date.php
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,7 @@ public function getValue(Model $model): mixed
public function resolveFormat(Request $request, Model $model): ?string
{
if (is_null($this->formatResolver)) {
$this->formatResolver = function (Request $request, Model $model, mixed $value): ?string {
return is_null($value) ? $value : $value->format($this->format);
};
$this->formatResolver = fn (Request $request, Model $model, mixed $value): ?string => is_null($value) ? $value : $value->format($this->format);
}

return parent::resolveFormat($request, $model);
Expand Down
16 changes: 6 additions & 10 deletions src/Fields/Dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,18 +33,14 @@ public function toInput(Request $request, Model $model): array
$data = parent::toInput($request, $model);

return array_merge($data, [
'options' => array_map(static function (array $option): array {
return array_merge($option, [
'html' => View::make('root::fields.dropdown-option', $option)->render(),
]);
}, $data['options']),
'options' => array_map(static fn (array $option): array => array_merge($option, [
'html' => View::make('root::fields.dropdown-option', $option)->render(),
]), $data['options']),
'selection' => Collection::make($data['options'])
->filter(fn (array $option): bool => $option['selected'] ?? false)
->map(static function (array $option): array {
return array_merge($option, [
'html' => View::make('root::fields.dropdown-option', $option)->render(),
]);
})
->map(static fn (array $option): array => array_merge($option, [
'html' => View::make('root::fields.dropdown-option', $option)->render(),
]))
->values()
->all(),
'config' => [
Expand Down
28 changes: 13 additions & 15 deletions src/Fields/Editor.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,19 @@ protected function newMediaField(): Media
{
public function __construct(string $modelAttribute)
{
parent::__construct(__('Media'), $modelAttribute.'-media', static function (): MorphToMany {
return new MorphToMany(
Medium::proxy()->newQuery(),
new class extends Model
{
//
},
'media',
'root_mediables',
'medium_id',
'_model_id',
'id',
'id'
);
});
parent::__construct(__('Media'), $modelAttribute.'-media', static fn (): MorphToMany => new MorphToMany(
Medium::proxy()->newQuery(),
new class extends Model
{
//
},
'media',
'root_mediables',
'medium_id',
'_model_id',
'id',
'id'
));

$this->template = 'root::fields.editor.media';

Expand Down
12 changes: 5 additions & 7 deletions src/Fields/Field.php
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ public function isSortable(): bool
}

/**
* Set the searachable attribute.
* Set the searchable attribute.
*/
public function searchable(bool|Closure $value = true): static
{
Expand Down Expand Up @@ -441,7 +441,7 @@ public function withOldValue(bool $value = true): static
/**
* Set the with old value attribute to false.
*/
public function withoutOldValue(): mixed
public function withoutOldValue(): static
{
return $this->withOldValue(false);
}
Expand Down Expand Up @@ -588,7 +588,7 @@ public function resolveErrors(Request $request): MessageBag
*/
public function invalid(Request $request): bool
{
return $this->resolveErrors($request)->has($this->getValidationKey()) ?: false;
return $this->resolveErrors($request)->has($this->getValidationKey());
}

/**
Expand All @@ -602,7 +602,7 @@ public function error(Request $request): ?string
/**
* Convert the element to a JSON serializable format.
*/
public function jsonSerialize(): mixed
public function jsonSerialize(): array
{
return $this->toArray();
}
Expand Down Expand Up @@ -661,9 +661,7 @@ public function toValidate(Request $request, Model $model): array
$key = $model->exists ? 'update' : 'create';

$rules = array_map(
static function (array|Closure $rule) use ($request, $model): array {
return is_array($rule) ? $rule : call_user_func_array($rule, [$request, $model]);
},
static fn (array|Closure $rule): array => is_array($rule) ? $rule : call_user_func_array($rule, [$request, $model]),
Arr::only($this->rules, array_unique(['*', $key]))
);

Expand Down
Loading

0 comments on commit da81c80

Please sign in to comment.