Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Codebase health checkup #234

Merged
merged 20 commits into from
Feb 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@
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 @@
{
$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 @@ -216,7 +212,7 @@

if (in_array(HasRootEvents::class, class_uses_recursive($model))) {
$models->each(static function (Model $model) use ($request): void {
$model->recordRootEvent(

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

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Call to an undefined method Illuminate\Database\Eloquent\Model::recordRootEvent().
Str::of(static::class)->classBasename()->headline()->value(),
$request->user()
);
Expand Down Expand Up @@ -282,7 +278,7 @@
/**
* 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 @@ -28,7 +28,7 @@
*/
public function authorized(Request $request, ?Model $model = null): static
{
return $this->filter->authorized($request, $model)->values();

Check failure on line 31 in src/Actions/Actions.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Method Cone\Root\Actions\Actions::authorized() should return static(Cone\Root\Actions\Actions) but returns Cone\Root\Actions\Actions.
}

/**
Expand All @@ -36,7 +36,7 @@
*/
public function visible(string|array $context): static
{
return $this->filter->visible($context)->values();

Check failure on line 39 in src/Actions/Actions.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Method Cone\Root\Actions\Actions::visible() should return static(Cone\Root\Actions\Actions) but returns Cone\Root\Actions\Actions.
}

/**
Expand All @@ -44,9 +44,7 @@
*/
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 @@
*/
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 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 @@
}

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 @@
$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 @@
/**
* 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 Expand Up @@ -225,7 +214,7 @@
*/
public function resolveRouteBinding(Request $request, string $id): Model
{
$relation = $this->getRelation($request->route()->parentOfParameter($this->getRouteKeyName()));

Check failure on line 217 in src/Fields/BelongsToMany.php

View workflow job for this annotation

GitHub Actions / 3️⃣ Static Analysis

Parameter #1 $model of method Cone\Root\Fields\BelongsToMany<TRelation of Illuminate\Database\Eloquent\Relations\BelongsToMany>::getRelation() expects Illuminate\Database\Eloquent\Model, string|null given.

$related = $relation->wherePivot($relation->newPivot()->getQualifiedKeyName(), $id)->firstOrFail();

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
Loading