diff --git a/.github/workflows/back-end.yml b/.github/workflows/back-end.yml index 74185190..ad45d93b 100644 --- a/.github/workflows/back-end.yml +++ b/.github/workflows/back-end.yml @@ -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 }}" diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 8ef5de5e..b3e7e1cc 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -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; @@ -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)); 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()); } } @@ -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()); } diff --git a/src/Actions/Actions.php b/src/Actions/Actions.php index 3a5820a5..5e7fcea7 100644 --- a/src/Actions/Actions.php +++ b/src/Actions/Actions.php @@ -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()); } /** diff --git a/src/Actions/SendVerificationNotification.php b/src/Actions/SendVerificationNotification.php index 15285d0e..ed1b876b 100644 --- a/src/Actions/SendVerificationNotification.php +++ b/src/Actions/SendVerificationNotification.php @@ -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 { $user->sendEmailVerificationNotification(); }); } diff --git a/src/Casts/MetaValue.php b/src/Casts/MetaValue.php index 55b7fe25..c50b02b4 100644 --- a/src/Casts/MetaValue.php +++ b/src/Casts/MetaValue.php @@ -23,7 +23,7 @@ public function get(Model $model, string $key, mixed $value, array $attributes): * * @param array $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, diff --git a/src/Console/Commands/WidgetMake.php b/src/Console/Commands/WidgetMake.php index d5a783b8..f56446a9 100644 --- a/src/Console/Commands/WidgetMake.php +++ b/src/Console/Commands/WidgetMake.php @@ -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))); } /** diff --git a/src/Conversion/Image.php b/src/Conversion/Image.php index 27afc11c..da6aa495 100644 --- a/src/Conversion/Image.php +++ b/src/Conversion/Image.php @@ -162,22 +162,13 @@ 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."), + }; } /** @@ -185,22 +176,13 @@ public function save(): void */ 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."), + }; } /** diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index a4f254a6..784b170b 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -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)), ]; } @@ -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); @@ -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; @@ -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); } /** diff --git a/src/Fields/Boolean.php b/src/Fields/Boolean.php index 4e9ea4e5..3c70938b 100644 --- a/src/Fields/Boolean.php +++ b/src/Fields/Boolean.php @@ -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()); } @@ -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( - '%s', - $value ? 'status--success' : 'status--danger', - $value ? __('Yes') : __('No') - ); - }; + $this->formatResolver = static fn (Request $request, Model $model, ?bool $value): string => sprintf( + '%s', + $value ? 'status--success' : 'status--danger', + $value ? __('Yes') : __('No') + ); } return parent::resolveFormat($request, $model); diff --git a/src/Fields/Date.php b/src/Fields/Date.php index 1be5354e..baf66ed8 100644 --- a/src/Fields/Date.php +++ b/src/Fields/Date.php @@ -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); diff --git a/src/Fields/Dropdown.php b/src/Fields/Dropdown.php index 1e929ab8..4b014901 100644 --- a/src/Fields/Dropdown.php +++ b/src/Fields/Dropdown.php @@ -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' => [ diff --git a/src/Fields/Editor.php b/src/Fields/Editor.php index e376964a..b8d62f74 100644 --- a/src/Fields/Editor.php +++ b/src/Fields/Editor.php @@ -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'; diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 8782c4bd..447ab9af 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -311,7 +311,7 @@ public function isSortable(): bool } /** - * Set the searachable attribute. + * Set the searchable attribute. */ public function searchable(bool|Closure $value = true): static { @@ -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); } @@ -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()); } /** @@ -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(); } @@ -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])) ); diff --git a/src/Fields/Fields.php b/src/Fields/Fields.php index 6050d7ce..ecbf4c36 100644 --- a/src/Fields/Fields.php +++ b/src/Fields/Fields.php @@ -72,9 +72,7 @@ public function sortable(): static */ public function relation(): static { - return $this->filter(static function (Field $field): bool { - return $field instanceof Relation; - }); + return $this->filter(static fn (Field $field): bool => $field instanceof Relation); } /** @@ -82,9 +80,7 @@ public function relation(): static */ public function translatable(): static { - return $this->filter(static function (Field $field): bool { - return $field->isTranslatable(); - }); + return $this->filter(static fn (Field $field): bool => $field->isTranslatable()); } /** @@ -92,11 +88,9 @@ public function translatable(): static */ public function subResource(bool $value = true): static { - return $this->filter(static function (Field $field) use ($value): bool { - return $value - ? $field instanceof Relation && $field->isSubResource() - : ! $field instanceof Relation || ! $field->isSubResource(); - }); + return $this->filter(static fn (Field $field): bool => $value + ? $field instanceof Relation && $field->isSubResource() + : ! $field instanceof Relation || ! $field->isSubResource()); } /** @@ -104,9 +98,7 @@ public function subResource(bool $value = true): static */ public function mapToValidate(Request $request, Model $model): array { - return $this->reduce(static function (array $rules, Field $field) use ($request, $model): array { - return array_merge_recursive($rules, $field->toValidate($request, $model)); - }, []); + return $this->reduce(static fn (array $rules, Field $field): array => array_merge_recursive($rules, $field->toValidate($request, $model)), []); } /** diff --git a/src/Fields/Fieldset.php b/src/Fields/Fieldset.php index 3eb970a0..a7365046 100644 --- a/src/Fields/Fieldset.php +++ b/src/Fields/Fieldset.php @@ -47,9 +47,9 @@ protected function resolveField(Request $request, Field $field): void $field->resolveErrorsUsing($this->errorsResolver); if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(function () use ($field): string { - return Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getModelAttribute())->value(); - }); + $field->resolveRouteKeyNameUsing( + fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getModelAttribute())->value() + ); } } diff --git a/src/Fields/File.php b/src/Fields/File.php index 58396025..bfb51963 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -102,11 +102,9 @@ public function collection(string $value): static public function resolveDisplay(Model $related): ?string { if (is_null($this->displayResolver)) { - $this->display(function (Medium $related): string { - return $related->isImage - ? sprintf('%s', $related->getUrl($this->displayConversion), $related->name) - : $related->file_name; - }); + $this->display(fn (Medium $related): string => $related->isImage + ? sprintf('%s', $related->getUrl($this->displayConversion), $related->name) + : $related->file_name); } return parent::resolveDisplay($related); @@ -214,9 +212,7 @@ public function persist(Request $request, Model $model, mixed $value): void $model->saved(function (Model $model) use ($request, $value): void { $files = Arr::wrap($request->file($this->getRequestKey(), [])); - $ids = array_map(function (UploadedFile $file) use ($request, $model): int { - return $this->store($request, $model, $file)['value']; - }, $files); + $ids = array_map(fn (UploadedFile $file): int => $this->store($request, $model, $file)['value'], $files); $value += $this->mergePivotValues($ids); diff --git a/src/Fields/HasOneOrMany.php b/src/Fields/HasOneOrMany.php index 2036fb89..c4c11383 100644 --- a/src/Fields/HasOneOrMany.php +++ b/src/Fields/HasOneOrMany.php @@ -55,9 +55,7 @@ public function resolveHydrate(Request $request, Model $model, mixed $value): vo if (is_null($this->hydrateResolver)) { $this->hydrateResolver = function (Request $request, Model $model, mixed $value): void { $related = $this->resolveRelatableQuery($request, $model) - ->where(function (Builder $query) use ($model, $value): Builder { - return $query->whereIn($this->getRelation($model)->getRelated()->getQualifiedKeyName(), (array) $value); - }) + ->where(fn (Builder $query): Builder => $query->whereIn($this->getRelation($model)->getRelated()->getQualifiedKeyName(), (array) $value)) ->get(); $model->setRelation($this->getRelationName(), is_array($value) ? $related : $related->first()); diff --git a/src/Fields/Media.php b/src/Fields/Media.php index 2db3d8d8..e6511b19 100644 --- a/src/Fields/Media.php +++ b/src/Fields/Media.php @@ -181,22 +181,18 @@ public function toInput(Request $request, Model $model): array 'chunk_size' => Config::get('root.media.chunk_size'), 'query' => $filters->mapToData($request), ], - 'selection' => array_map(static function (array $option): array { - return array_merge($option, [ - 'html' => View::make('root::fields.file-option', $option)->render(), - ]); - }, $data['options'] ?? []), + 'selection' => array_map(static fn (array $option): array => array_merge($option, [ + 'html' => View::make('root::fields.file-option', $option)->render(), + ]), $data['options'] ?? []), 'url' => $this->modelUrl($model), 'filters' => $filters->renderable() - ->map(function (RenderableFilter $filter) use ($request, $model): array { - return $filter->toField() - ->removeAttribute('name') - ->setAttributes([ - 'x-model.debounce.300ms' => $filter->getKey(), - 'x-bind:readonly' => 'processing', - ]) - ->toInput($request, $this->getRelation($model)->make()); - }) + ->map(fn (RenderableFilter $filter): array => $filter->toField() + ->removeAttribute('name') + ->setAttributes([ + 'x-model.debounce.300ms' => $filter->getKey(), + 'x-bind:readonly' => 'processing', + ]) + ->toInput($request, $this->getRelation($model)->make())) ->all(), ]); } diff --git a/src/Fields/Meta.php b/src/Fields/Meta.php index e122b532..7bfe2e71 100644 --- a/src/Fields/Meta.php +++ b/src/Fields/Meta.php @@ -67,9 +67,7 @@ public function as(string $field, ?Closure $callback = null): static { $this->field = new $field($this->label, $this->getModelAttribute()); - $this->field->value(function (Request $request, Model $model): mixed { - return $this->resolveValue($request, $model); - }); + $this->field->value(fn (Request $request, Model $model): mixed => $this->resolveValue($request, $model)); if (! is_null($callback)) { call_user_func_array($callback, [$this->field]); @@ -115,9 +113,7 @@ public function getValue(Model $model): mixed public function resolveValue(Request $request, Model $model): mixed { if (is_null($this->valueResolver)) { - $this->valueResolver = static function (Request $request, Model $model, mixed $value): mixed { - return $value?->value; - }; + $this->valueResolver = static fn (Request $request, Model $model, mixed $value): mixed => $value?->value; } return parent::resolveValue($request, $model); diff --git a/src/Fields/MorphTo.php b/src/Fields/MorphTo.php index 6f27919e..a7ba040c 100644 --- a/src/Fields/MorphTo.php +++ b/src/Fields/MorphTo.php @@ -98,9 +98,9 @@ public function routes(Router $router): void public function toArray(): array { return array_merge(parent::toArray(), [ - 'types' => array_map(static function (string $type): string { - return __(Str::of($type)->classBasename()->headline()->value()); - }, array_combine($this->types, $this->types)), + 'types' => array_map( + static fn (string $type): string => __(Str::of($type)->classBasename()->headline()->value()), array_combine($this->types, $this->types) + ), ]); } diff --git a/src/Fields/MorphToMany.php b/src/Fields/MorphToMany.php index 525b0602..2a9671aa 100644 --- a/src/Fields/MorphToMany.php +++ b/src/Fields/MorphToMany.php @@ -28,29 +28,24 @@ public function getRelation(Model $model): EloquentRelation public function fields(Request $request): array { return [ - MorphTo::make($this->getRelatedName(), 'related', static function (MorphPivot $model): MorphToRelation { - return $model->morphTo( - 'related', - $model->getMorphType(), - $model->getRelatedKey(), - $model->getForeignKey(), - )->withDefault(); - })->withRelatableQuery(function (Request $request, Builder $query, MorphPivot $model): Builder { - return $this->resolveRelatableQuery($request, $model->pivotParent) - ->unless($this->allowDuplicateRelations, function (Builder $query) use ($model): Builder { - return $query->whereNotIn( + MorphTo::make($this->getRelatedName(), 'related', static fn (MorphPivot $model): MorphToRelation => $model->morphTo( + 'related', + $model->getMorphType(), + $model->getRelatedKey(), + $model->getForeignKey(), + )->withDefault()) + ->withRelatableQuery( + fn (Request $request, Builder $query, MorphPivot $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, MorphPivot $model, mixed $value): void { - $model->setAttribute( - $this->getRelation($model->pivotParent)->getRelatedPivotKeyName(), - $value - ); - })->display(function (Model $model): mixed { - return $this->resolveDisplay($model); - }), + ))) + ->hydrate(function (Request $request, MorphPivot $model, mixed $value): void { + $model->setAttribute( + $this->getRelation($model->pivotParent)->getRelatedPivotKeyName(), + $value + ); + })->display(fn (Model $model): ?string => $this->resolveDisplay($model)), ]; } } diff --git a/src/Fields/Option.php b/src/Fields/Option.php index a7d89241..ffaec41a 100644 --- a/src/Fields/Option.php +++ b/src/Fields/Option.php @@ -50,7 +50,7 @@ public function selected(bool $value = true): static /** * Convert the element to a JSON serializable format. */ - public function jsonSerialize(): mixed + public function jsonSerialize(): array { return $this->toArray(); } diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index bbb20932..a7c41b56 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -198,9 +198,7 @@ public function resolveRouteKeyNameUsing(Closure $callback): static public function getRouteKeyName(): string { $callback = is_null($this->routeKeyNameResolver) - ? function (): string { - return Str::of($this->getRelationName())->singular()->ucfirst()->prepend('relation')->value(); - } + ? fn (): string => Str::of($this->getRelationName())->singular()->ucfirst()->prepend('relation')->value() : $this->routeKeyNameResolver; return call_user_func($callback); @@ -334,9 +332,7 @@ public function isTranslatable(): bool public function display(Closure|string $callback): static { if (is_string($callback)) { - $callback = static function (Model $model) use ($callback) { - return $model->getAttribute($callback); - }; + $callback = static fn (Model $model) => $model->getAttribute($callback); } $this->displayResolver = $callback; @@ -387,9 +383,7 @@ public function resolveFormat(Request $request, Model $model): ?string return $default; } - return Collection::wrap($default)->map(function (Model $related) use ($model, $request): mixed { - return $this->formatRelated($request, $model, $related); - })->filter()->join(', '); + return Collection::wrap($default)->map(fn (Model $related): ?string => $this->formatRelated($request, $model, $related))->filter()->join(', '); }; } @@ -443,9 +437,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() + ); } } @@ -494,9 +488,8 @@ public function resolveRelatableQuery(Request $request, Model $model): Builder $query = call_user_func_array($scope, [$request, $query, $model]); } - return $query->when(! is_null($this->queryResolver), function (Builder $query) use ($request, $model): Builder { - return call_user_func_array($this->queryResolver, [$request, $query, $model]); - }); + return $query + ->when(! is_null($this->queryResolver), fn (Builder $query): Builder => call_user_func_array($this->queryResolver, [$request, $query, $model])); } /** @@ -548,21 +541,11 @@ public function resolveOptions(Request $request, Model $model): array { return $this->resolveRelatableQuery($request, $model) ->get() - ->when(! is_null($this->groupResolver), function (Collection $collection) use ($request, $model): Collection { - return $collection->groupBy($this->groupResolver) - ->map(function (Collection $group, string $key) use ($request, $model): array { - return [ - 'label' => $key, - 'options' => $group->map(function (Model $related) use ($request, $model): array { - return $this->toOption($request, $model, $related); - })->all(), - ]; - }); - }, function (Collection $collection) use ($request, $model): Collection { - return $collection->map(function (Model $related) use ($request, $model): array { - return $this->toOption($request, $model, $related); - }); - }) + ->when(! is_null($this->groupResolver), fn (Collection $collection): Collection => $collection->groupBy($this->groupResolver) + ->map(fn (Collection $group, string $key): array => [ + 'label' => $key, + 'options' => $group->map(fn (Model $related): array => $this->toOption($request, $model, $related))->all(), + ]), fn (Collection $collection): Collection => $collection->map(fn (Model $related): array => $this->toOption($request, $model, $related))) ->toArray(); } @@ -772,13 +755,14 @@ public function getRouteMiddleware(): array */ protected function routesRegistered(Request $request): void { + $uri = $this->getUri(); + $routeKeyName = $this->getRouteKeyName(); + Root::instance()->breadcrumbs->patterns([ $this->getUri() => $this->label, - sprintf('%s/create', $this->getUri()) => __('Add'), - sprintf('%s/{%s}', $this->getUri(), $this->getRouteKeyName()) => function (Request $request): string { - return $this->resolveDisplay($request->route($this->getRouteKeyName())); - }, - sprintf('%s/{%s}/edit', $this->getUri(), $this->getRouteKeyName()) => __('Edit'), + sprintf('%s/create', $uri) => __('Add'), + sprintf('%s/{%s}', $uri, $routeKeyName) => fn (Request $request): string => $this->resolveDisplay($request->route($routeKeyName)), + sprintf('%s/{%s}/edit', $uri, $routeKeyName) => __('Edit'), ]); } @@ -875,11 +859,9 @@ public function routes(Router $router): void */ public function registerRouteConstraints(Request $request, Router $router): void { - $router->bind($this->getRouteKeyName(), function (string $id, Route $route) use ($router): Model { - return match ($id) { - 'create' => $this->getRelation($route->parentOfParameter($this->getRouteKeyName()))->make(), - default => $this->resolveRouteBinding($router->getCurrentRequest(), $id), - }; + $router->bind($this->getRouteKeyName(), fn (string $id, Route $route): Model => match ($id) { + 'create' => $this->getRelation($route->parentOfParameter($this->getRouteKeyName()))->make(), + default => $this->resolveRouteBinding($router->getCurrentRequest(), $id), }); } @@ -892,9 +874,7 @@ public function parseQueryString(string $url): array parse_str($query, $result); - return array_filter($result, function (string $key): bool { - return str_starts_with($key, $this->getRequestKey()); - }, ARRAY_FILTER_USE_KEY); + return array_filter($result, fn (string $key): bool => str_starts_with($key, $this->getRequestKey()), ARRAY_FILTER_USE_KEY); } /** @@ -952,18 +932,14 @@ public function toIndex(Request $request, Model $model): array ->visible('index') ->standalone(false) ->mapToForms($request, $model), - 'data' => $this->paginate($request, $model)->through(function (Model $related) use ($request, $model): array { - return $this->mapRelated($request, $model, $related); - }), + 'data' => $this->paginate($request, $model)->through(fn (Model $related): array => $this->mapRelated($request, $model, $related)), 'perPageOptions' => $this->getPerPageOptions(), 'perPageKey' => $this->getPerPageKey(), 'sortKey' => $this->getSortKey(), 'filters' => $this->resolveFilters($request) ->authorized($request) ->renderable() - ->map(static function (RenderableFilter $filter) use ($request, $model): array { - return $filter->toField()->toInput($request, $model); - }) + ->map(static fn (RenderableFilter $filter): array => $filter->toField()->toInput($request, $model)) ->all(), 'activeFilters' => $this->resolveFilters($request)->active($request)->count(), 'parentUrl' => URL::query($request->server('HTTP_REFERER'), $request->query()), diff --git a/src/Fields/Repeater.php b/src/Fields/Repeater.php index 2778991f..876140c3 100644 --- a/src/Fields/Repeater.php +++ b/src/Fields/Repeater.php @@ -91,7 +91,7 @@ public function getAddNewOptionLabel(): string /** * {@inheritdoc} */ - public function getValueForHydrate(Request $request): mixed + public function getValueForHydrate(Request $request): array { return array_values((array) parent::getValueForHydrate($request)); } @@ -99,7 +99,7 @@ public function getValueForHydrate(Request $request): mixed /** * {@inheritdoc} */ - public function getOldValue(Request $request): mixed + public function getOldValue(Request $request): array { return array_values((array) parent::getOldValue($request)); } @@ -114,9 +114,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->getModelAttribute())->value(); - }); + $field->resolveRouteKeyNameUsing( + fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getModelAttribute())->value() + ); } } @@ -185,9 +185,7 @@ public function resolveOptions(Request $request, Model $model): array { $value = (array) $this->resolveValue($request, $model); - return array_map(function (array $option) use ($request, $model): array { - return $this->toOption($request, $model, $this->newTemporaryModel($option)); - }, $value); + return array_map(fn (array $option): array => $this->toOption($request, $model, $this->newTemporaryModel($option)), $value); } /** @@ -219,12 +217,10 @@ public function resolveFormat(Request $request, Model $model): ?string { if (is_null($this->formatResolver)) { $this->formatResolver = function (Request $request, Model $model, ?array $value = null): string { - $values = array_map(function (array $value) use ($request, $model): array { - return $this->resolveOptionFields($request, $model, $this->newTemporaryModel($value)) - ->authorized($request, $model) - ->visible('show') - ->mapToDisplay($request, $model); - }, (array) $value); + $values = array_map(fn (array $value): array => $this->resolveOptionFields($request, $model, $this->newTemporaryModel($value)) + ->authorized($request, $model) + ->visible('show') + ->mapToDisplay($request, $model), (array) $value); return View::make('root::fields.repeater-table', ['values' => $values])->render(); }; diff --git a/src/Fields/Select.php b/src/Fields/Select.php index c6418db5..29d53a5b 100644 --- a/src/Fields/Select.php +++ b/src/Fields/Select.php @@ -89,9 +89,7 @@ public function resolveFormat(Request $request, Model $model): ?string */ public function options(array|Closure $value): static { - $this->optionsResolver = is_callable($value) ? $value : static function () use ($value): array { - return $value; - }; + $this->optionsResolver = is_callable($value) ? $value : static fn (): array => $value; return $this; } diff --git a/src/Fields/Slug.php b/src/Fields/Slug.php index 9c576f68..b6924d24 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -109,11 +109,11 @@ public function persist(Request $request, Model $model, mixed $value): void /** * {@inheritdoc} */ - public function getValueForHydrate(Request $request): mixed + public function getValueForHydrate(Request $request): string { $value = parent::getValueForHydrate($request); - if (! $this->isNullable() && empty($value)) { + if (empty($value) && ! $this->isNullable()) { $value = Str::random(); } @@ -148,11 +148,9 @@ public function unique(bool $value = true): static $this->unique = $value; if ($value) { - $this->createRules(static function (Request $request, Model $model): array { - return [Rule::unique($model->getTable())]; - })->updateRules(static function (Request $request, Model $model): array { - return [Rule::unique($model->getTable())->ignoreModel($model)]; - }); + $this->createRules( + static fn (Request $request, Model $model): array => [Rule::unique($model->getTable())] + )->updateRules(static fn (Request $request, Model $model): array => [Rule::unique($model->getTable())->ignoreModel($model)]); } return $this; @@ -198,9 +196,7 @@ protected function generate(Request $request, Model $model): string $value = is_null($match) ? $value : preg_replace_callback( sprintf('/%s([\d]+)?$/', preg_quote($this->separator)), - static function (array $match): string { - return str_replace($match[1], (string) (((int) $match[1]) + 1), $match[0]); - }, + static fn (array $match): string => str_replace($match[1], (string) (((int) $match[1]) + 1), $match[0]), $match ); diff --git a/src/Fields/URL.php b/src/Fields/URL.php index 2b246cd9..4df440df 100644 --- a/src/Fields/URL.php +++ b/src/Fields/URL.php @@ -42,14 +42,12 @@ public function text(Closure|string $value): static 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 : sprintf( - '%3$s', - $value, - $this->isExternal($value) ? ' data-turbo="false" target="_blank"' : '', - call_user_func_array($this->textResolver, [$model]) - ); - }; + $this->formatResolver = fn (Request $request, Model $model, mixed $value): ?string => is_null($value) ? $value : sprintf( + '%3$s', + $value, + $this->isExternal($value) ? ' data-turbo="false" target="_blank"' : '', + call_user_func_array($this->textResolver, [$model]) + ); } return parent::resolveFormat($request, $model); diff --git a/src/Filters/Filters.php b/src/Filters/Filters.php index 1d012b84..7fa12566 100644 --- a/src/Filters/Filters.php +++ b/src/Filters/Filters.php @@ -48,9 +48,7 @@ public function apply(Request $request, Builder $query): Builder */ public function renderable(): static { - return $this->filter(static function (Filter $filter): bool { - return $filter instanceof RenderableFilter; - }); + return $this->filter(static fn (Filter $filter): bool => $filter instanceof RenderableFilter); } /** @@ -58,9 +56,7 @@ public function renderable(): static */ public function functional(): static { - return $this->reject(static function (Filter $filter): bool { - return $filter instanceof RenderableFilter; - }); + return $this->reject(static fn (Filter $filter): bool => $filter instanceof RenderableFilter); } /** @@ -76,8 +72,6 @@ public function active(Request $request): static */ public function mapToData(Request $request): array { - return $this->mapWithKeys(static function (Filter $filter) use ($request): array { - return [$filter->getKey() => $filter->getValue($request)]; - })->toArray(); + return $this->mapWithKeys(static fn (Filter $filter): array => [$filter->getKey() => $filter->getValue($request)])->toArray(); } } diff --git a/src/Filters/Search.php b/src/Filters/Search.php index beca61b2..aba98e31 100644 --- a/src/Filters/Search.php +++ b/src/Filters/Search.php @@ -62,11 +62,9 @@ public function apply(Request $request, Builder $query, mixed $value): Builder */ public function getSearchableAttributes(): array { - return $this->fields->mapWithKeys(static function (Field $field): array { - return [ - $field->getModelAttribute() => $field instanceof Relation ? $field->getSearchableColumns() : null, - ]; - })->all(); + return $this->fields->mapWithKeys(static fn (Field $field): array => [ + $field->getModelAttribute() => $field instanceof Relation ? $field->getSearchableColumns() : null, + ])->all(); } /** diff --git a/src/Filters/Select.php b/src/Filters/Select.php index 32ad3fe0..a89d942c 100644 --- a/src/Filters/Select.php +++ b/src/Filters/Select.php @@ -53,9 +53,7 @@ public function isMultiple(): bool public function toField(): Field { return Field::make($this->getName(), $this->getRequestKey()) - ->options(App::call(function (Request $request): array { - return $this->options($request); - })) + ->options(App::call(fn (Request $request): array => $this->options($request))) ->value(fn (Request $request): mixed => $this->getValue($request)) ->multiple($this->isMultiple()); } diff --git a/src/Filters/Sort.php b/src/Filters/Sort.php index ef593684..fca746ee 100644 --- a/src/Filters/Sort.php +++ b/src/Filters/Sort.php @@ -34,11 +34,9 @@ public function apply(Request $request, Builder $query, mixed $value): Builder { $value = array_replace(['by' => 'id', 'order' => 'desc'], (array) $value); - $attributes = $this->fields->mapWithKeys(static function (Field $field): array { - return [ - $field->getModelAttribute() => $field instanceof Relation ? $field->getSortableColumn() : null, - ]; - })->all(); + $attributes = $this->fields->mapWithKeys(static fn (Field $field): array => [ + $field->getModelAttribute() => $field instanceof Relation ? $field->getSortableColumn() : null, + ])->all(); if (! array_key_exists($value['by'], $attributes)) { return $query; diff --git a/src/Http/Controllers/Auth/ForgotPasswordController.php b/src/Http/Controllers/Auth/ForgotPasswordController.php index a7a65cd6..66f66afc 100644 --- a/src/Http/Controllers/Auth/ForgotPasswordController.php +++ b/src/Http/Controllers/Auth/ForgotPasswordController.php @@ -29,7 +29,7 @@ public function send(Request $request): RedirectResponse { $data = $request->validate(['email' => ['required', 'string', 'email']]); - Password::broker()->sendResetLink($data, static function (User $user, string $token): void { + Password::broker()->sendResetLink($data, static function (User $user, #[\SensitiveParameter] string $token): void { $user->notify(new ResetPassword($token)); }); diff --git a/src/Http/Controllers/Auth/ResetPasswordController.php b/src/Http/Controllers/Auth/ResetPasswordController.php index 2292457c..3e40b76d 100644 --- a/src/Http/Controllers/Auth/ResetPasswordController.php +++ b/src/Http/Controllers/Auth/ResetPasswordController.php @@ -43,7 +43,7 @@ public function reset(Request $request): RedirectResponse $response = Password::broker()->reset( $request->only(['email', 'password', 'password_confirmation', 'token']), - function (User $user, string $password): void { + function (User $user, #[\SensitiveParameter] string $password): void { $this->resetPassword($user, $password); if ($user instanceof MustVerifyEmail && ! $user->hasVerifiedEmail()) { @@ -60,7 +60,7 @@ function (User $user, string $password): void { /** * Reset the given user's password. */ - protected function resetPassword(User $user, string $password): void + protected function resetPassword(User $user, #[\SensitiveParameter] string $password): void { $user->setAttribute('password', Hash::make($password)); diff --git a/src/Interfaces/Models/User.php b/src/Interfaces/Models/User.php index 55254d9c..7cb2be14 100644 --- a/src/Interfaces/Models/User.php +++ b/src/Interfaces/Models/User.php @@ -30,7 +30,7 @@ public function authCode(): HasOne; public function authCodes(): HasMany; /** - * Determine whether the object requires two factor authentitaction. + * Determine whether the object requires two factor authentication. */ public function requiresTwoFactorAuthentication(): bool; diff --git a/src/Listeners/FormatRootStubs.php b/src/Listeners/FormatRootStubs.php index 6d2ae422..92c8ca0e 100644 --- a/src/Listeners/FormatRootStubs.php +++ b/src/Listeners/FormatRootStubs.php @@ -13,7 +13,7 @@ class FormatRootStubs public function handle(VendorTagPublished $event): void { if ($event->tag === 'root-stubs') { - foreach ($event->paths as $from => $to) { + foreach ($event->paths as $to) { $contents = file_get_contents($to); $contents = str_replace('{{ namespace }}', App::getNamespace(), $contents); diff --git a/src/Models/Medium.php b/src/Models/Medium.php index 5381811a..2dab9b3b 100644 --- a/src/Models/Medium.php +++ b/src/Models/Medium.php @@ -156,8 +156,8 @@ public static function fromPath(string $path, array $attributes = []): static return new static(array_merge([ 'file_name' => $name = basename($path), 'mime_type' => $type, - 'width' => isset($width) ? $width : null, - 'height' => isset($height) ? $height : null, + 'width' => $width ?? null, + 'height' => $height ?? null, 'disk' => Config::get('root.media.disk', 'public'), 'size' => max(round(filesize($path) / 1024), 1), 'name' => pathinfo($name, PATHINFO_FILENAME), @@ -185,7 +185,7 @@ public function uniqueIds(): array */ public function user(): BelongsTo { - return $this->belongsTo(get_class(App::make(User::class))); + return $this->belongsTo(App::make(User::class)::class); } /** @@ -196,9 +196,7 @@ public function user(): BelongsTo protected function isImage(): Attribute { return new Attribute( - get: static function (mixed $value, array $attributes): bool { - return Str::is('image/*', $attributes['mime_type']); - } + get: static fn (mixed $value, array $attributes): bool => Str::is('image/*', $attributes['mime_type']) ); } @@ -210,15 +208,11 @@ protected function isImage(): Attribute protected function urls(): Attribute { return new Attribute( - get: function (): array { - return array_reduce( - $this->properties['conversions'] ?? [], - function (array $urls, string $conversion): array { - return array_merge($urls, [$conversion => $this->getUrl($conversion)]); - }, - ['original' => $this->getUrl()] - ); - } + get: fn (): array => array_reduce( + $this->properties['conversions'] ?? [], + fn (array $urls, string $conversion): array => array_merge($urls, [$conversion => $this->getUrl($conversion)]), + ['original' => $this->getUrl()] + ) ); } @@ -242,11 +236,9 @@ protected function formattedSize(): Attribute protected function dimensions(): Attribute { return new Attribute( - get: static function (mixed $value, array $attributes): ?string { - return isset($attributes['width'], $attributes['height']) - ? sprintf('%dx%d px', $attributes['width'], $attributes['height']) - : null; - } + get: static fn (mixed $value, array $attributes): ?string => isset($attributes['width'], $attributes['height']) + ? sprintf('%dx%d px', $attributes['width'], $attributes['height']) + : null ); } diff --git a/src/Models/Notification.php b/src/Models/Notification.php index 0701f2fd..da03bca2 100644 --- a/src/Models/Notification.php +++ b/src/Models/Notification.php @@ -68,9 +68,7 @@ public function getMorphClass(): string protected function formattedCreatedAt(): Attribute { return new Attribute( - get: function (): ?string { - return $this->created_at?->setTimezone(Root::instance()->getTimezone())?->isoFormat('YYYY. MMMM DD. HH:mm'); - } + get: fn (): ?string => $this->created_at?->setTimezone(Root::instance()->getTimezone())?->isoFormat('YYYY. MMMM DD. HH:mm') ); } @@ -82,9 +80,7 @@ protected function formattedCreatedAt(): Attribute protected function isRead(): Attribute { return new Attribute( - get: function (): bool { - return ! is_null($this->read_at); - } + get: fn (): bool => ! is_null($this->read_at) ); } @@ -96,9 +92,7 @@ protected function isRead(): Attribute protected function url(): Attribute { return new Attribute( - get: function (): ?string { - return $this->exists ? URL::route('root.api.notifications.update', $this) : null; - } + get: fn (): ?string => $this->exists ? URL::route('root.api.notifications.update', $this) : null ); } } diff --git a/src/Models/User.php b/src/Models/User.php index 226691c8..d448ed96 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -79,7 +79,7 @@ public function triggeredRootEvents(): HasMany } /** - * Determine whether the object requires two factor authentitaction. + * Determine whether the object requires two factor authentication. */ public function requiresTwoFactorAuthentication(): bool { @@ -135,11 +135,9 @@ public function generateDeviceToken(Request $request): string protected function avatar(): Attribute { return new Attribute( - get: static function (mixed $value, array $attributes): ?string { - return isset($attributes['email']) - ? sprintf('https://www.gravatar.com/avatar/%s?d=mp', md5($attributes['email'])) - : null; - } + get: static fn (mixed $value, array $attributes): ?string => isset($attributes['email']) + ? sprintf('https://www.gravatar.com/avatar/%s?d=mp', md5($attributes['email'])) + : null ); } } diff --git a/src/Notifications/AuthCodeNotification.php b/src/Notifications/AuthCodeNotification.php index 3d1fdc5d..9fb91d6a 100644 --- a/src/Notifications/AuthCodeNotification.php +++ b/src/Notifications/AuthCodeNotification.php @@ -29,7 +29,7 @@ class AuthCodeNotification extends Notification implements ShouldQueue /** * Create a new notification instance. */ - public function __construct(AuthCode $code) + public function __construct(#[\SensitiveParameter] AuthCode $code) { $this->code = $code; } diff --git a/src/Resources/Resource.php b/src/Resources/Resource.php index 67f11fa0..b47900e9 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -282,9 +282,7 @@ public function resolveRouteBindingQuery(Request $request): Builder ->withoutEagerLoads() ->when( $this->isSoftDeletable(), - static function (Builder $query): Builder { - return $query->withTrashed(); - } + static fn (Builder $query): Builder => $query->withTrashed() ); } @@ -351,18 +349,14 @@ public function resolveTranslationsField(Request $request): ?Translations { return $this->translatable() ? Translations::make() - ->withFields(function () use ($request): array { - return $this->resolveFields($request) - ->translatable() - ->map(static function (Field $field): Field { - return (clone $field) - ->translatable(false) - ->setModelAttribute($key = 'values->'.$field->getModelAttribute()) - ->name($key) - ->id($key); - }) - ->all(); - }) + ->withFields(fn (): array => $this->resolveFields($request) + ->translatable() + ->map(static fn (Field $field): Field => (clone $field) + ->translatable(false) + ->setModelAttribute($key = 'values->'.$field->getModelAttribute()) + ->name($key) + ->id($key)) + ->all()) : null; } @@ -372,12 +366,10 @@ public function resolveTranslationsField(Request $request): ?Translations public function resolveFields(Request $request): Fields { if (is_null($this->fields)) { - $this->withFields(function () use ($request): array { - return array_values(array_filter([ - $this->resolveTranslationsField($request), - $this->resolveEventsField($request), - ])); - }); + $this->withFields(fn (): array => array_values(array_filter([ + $this->resolveTranslationsField($request), + $this->resolveEventsField($request), + ]))); } return $this->__resolveFields($request); @@ -410,9 +402,7 @@ protected function resolveField(Request $request, Field $field): void $field->resolveErrorsUsing(fn (Request $request): MessageBag => $this->errors($request)); 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()); } } @@ -495,9 +485,7 @@ public function paginate(Request $request): LengthAwarePaginator ->latest() ->paginate($request->input($this->getPerPageKey())) ->withQueryString() - ->through(function (Model $model) use ($request): array { - return $this->mapModel($request, $model); - }); + ->through(fn (Model $model): array => $this->mapModel($request, $model)); } /** @@ -665,9 +653,7 @@ public function toIndex(Request $request): array 'filters' => $this->resolveFilters($request) ->authorized($request) ->renderable() - ->map(static function (RenderableFilter $filter) use ($request, $model): array { - return $filter->toField()->toInput($request, $model); - }) + ->map(static fn (RenderableFilter $filter): array => $filter->toField()->toInput($request, $model)) ->all(), 'activeFilters' => $this->resolveFilters($request)->active($request)->count(), 'url' => $this->getUri(), @@ -723,11 +709,9 @@ public function toShow(Request $request, Model $model): array 'relations' => $this->resolveFields($request) ->subResource() ->authorized($request, $model) - ->map(static function (Relation $relation) use ($request, $model): array { - return array_merge($relation->toSubResource($request, $model), [ - 'url' => URL::query($relation->modelUrl($model), $relation->parseQueryString($request->fullUrl())), - ]); - }), + ->map(static fn (Relation $relation): array => array_merge($relation->toSubResource($request, $model), [ + 'url' => URL::query($relation->modelUrl($model), $relation->parseQueryString($request->fullUrl())), + ])), 'abilities' => array_merge( $this->mapResourceAbilities($request), $this->mapModelAbilities($request, $model) diff --git a/src/Resources/Resources.php b/src/Resources/Resources.php index c30df0d6..5026d667 100644 --- a/src/Resources/Resources.php +++ b/src/Resources/Resources.php @@ -65,9 +65,7 @@ public function forModel(string|Model $model): ?Resource { $model = is_string($model) ? $model : $model::class; - return $this->first(static function (Resource $resource) use ($model): bool { - return $resource->getModel() === $model; - }); + return $this->first(static fn (Resource $resource): bool => $resource->getModel() === $model); } /** diff --git a/src/Root.php b/src/Root.php index a2375211..8bef4738 100644 --- a/src/Root.php +++ b/src/Root.php @@ -25,7 +25,7 @@ class Root * * @var string */ - public const VERSION = '2.5.6'; + public const string VERSION = '2.5.6'; /** * The registered booting callbacks. @@ -111,16 +111,14 @@ public function boot(): void call_user_func_array($callback, [$this]); } + $path = $this->getPath(); + $this->breadcrumbs->patterns([ - $this->getPath() => __('Dashboard'), - sprintf('%s/resources/{resource}', $this->getPath()) => static function (Request $request): string { - return $request->route('_resource')->getName(); - }, - sprintf('%s/resources/{resource}/create', $this->getPath()) => __('Create'), - sprintf('%s/resources/{resource}/{resourceModel}', $this->getPath()) => static function (Request $request): string { - return $request->route('_resource')->modelTitle($request->route('resourceModel')); - }, - sprintf('%s/resources/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'), + $path => __('Dashboard'), + sprintf('%s/resources/{resource}', $path) => static fn (Request $request): string => $request->route('_resource')->getName(), + sprintf('%s/resources/{resource}/create', $path) => __('Create'), + sprintf('%s/resources/{resource}/{resourceModel}', $path) => static fn (Request $request): string => $request->route('_resource')->modelTitle($request->route('resourceModel')), + sprintf('%s/resources/{resource}/{resourceModel}/edit', $path) => __('Edit'), ]); } diff --git a/src/RootServiceProvider.php b/src/RootServiceProvider.php index d69dfb28..0eb3327d 100644 --- a/src/RootServiceProvider.php +++ b/src/RootServiceProvider.php @@ -63,9 +63,7 @@ class RootServiceProvider extends ServiceProvider */ public function register(): void { - $this->app->singleton(Root::class, static function (Application $app): Root { - return new Root($app); - }); + $this->app->singleton(Root::class, static fn (Application $app): Root => new Root($app)); $this->app->alias(Root::class, 'root'); @@ -81,9 +79,7 @@ public function register(): void $app->make(Root::class)->boot(); }); - $this->app['request']->macro('isTurboFrameRequest', function (): bool { - return $this->hasHeader('Turbo-Frame'); - }); + $this->app['request']->macro('isTurboFrameRequest', fn (): bool => $this->hasHeader('Turbo-Frame')); } /** @@ -146,16 +142,14 @@ protected function registerRoutes(): void $this->app['router']->bind('resource', function (string $key) use ($root): Resource { try { return $root->resources->resolve($key); - } catch (ResourceResolutionException $exception) { + } catch (ResourceResolutionException) { throw new NotFoundHttpException; } }); - $this->app['router']->bind('resourceModel', function (string $id, Route $route): Model { - return $id === 'create' - ? $route->parameter('_resource')->getModelInstance() - : $route->parameter('_resource')->resolveRouteBinding($this->app['request'], $id); - }); + $this->app['router']->bind('resourceModel', fn (string $id, Route $route): Model => $id === 'create' + ? $route->parameter('_resource')->getModelInstance() + : $route->parameter('_resource')->resolveRouteBinding($this->app['request'], $id)); $this->app['router'] ->middleware(['web']) @@ -174,9 +168,7 @@ protected function registerRoutes(): void $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); }); - RateLimiter::for('root.auth', static function (Request $request): Limit { - return Limit::perMinute(6)->by($request->user()?->id ?: $request->ip()); - }); + RateLimiter::for('root.auth', static fn (Request $request): Limit => Limit::perMinute(6)->by($request->user()?->id ?: $request->ip())); } /** @@ -227,11 +219,9 @@ protected function registerExceptions(): void { $exceptions = $this->app->make(ExceptionHandler::class); - $exceptions->renderable(static function (SaveFormDataException $exception): RedirectResponse { - return Redirect::back() - ->withInput() - ->with('alerts.form-save', Alert::error($exception->getMessage())); - }); + $exceptions->renderable(static fn (SaveFormDataException $exception): RedirectResponse => Redirect::back() + ->withInput() + ->with('alerts.form-save', Alert::error($exception->getMessage()))); } /** @@ -239,9 +229,7 @@ protected function registerExceptions(): void */ protected function registerAuth(): void { - Gate::define('viewRoot', static function (User $user): bool { - return Root::instance()->authorized($user); - }); + Gate::define('viewRoot', static fn (User $user): bool => Root::instance()->authorized($user)); Gate::policy(Medium::getProxiedClass(), MediumPolicy::class); } diff --git a/src/Settings/Repository.php b/src/Settings/Repository.php index 018011b8..4172856d 100644 --- a/src/Settings/Repository.php +++ b/src/Settings/Repository.php @@ -83,7 +83,7 @@ public function getCasts(): array */ public function get(string $key, mixed $default = null, bool $fresh = false): mixed { - if ($this->offsetExists($key) && ! $fresh) { + if (! $fresh && $this->offsetExists($key)) { return $this->offsetGet($key); } diff --git a/src/Support/Alert.php b/src/Support/Alert.php index 36bc0c97..33376290 100644 --- a/src/Support/Alert.php +++ b/src/Support/Alert.php @@ -8,13 +8,13 @@ class Alert implements Arrayable, Jsonable, Stringable { - public const INFO = 'info'; + public const string INFO = 'info'; - public const SUCCESS = 'success'; + public const string SUCCESS = 'success'; - public const ERROR = 'error'; + public const string ERROR = 'error'; - public const WARNING = 'warning'; + public const string WARNING = 'warning'; /** * The alert message. diff --git a/src/Traits/HasAttributes.php b/src/Traits/HasAttributes.php index b2c10bb8..23b87d43 100644 --- a/src/Traits/HasAttributes.php +++ b/src/Traits/HasAttributes.php @@ -78,7 +78,7 @@ public function hasAttribute(string $key): bool public function getAttribute(string $key, mixed $default = null): mixed { return match ($key) { - 'class' => $this->classList()->__toString(), + 'class' => (string) $this->classList(), default => $this->attributes[$key] ?? $default, }; } @@ -135,9 +135,7 @@ public function resolveAttributes(): array { return array_reduce( array_keys($this->attributes), - function (array $attributes, string $key): mixed { - return array_merge($attributes, [$key => $this->resolveAttribute($key)]); - }, + fn (array $attributes, string $key): array => array_merge($attributes, [$key => $this->resolveAttribute($key)]), [] ); } diff --git a/src/Traits/HasMedia.php b/src/Traits/HasMedia.php index b02e9b26..1a971ef2 100644 --- a/src/Traits/HasMedia.php +++ b/src/Traits/HasMedia.php @@ -15,7 +15,7 @@ trait HasMedia protected static function bootHasMedia(): void { static::deleting(static function (self $model): void { - if (! in_array(SoftDeletes::class, class_uses_recursive($model)) || $model->forceDeleting) { + if ($model->forceDeleting || ! in_array(SoftDeletes::class, class_uses_recursive($model))) { $model->media()->detach(); } }); diff --git a/src/Traits/InteractsWithProxy.php b/src/Traits/InteractsWithProxy.php index 5ea084e1..75ce24ed 100644 --- a/src/Traits/InteractsWithProxy.php +++ b/src/Traits/InteractsWithProxy.php @@ -37,6 +37,6 @@ public static function proxy(): static */ public static function getProxiedClass(): string { - return get_class(static::proxy()); + return static::proxy()::class; } } diff --git a/src/Traits/RegistersRoutes.php b/src/Traits/RegistersRoutes.php index 09a9e95a..6b2224cc 100644 --- a/src/Traits/RegistersRoutes.php +++ b/src/Traits/RegistersRoutes.php @@ -34,7 +34,7 @@ public function getUri(): ?string */ public function getRoutePrefix(): string { - return (string) $this->getUriKey(); + return $this->getUriKey(); } /** diff --git a/src/Traits/ResolvesFields.php b/src/Traits/ResolvesFields.php index cfb4ec94..922c8f07 100644 --- a/src/Traits/ResolvesFields.php +++ b/src/Traits/ResolvesFields.php @@ -48,10 +48,8 @@ public function hasFileField(Request $request): bool return $this->resolveFields($request) ->subResource(false) ->visible(['update', 'create']) - ->some(static function (Field $field) use ($request): bool { - return $field instanceof File && ! $field instanceof Media - || (in_array(ResolvesFields::class, class_uses_recursive($field)) && $field->hasFileField($request)); - }); + ->some(static fn (Field $field): bool => ($field instanceof File && ! $field instanceof Media) + || (in_array(ResolvesFields::class, class_uses_recursive($field)) && $field->hasFileField($request))); } /** diff --git a/src/Traits/ResolvesVisibility.php b/src/Traits/ResolvesVisibility.php index 071bd3ad..463ff16b 100644 --- a/src/Traits/ResolvesVisibility.php +++ b/src/Traits/ResolvesVisibility.php @@ -33,9 +33,7 @@ public function visibleOn(string|array|Closure $context): static { $this->visibilityResolvers[] = $context instanceof Closure ? $context - : static function (string|array $currentContext) use ($context) { - return ! empty(array_intersect(Arr::wrap($currentContext), Arr::wrap($context))); - }; + : static fn (string|array $currentContext) => ! empty(array_intersect(Arr::wrap($currentContext), Arr::wrap($context))); return $this; } @@ -45,10 +43,8 @@ public function visibleOn(string|array|Closure $context): static */ public function hiddenOn(string|array|Closure $context): static { - return $this->visibleOn(static function (array|string $currentContext) use ($context): bool { - return $context instanceof Closure - ? ! call_user_func_array($context, [$currentContext]) - : empty(array_intersect(Arr::wrap($currentContext), Arr::wrap($context))); - }); + return $this->visibleOn(static fn (array|string $currentContext): bool => $context instanceof Closure + ? ! call_user_func_array($context, [$currentContext]) + : empty(array_intersect(Arr::wrap($currentContext), Arr::wrap($context)))); } } diff --git a/src/View/Components/Alert.php b/src/View/Components/Alert.php index e8d27543..143b1e1a 100644 --- a/src/View/Components/Alert.php +++ b/src/View/Components/Alert.php @@ -13,7 +13,7 @@ class Alert extends Component protected string $type = 'info'; /** - * Incidates whether the alert is closable. + * Indicates whether the alert is closable. */ protected bool $closable = false; diff --git a/src/Widgets/Metric.php b/src/Widgets/Metric.php index c3f000f7..bcd9783a 100644 --- a/src/Widgets/Metric.php +++ b/src/Widgets/Metric.php @@ -59,7 +59,7 @@ public function getCurrentRange(Request $request): string $range = $request->input('range', $default); - return in_array($range, array_keys($this->ranges())) ? $range : $default; + return array_key_exists($range, $this->ranges()) ? $range : $default; } /** diff --git a/src/Widgets/Trend.php b/src/Widgets/Trend.php index 10160d14..e6425c7b 100644 --- a/src/Widgets/Trend.php +++ b/src/Widgets/Trend.php @@ -467,7 +467,7 @@ public function data(Request $request): array /** * Get the results by the given interval. */ - public function resultBy(array $result, DatePeriod $period, string $interval): mixed + public function resultBy(array $result, DatePeriod $period, string $interval): array { $dates = []; diff --git a/src/Widgets/Value.php b/src/Widgets/Value.php index 0ac36897..27a04262 100644 --- a/src/Widgets/Value.php +++ b/src/Widgets/Value.php @@ -51,14 +51,12 @@ protected function aggregate(Builder $query, DatePeriod $period, string $fn, str return parent::aggregate($query, $extended, $fn, $column, $dateColumn)->when( $period->getStartDate()->getTimestamp() > 0, - function (Builder $query) use ($period, $dateColumn): Builder { - return $query->selectRaw(sprintf( - "(case when %s between '%s' and '%s' then 'current' else 'previous' end) as `__interval`", - $query->getQuery()->getGrammar()->wrap($dateColumn), - $period->getStartDate()->format('Y-m-d H:i:s'), - $period->getEndDate()->format('Y-m-d H:i:s') - ))->groupBy('__interval'); - } + fn (Builder $query): Builder => $query->selectRaw(sprintf( + "(case when %s between '%s' and '%s' then 'current' else 'previous' end) as `__interval`", + $query->getQuery()->getGrammar()->wrap($dateColumn), + $period->getStartDate()->format('Y-m-d H:i:s'), + $period->getEndDate()->format('Y-m-d H:i:s') + ))->groupBy('__interval') ); }