From 1f05a1e3ac975db35c4e4674d90a0220070960e8 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:08:27 +0100 Subject: [PATCH 01/20] fix typos --- src/Actions/Action.php | 2 +- src/Fields/Field.php | 2 +- src/Interfaces/Models/User.php | 2 +- src/Models/User.php | 2 +- src/View/Components/Alert.php | 2 +- 5 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 8ef5de5e..64f078df 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; diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 8782c4bd..b6d3b4b0 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 { 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/Models/User.php b/src/Models/User.php index 226691c8..eb4d22a5 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 { 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; From 827559c0975827105504b7e627ef2d449f638484 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:10:22 +0100 Subject: [PATCH 02/20] remove useless ternary --- src/Fields/Field.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Fields/Field.php b/src/Fields/Field.php index b6d3b4b0..90882ef2 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -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()); } /** From 30c8f3ed5646b2d663085a059687c487b0ac3b5d Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:12:27 +0100 Subject: [PATCH 03/20] use null-coalescing instead of isset --- src/Models/Medium.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Models/Medium.php b/src/Models/Medium.php index 5381811a..1d8ac1f1 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), From 73940722cfcd96f1f8456bc79e0255d2394f81fb Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:13:34 +0100 Subject: [PATCH 04/20] getUriKey() always returns string --- src/Traits/RegistersRoutes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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(); } /** From bf3d31be8311c4bc0621cdb0458d408c6b462874 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:16:57 +0100 Subject: [PATCH 05/20] narrow down return types --- src/Actions/Action.php | 2 +- src/Casts/MetaValue.php | 2 +- src/Fields/BelongsToMany.php | 2 +- src/Fields/Boolean.php | 2 +- src/Fields/Field.php | 4 ++-- src/Fields/MorphToMany.php | 2 +- src/Fields/Option.php | 2 +- src/Fields/Relation.php | 2 +- src/Fields/Repeater.php | 4 ++-- src/Fields/Slug.php | 2 +- src/Traits/HasAttributes.php | 2 +- src/Widgets/Trend.php | 2 +- 12 files changed, 14 insertions(+), 14 deletions(-) diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 64f078df..9846b1a6 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -282,7 +282,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/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/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index a4f254a6..6e8c3d4c 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -170,7 +170,7 @@ 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; diff --git a/src/Fields/Boolean.php b/src/Fields/Boolean.php index 4e9ea4e5..a9bcd488 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()); } diff --git a/src/Fields/Field.php b/src/Fields/Field.php index 90882ef2..02cd46ca 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -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); } @@ -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(); } diff --git a/src/Fields/MorphToMany.php b/src/Fields/MorphToMany.php index 525b0602..d08204b6 100644 --- a/src/Fields/MorphToMany.php +++ b/src/Fields/MorphToMany.php @@ -48,7 +48,7 @@ public function fields(Request $request): array $this->getRelation($model->pivotParent)->getRelatedPivotKeyName(), $value ); - })->display(function (Model $model): mixed { + })->display(function (Model $model): ?string { return $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..54fc9314 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -387,7 +387,7 @@ public function resolveFormat(Request $request, Model $model): ?string return $default; } - return Collection::wrap($default)->map(function (Model $related) use ($model, $request): mixed { + return Collection::wrap($default)->map(function (Model $related) use ($model, $request): ?string { return $this->formatRelated($request, $model, $related); })->filter()->join(', '); }; diff --git a/src/Fields/Repeater.php b/src/Fields/Repeater.php index 2778991f..d21e92d1 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)); } diff --git a/src/Fields/Slug.php b/src/Fields/Slug.php index 9c576f68..af885b07 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -109,7 +109,7 @@ 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); diff --git a/src/Traits/HasAttributes.php b/src/Traits/HasAttributes.php index b2c10bb8..af7d7669 100644 --- a/src/Traits/HasAttributes.php +++ b/src/Traits/HasAttributes.php @@ -135,7 +135,7 @@ public function resolveAttributes(): array { return array_reduce( array_keys($this->attributes), - function (array $attributes, string $key): mixed { + function (array $attributes, string $key): array { return array_merge($attributes, [$key => $this->resolveAttribute($key)]); }, [] 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 = []; From c0659b45b4a5aad0c2891e26ffe8180ad36adabc Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:21:36 +0100 Subject: [PATCH 06/20] replace __toString() call with string cast --- src/Traits/HasAttributes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/HasAttributes.php b/src/Traits/HasAttributes.php index af7d7669..68247b4d 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, }; } From 8be3cfc7104c071b60d0cce12eaf90bb8ef53e16 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:22:28 +0100 Subject: [PATCH 07/20] mark sensitive parameters --- src/Http/Controllers/Auth/ForgotPasswordController.php | 2 +- src/Http/Controllers/Auth/ResetPasswordController.php | 4 ++-- src/Notifications/AuthCodeNotification.php | 2 +- 3 files changed, 4 insertions(+), 4 deletions(-) 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/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; } From d05b7ab6e65eac61185fe9a79a36eff0d3dbb52e Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:24:38 +0100 Subject: [PATCH 08/20] optimize if condition order --- src/Fields/Slug.php | 2 +- src/Settings/Repository.php | 2 +- src/Traits/HasMedia.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fields/Slug.php b/src/Fields/Slug.php index af885b07..124b64b7 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -113,7 +113,7 @@ public function getValueForHydrate(Request $request): string { $value = parent::getValueForHydrate($request); - if (! $this->isNullable() && empty($value)) { + if (empty($value) && ! $this->isNullable()) { $value = Str::random(); } 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/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(); } }); From dc695534cf0fe6774bc6fc388bc56ae6578b257b Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:25:08 +0100 Subject: [PATCH 09/20] replace misused in_array with array_key_exists --- src/Widgets/Metric.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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; } /** From 9b1452e080a6f17706627b588df6b43ae1489c21 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:26:21 +0100 Subject: [PATCH 10/20] wrap condition in parentheses --- src/Traits/ResolvesFields.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/ResolvesFields.php b/src/Traits/ResolvesFields.php index cfb4ec94..b996dcc2 100644 --- a/src/Traits/ResolvesFields.php +++ b/src/Traits/ResolvesFields.php @@ -49,7 +49,7 @@ public function hasFileField(Request $request): bool ->subResource(false) ->visible(['update', 'create']) ->some(static function (Field $field) use ($request): bool { - return $field instanceof File && ! $field instanceof Media + return ($field instanceof File && ! $field instanceof Media) || (in_array(ResolvesFields::class, class_uses_recursive($field)) && $field->hasFileField($request)); }); } From f137b2d3a2f6ff3537af4f2bdff819f3bea275cf Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:37:07 +0100 Subject: [PATCH 11/20] add missing constant types --- src/Root.php | 2 +- src/Support/Alert.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/Root.php b/src/Root.php index a2375211..3045281a 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. 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. From 8faaaaf20c283c3178576944403bd512b2f2bac6 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:41:14 +0100 Subject: [PATCH 12/20] remove unused key --- src/Listeners/FormatRootStubs.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From 46cf95bac42418ac0e2a749882301a65e21b2fa1 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:49:49 +0100 Subject: [PATCH 13/20] use first-class-callable --- src/Console/Commands/WidgetMake.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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))); } /** From de843b205c656e855ec5db7544fc1eb528771ced Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:52:44 +0100 Subject: [PATCH 14/20] use ::class instead of get_class call --- src/Fields/BelongsToMany.php | 2 +- src/Models/Medium.php | 2 +- src/Traits/InteractsWithProxy.php | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 6e8c3d4c..1ad70f70 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -69,7 +69,7 @@ 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->getRelation('related')::class, $model->getRelatedKey(), $model->getForeignKey(), 'related' diff --git a/src/Models/Medium.php b/src/Models/Medium.php index 1d8ac1f1..ee879dca 100644 --- a/src/Models/Medium.php +++ b/src/Models/Medium.php @@ -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); } /** 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; } } From 8379fea56ddb93762f4368bd047d461e26a778e5 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:53:25 +0100 Subject: [PATCH 15/20] remove unused catch variable --- src/RootServiceProvider.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/RootServiceProvider.php b/src/RootServiceProvider.php index d69dfb28..5acab0ac 100644 --- a/src/RootServiceProvider.php +++ b/src/RootServiceProvider.php @@ -146,7 +146,7 @@ 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; } }); From c8e0713e776d286df00796b12231a3cd786b13ff Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 13:55:28 +0100 Subject: [PATCH 16/20] replace switch with match --- src/Conversion/Image.php | 46 ++++++++++++---------------------------- 1 file changed, 14 insertions(+), 32 deletions(-) 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."), + }; } /** From f44ef9da04714044c7095734981446945804772d Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 14:01:38 +0100 Subject: [PATCH 17/20] replace closure with arrow function --- src/Actions/Action.php | 8 +-- src/Actions/Actions.php | 4 +- src/Actions/SendVerificationNotification.php | 4 +- src/Fields/BelongsToMany.php | 42 +++++-------- src/Fields/Boolean.php | 12 ++-- src/Fields/Date.php | 4 +- src/Fields/Dropdown.php | 16 ++--- src/Fields/Editor.php | 28 ++++----- src/Fields/Field.php | 4 +- src/Fields/Fields.php | 20 ++---- src/Fields/Fieldset.php | 4 +- src/Fields/File.php | 12 ++-- src/Fields/HasOneOrMany.php | 4 +- src/Fields/Media.php | 24 +++----- src/Fields/Meta.php | 8 +-- src/Fields/MorphTo.php | 4 +- src/Fields/MorphToMany.php | 30 ++++----- src/Fields/Relation.php | 64 ++++++-------------- src/Fields/Repeater.php | 18 ++---- src/Fields/Select.php | 4 +- src/Fields/Slug.php | 10 +-- src/Fields/URL.php | 14 ++--- src/Filters/Filters.php | 12 +--- src/Filters/Search.php | 8 +-- src/Filters/Select.php | 4 +- src/Filters/Sort.php | 8 +-- src/Models/Medium.php | 26 +++----- src/Models/Notification.php | 12 +--- src/Models/User.php | 8 +-- src/Resources/Resource.php | 54 ++++++----------- src/Resources/Resources.php | 4 +- src/Root.php | 8 +-- src/RootServiceProvider.php | 32 +++------- src/Traits/HasAttributes.php | 4 +- src/Traits/ResolvesFields.php | 6 +- src/Traits/ResolvesVisibility.php | 12 ++-- src/Widgets/Value.php | 14 ++--- 37 files changed, 182 insertions(+), 368 deletions(-) diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 9846b1a6..73b56379 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -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()); } } diff --git a/src/Actions/Actions.php b/src/Actions/Actions.php index 3a5820a5..ca1ddb8c 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..6996166a 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/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 1ad70f70..4eee98b7 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -67,29 +67,21 @@ 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( - $model->getRelation('related')::class, - $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()) - ); - }); - })->hydrate(function (Request $request, Pivot $model, mixed $value): void { + 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 ); - })->display(function (Model $model): ?string { - return $this->resolveDisplay($model); - }), + })->display(fn(Model $model): ?string => $this->resolveDisplay($model)), ]; } @@ -115,9 +107,7 @@ 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 +136,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; @@ -174,9 +162,7 @@ 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 a9bcd488..5d0aabd7 100644 --- a/src/Fields/Boolean.php +++ b/src/Fields/Boolean.php @@ -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..c7eec3e4 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..1038c84e 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..8b69a7b8 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 02cd46ca..a868ae82 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -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..29231127 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..46790d6e 100644 --- a/src/Fields/Fieldset.php +++ b/src/Fields/Fieldset.php @@ -47,9 +47,7 @@ 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..12e25446 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..2e4b27b8 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..0ed9ff1e 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..c211d27f 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..075bd34b 100644 --- a/src/Fields/MorphTo.php +++ b/src/Fields/MorphTo.php @@ -98,9 +98,7 @@ 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 d08204b6..a278ddf5 100644 --- a/src/Fields/MorphToMany.php +++ b/src/Fields/MorphToMany.php @@ -28,29 +28,21 @@ 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( - $query->getModel()->getQualifiedKeyName(), - $this->getRelation($model->pivotParent)->select($query->getModel()->getQualifiedKeyName()) - ); - }); - })->hydrate(function (Request $request, MorphPivot $model, mixed $value): void { + 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): ?string { - return $this->resolveDisplay($model); - }), + })->display(fn(Model $model): ?string => $this->resolveDisplay($model)), ]; } } diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index 54fc9314..ce2b3a5b 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): ?string { - 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,7 @@ 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 +486,7 @@ 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 +538,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(); } @@ -775,9 +755,7 @@ protected function routesRegistered(Request $request): void 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}', $this->getUri(), $this->getRouteKeyName()) => fn(Request $request): string => $this->resolveDisplay($request->route($this->getRouteKeyName())), sprintf('%s/{%s}/edit', $this->getUri(), $this->getRouteKeyName()) => __('Edit'), ]); } @@ -875,11 +853,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 +868,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 +926,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 d21e92d1..e7bea67c 100644 --- a/src/Fields/Repeater.php +++ b/src/Fields/Repeater.php @@ -114,9 +114,7 @@ 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 +183,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 +215,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..04c857d9 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 124b64b7..0bedff9d 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -148,11 +148,7 @@ 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 +194,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..2bf5e45f 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..711b2840 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..b4b0943c 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..4aa47789 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..25c0014d 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/Models/Medium.php b/src/Models/Medium.php index ee879dca..43ed824a 100644 --- a/src/Models/Medium.php +++ b/src/Models/Medium.php @@ -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..2222fabf 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 eb4d22a5..d063ece0 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -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/Resources/Resource.php b/src/Resources/Resource.php index 67f11fa0..2739dda3 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..41c66779 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 3045281a..a48b6da8 100644 --- a/src/Root.php +++ b/src/Root.php @@ -113,13 +113,9 @@ public function boot(): void $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}', $this->getPath()) => static fn(Request $request): string => $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}', $this->getPath()) => static fn(Request $request): string => $request->route('_resource')->modelTitle($request->route('resourceModel')), sprintf('%s/resources/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'), ]); } diff --git a/src/RootServiceProvider.php b/src/RootServiceProvider.php index 5acab0ac..8a03f174 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')); } /** @@ -151,11 +147,9 @@ protected function registerRoutes(): void } }); - $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/Traits/HasAttributes.php b/src/Traits/HasAttributes.php index 68247b4d..301ef2c0 100644 --- a/src/Traits/HasAttributes.php +++ b/src/Traits/HasAttributes.php @@ -135,9 +135,7 @@ public function resolveAttributes(): array { return array_reduce( array_keys($this->attributes), - function (array $attributes, string $key): array { - 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/ResolvesFields.php b/src/Traits/ResolvesFields.php index b996dcc2..40c5a4d8 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..01a9ade0 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/Widgets/Value.php b/src/Widgets/Value.php index 0ac36897..6deb14b4 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') ); } From ea305d947c56fbffa9f853a9a280d1abadaa4f68 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 14:09:21 +0100 Subject: [PATCH 18/20] style --- src/Actions/Action.php | 4 +-- src/Actions/Actions.php | 2 +- src/Actions/SendVerificationNotification.php | 2 +- src/Fields/BelongsToMany.php | 22 +++++++-------- src/Fields/Boolean.php | 2 +- src/Fields/Date.php | 2 +- src/Fields/Dropdown.php | 4 +-- src/Fields/Editor.php | 2 +- src/Fields/Field.php | 2 +- src/Fields/Fields.php | 8 +++--- src/Fields/Fieldset.php | 2 +- src/Fields/File.php | 4 +-- src/Fields/HasOneOrMany.php | 2 +- src/Fields/Media.php | 4 +-- src/Fields/Meta.php | 4 +-- src/Fields/MorphTo.php | 2 +- src/Fields/MorphToMany.php | 16 +++++------ src/Fields/Relation.php | 28 ++++++++++---------- src/Fields/Repeater.php | 6 ++--- src/Fields/Select.php | 2 +- src/Fields/Slug.php | 4 +-- src/Fields/URL.php | 2 +- src/Filters/Filters.php | 6 ++--- src/Filters/Search.php | 2 +- src/Filters/Select.php | 2 +- src/Filters/Sort.php | 2 +- src/Models/Medium.php | 8 +++--- src/Models/Notification.php | 6 ++--- src/Models/User.php | 2 +- src/Resources/Resource.php | 16 +++++------ src/Resources/Resources.php | 2 +- src/Root.php | 4 +-- src/RootServiceProvider.php | 12 ++++----- src/Traits/HasAttributes.php | 4 +-- src/Traits/ResolvesFields.php | 2 +- src/Traits/ResolvesVisibility.php | 4 +-- src/Widgets/Value.php | 2 +- 37 files changed, 100 insertions(+), 100 deletions(-) diff --git a/src/Actions/Action.php b/src/Actions/Action.php index 73b56379..b3e7e1cc 100644 --- a/src/Actions/Action.php +++ b/src/Actions/Action.php @@ -134,10 +134,10 @@ protected function resolveField(Request $request, Field $field): void { $field->setAttribute('form', $this->getKey()); $field->id($this->getKey().'-'.$field->getAttribute('id')); - $field->resolveErrorsUsing(fn(Request $request): MessageBag => $this->errors($request)); + $field->resolveErrorsUsing(fn (Request $request): MessageBag => $this->errors($request)); if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn(): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value()); + $field->resolveRouteKeyNameUsing(fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value()); } } diff --git a/src/Actions/Actions.php b/src/Actions/Actions.php index ca1ddb8c..5e7fcea7 100644 --- a/src/Actions/Actions.php +++ b/src/Actions/Actions.php @@ -44,7 +44,7 @@ public function visible(string|array $context): static */ public function standalone(bool $value = true): static { - return $this->filter(static fn(Action $action): bool => $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 6996166a..ed1b876b 100644 --- a/src/Actions/SendVerificationNotification.php +++ b/src/Actions/SendVerificationNotification.php @@ -13,7 +13,7 @@ class SendVerificationNotification extends Action */ public function handle(Request $request, Collection $models): void { - $models->reject(static fn(User $user): bool => $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/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 4eee98b7..977c6308 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -67,21 +67,21 @@ public function getRelation(Model $model): EloquentRelation public function fields(Request $request): array { return [ - BelongsTo::make($this->getRelatedName(), 'related', static fn(Pivot $model): BelongsToRelation => $model->belongsTo( + 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( + )->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 - ); - })->display(fn(Model $model): ?string => $this->resolveDisplay($model)), + $model->setAttribute( + $this->getRelation($model->pivotParent)->getRelatedPivotKeyName(), + $value + ); + })->display(fn (Model $model): ?string => $this->resolveDisplay($model)), ]; } @@ -107,7 +107,7 @@ protected function resolveField(Request $request, Field $field): void } if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn(): string => 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); @@ -136,7 +136,7 @@ public function withPivotFields(Closure $callback): static $field->setModelAttribute($attribute) ->name($attribute) ->id($attribute) - ->value(fn(): mixed => $related->getRelation($this->getRelation($model)->getPivotAccessor())->getAttribute($key)); + ->value(fn (): mixed => $related->getRelation($this->getRelation($model)->getPivotAccessor())->getAttribute($key)); }); return $fields; @@ -162,7 +162,7 @@ public function mergePivotValues(array $value): array { $value = array_is_list($value) ? array_fill_keys($value, []) : $value; - return array_map(fn(array $pivot): array => 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 5d0aabd7..3c70938b 100644 --- a/src/Fields/Boolean.php +++ b/src/Fields/Boolean.php @@ -60,7 +60,7 @@ public function resolveValue(Request $request, Model $model): bool public function resolveFormat(Request $request, Model $model): ?string { if (is_null($this->formatResolver)) { - $this->formatResolver = static fn(Request $request, Model $model, ?bool $value): string => sprintf( + $this->formatResolver = static fn (Request $request, Model $model, ?bool $value): string => sprintf( '%s', $value ? 'status--success' : 'status--danger', $value ? __('Yes') : __('No') diff --git a/src/Fields/Date.php b/src/Fields/Date.php index c7eec3e4..baf66ed8 100644 --- a/src/Fields/Date.php +++ b/src/Fields/Date.php @@ -132,7 +132,7 @@ public function getValue(Model $model): mixed public function resolveFormat(Request $request, Model $model): ?string { if (is_null($this->formatResolver)) { - $this->formatResolver = fn(Request $request, Model $model, mixed $value): ?string => 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 1038c84e..4b014901 100644 --- a/src/Fields/Dropdown.php +++ b/src/Fields/Dropdown.php @@ -33,12 +33,12 @@ public function toInput(Request $request, Model $model): array $data = parent::toInput($request, $model); return array_merge($data, [ - 'options' => array_map(static fn(array $option): array => array_merge($option, [ + '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 fn(array $option): array => array_merge($option, [ + ->map(static fn (array $option): array => array_merge($option, [ 'html' => View::make('root::fields.dropdown-option', $option)->render(), ])) ->values() diff --git a/src/Fields/Editor.php b/src/Fields/Editor.php index 8b69a7b8..b8d62f74 100644 --- a/src/Fields/Editor.php +++ b/src/Fields/Editor.php @@ -119,7 +119,7 @@ protected function newMediaField(): Media { public function __construct(string $modelAttribute) { - parent::__construct(__('Media'), $modelAttribute.'-media', static fn(): MorphToMany => new MorphToMany( + parent::__construct(__('Media'), $modelAttribute.'-media', static fn (): MorphToMany => new MorphToMany( Medium::proxy()->newQuery(), new class extends Model { diff --git a/src/Fields/Field.php b/src/Fields/Field.php index a868ae82..447ab9af 100644 --- a/src/Fields/Field.php +++ b/src/Fields/Field.php @@ -661,7 +661,7 @@ public function toValidate(Request $request, Model $model): array $key = $model->exists ? 'update' : 'create'; $rules = array_map( - static fn(array|Closure $rule): array => 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 29231127..ecbf4c36 100644 --- a/src/Fields/Fields.php +++ b/src/Fields/Fields.php @@ -72,7 +72,7 @@ public function sortable(): static */ public function relation(): static { - return $this->filter(static fn(Field $field): bool => $field instanceof Relation); + return $this->filter(static fn (Field $field): bool => $field instanceof Relation); } /** @@ -80,7 +80,7 @@ public function relation(): static */ public function translatable(): static { - return $this->filter(static fn(Field $field): bool => $field->isTranslatable()); + return $this->filter(static fn (Field $field): bool => $field->isTranslatable()); } /** @@ -88,7 +88,7 @@ public function translatable(): static */ public function subResource(bool $value = true): static { - return $this->filter(static fn(Field $field): bool => $value + return $this->filter(static fn (Field $field): bool => $value ? $field instanceof Relation && $field->isSubResource() : ! $field instanceof Relation || ! $field->isSubResource()); } @@ -98,7 +98,7 @@ public function subResource(bool $value = true): static */ public function mapToValidate(Request $request, Model $model): array { - return $this->reduce(static fn(array $rules, Field $field): array => 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 46790d6e..1e5880d2 100644 --- a/src/Fields/Fieldset.php +++ b/src/Fields/Fieldset.php @@ -47,7 +47,7 @@ protected function resolveField(Request $request, Field $field): void $field->resolveErrorsUsing($this->errorsResolver); if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn(): string => 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 12e25446..bfb51963 100644 --- a/src/Fields/File.php +++ b/src/Fields/File.php @@ -102,7 +102,7 @@ public function collection(string $value): static public function resolveDisplay(Model $related): ?string { if (is_null($this->displayResolver)) { - $this->display(fn(Medium $related): string => $related->isImage + $this->display(fn (Medium $related): string => $related->isImage ? sprintf('%s', $related->getUrl($this->displayConversion), $related->name) : $related->file_name); } @@ -212,7 +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(fn(UploadedFile $file): int => $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 2e4b27b8..c4c11383 100644 --- a/src/Fields/HasOneOrMany.php +++ b/src/Fields/HasOneOrMany.php @@ -55,7 +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(fn(Builder $query): Builder => $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 0ed9ff1e..e6511b19 100644 --- a/src/Fields/Media.php +++ b/src/Fields/Media.php @@ -181,12 +181,12 @@ public function toInput(Request $request, Model $model): array 'chunk_size' => Config::get('root.media.chunk_size'), 'query' => $filters->mapToData($request), ], - 'selection' => array_map(static fn(array $option): array => array_merge($option, [ + '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(fn(RenderableFilter $filter): array => $filter->toField() + ->map(fn (RenderableFilter $filter): array => $filter->toField() ->removeAttribute('name') ->setAttributes([ 'x-model.debounce.300ms' => $filter->getKey(), diff --git a/src/Fields/Meta.php b/src/Fields/Meta.php index c211d27f..7bfe2e71 100644 --- a/src/Fields/Meta.php +++ b/src/Fields/Meta.php @@ -67,7 +67,7 @@ public function as(string $field, ?Closure $callback = null): static { $this->field = new $field($this->label, $this->getModelAttribute()); - $this->field->value(fn(Request $request, Model $model): mixed => $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]); @@ -113,7 +113,7 @@ public function getValue(Model $model): mixed public function resolveValue(Request $request, Model $model): mixed { if (is_null($this->valueResolver)) { - $this->valueResolver = static fn(Request $request, Model $model, mixed $value): mixed => $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 075bd34b..766340ba 100644 --- a/src/Fields/MorphTo.php +++ b/src/Fields/MorphTo.php @@ -98,7 +98,7 @@ public function routes(Router $router): void public function toArray(): array { return array_merge(parent::toArray(), [ - 'types' => array_map(static fn(string $type): string => __(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 a278ddf5..c0cd5053 100644 --- a/src/Fields/MorphToMany.php +++ b/src/Fields/MorphToMany.php @@ -28,21 +28,21 @@ public function getRelation(Model $model): EloquentRelation public function fields(Request $request): array { return [ - MorphTo::make($this->getRelatedName(), 'related', static fn(MorphPivot $model): MorphToRelation => $model->morphTo( + 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( + )->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(fn(Model $model): ?string => $this->resolveDisplay($model)), + $model->setAttribute( + $this->getRelation($model->pivotParent)->getRelatedPivotKeyName(), + $value + ); + })->display(fn (Model $model): ?string => $this->resolveDisplay($model)), ]; } } diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index ce2b3a5b..8d01e5f0 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -198,7 +198,7 @@ public function resolveRouteKeyNameUsing(Closure $callback): static public function getRouteKeyName(): string { $callback = is_null($this->routeKeyNameResolver) - ? fn(): string => 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); @@ -332,7 +332,7 @@ public function isTranslatable(): bool public function display(Closure|string $callback): static { if (is_string($callback)) { - $callback = static fn(Model $model) => $model->getAttribute($callback); + $callback = static fn (Model $model) => $model->getAttribute($callback); } $this->displayResolver = $callback; @@ -383,7 +383,7 @@ public function resolveFormat(Request $request, Model $model): ?string return $default; } - return Collection::wrap($default)->map(fn(Model $related): ?string => $this->formatRelated($request, $model, $related))->filter()->join(', '); + return Collection::wrap($default)->map(fn (Model $related): ?string => $this->formatRelated($request, $model, $related))->filter()->join(', '); }; } @@ -437,7 +437,7 @@ protected function resolveField(Request $request, Field $field): void } if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn(): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value()); + $field->resolveRouteKeyNameUsing(fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value()); } } @@ -486,7 +486,7 @@ public function resolveRelatableQuery(Request $request, Model $model): Builder $query = call_user_func_array($scope, [$request, $query, $model]); } - return $query->when(! is_null($this->queryResolver), fn(Builder $query): Builder => 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])); } /** @@ -538,11 +538,11 @@ public function resolveOptions(Request $request, Model $model): array { return $this->resolveRelatableQuery($request, $model) ->get() - ->when(! is_null($this->groupResolver), fn(Collection $collection): Collection => $collection->groupBy($this->groupResolver) - ->map(fn(Collection $group, string $key): array => [ + ->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))) + '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(); } @@ -755,7 +755,7 @@ protected function routesRegistered(Request $request): void Root::instance()->breadcrumbs->patterns([ $this->getUri() => $this->label, sprintf('%s/create', $this->getUri()) => __('Add'), - sprintf('%s/{%s}', $this->getUri(), $this->getRouteKeyName()) => fn(Request $request): string => $this->resolveDisplay($request->route($this->getRouteKeyName())), + sprintf('%s/{%s}', $this->getUri(), $this->getRouteKeyName()) => fn (Request $request): string => $this->resolveDisplay($request->route($this->getRouteKeyName())), sprintf('%s/{%s}/edit', $this->getUri(), $this->getRouteKeyName()) => __('Edit'), ]); } @@ -853,7 +853,7 @@ public function routes(Router $router): void */ public function registerRouteConstraints(Request $request, Router $router): void { - $router->bind($this->getRouteKeyName(), fn(string $id, Route $route): Model => match ($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), }); @@ -868,7 +868,7 @@ public function parseQueryString(string $url): array parse_str($query, $result); - return array_filter($result, fn(string $key): bool => 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); } /** @@ -926,14 +926,14 @@ public function toIndex(Request $request, Model $model): array ->visible('index') ->standalone(false) ->mapToForms($request, $model), - 'data' => $this->paginate($request, $model)->through(fn(Model $related): array => $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 fn(RenderableFilter $filter): array => $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 e7bea67c..1f469195 100644 --- a/src/Fields/Repeater.php +++ b/src/Fields/Repeater.php @@ -114,7 +114,7 @@ protected function resolveField(Request $request, Field $field): void ); if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn(): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getModelAttribute())->value()); + $field->resolveRouteKeyNameUsing(fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getModelAttribute())->value()); } } @@ -183,7 +183,7 @@ public function resolveOptions(Request $request, Model $model): array { $value = (array) $this->resolveValue($request, $model); - return array_map(fn(array $option): array => $this->toOption($request, $model, $this->newTemporaryModel($option)), $value); + return array_map(fn (array $option): array => $this->toOption($request, $model, $this->newTemporaryModel($option)), $value); } /** @@ -215,7 +215,7 @@ 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(fn(array $value): array => $this->resolveOptionFields($request, $model, $this->newTemporaryModel($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); diff --git a/src/Fields/Select.php b/src/Fields/Select.php index 04c857d9..29d53a5b 100644 --- a/src/Fields/Select.php +++ b/src/Fields/Select.php @@ -89,7 +89,7 @@ public function resolveFormat(Request $request, Model $model): ?string */ public function options(array|Closure $value): static { - $this->optionsResolver = is_callable($value) ? $value : static fn(): array => $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 0bedff9d..8973136f 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -148,7 +148,7 @@ public function unique(bool $value = true): static $this->unique = $value; if ($value) { - $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)]); + $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; @@ -194,7 +194,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 fn(array $match): string => 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 2bf5e45f..4df440df 100644 --- a/src/Fields/URL.php +++ b/src/Fields/URL.php @@ -42,7 +42,7 @@ public function text(Closure|string $value): static public function resolveFormat(Request $request, Model $model): ?string { if (is_null($this->formatResolver)) { - $this->formatResolver = fn(Request $request, Model $model, mixed $value): ?string => is_null($value) ? $value : sprintf( + $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"' : '', diff --git a/src/Filters/Filters.php b/src/Filters/Filters.php index 711b2840..7fa12566 100644 --- a/src/Filters/Filters.php +++ b/src/Filters/Filters.php @@ -48,7 +48,7 @@ public function apply(Request $request, Builder $query): Builder */ public function renderable(): static { - return $this->filter(static fn(Filter $filter): bool => $filter instanceof RenderableFilter); + return $this->filter(static fn (Filter $filter): bool => $filter instanceof RenderableFilter); } /** @@ -56,7 +56,7 @@ public function renderable(): static */ public function functional(): static { - return $this->reject(static fn(Filter $filter): bool => $filter instanceof RenderableFilter); + return $this->reject(static fn (Filter $filter): bool => $filter instanceof RenderableFilter); } /** @@ -72,6 +72,6 @@ public function active(Request $request): static */ public function mapToData(Request $request): array { - return $this->mapWithKeys(static fn(Filter $filter): array => [$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 b4b0943c..aba98e31 100644 --- a/src/Filters/Search.php +++ b/src/Filters/Search.php @@ -62,7 +62,7 @@ public function apply(Request $request, Builder $query, mixed $value): Builder */ public function getSearchableAttributes(): array { - return $this->fields->mapWithKeys(static fn(Field $field): array => [ + 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 4aa47789..a89d942c 100644 --- a/src/Filters/Select.php +++ b/src/Filters/Select.php @@ -53,7 +53,7 @@ public function isMultiple(): bool public function toField(): Field { return Field::make($this->getName(), $this->getRequestKey()) - ->options(App::call(fn(Request $request): array => $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 25c0014d..fca746ee 100644 --- a/src/Filters/Sort.php +++ b/src/Filters/Sort.php @@ -34,7 +34,7 @@ public function apply(Request $request, Builder $query, mixed $value): Builder { $value = array_replace(['by' => 'id', 'order' => 'desc'], (array) $value); - $attributes = $this->fields->mapWithKeys(static fn(Field $field): array => [ + $attributes = $this->fields->mapWithKeys(static fn (Field $field): array => [ $field->getModelAttribute() => $field instanceof Relation ? $field->getSortableColumn() : null, ])->all(); diff --git a/src/Models/Medium.php b/src/Models/Medium.php index 43ed824a..2dab9b3b 100644 --- a/src/Models/Medium.php +++ b/src/Models/Medium.php @@ -196,7 +196,7 @@ public function user(): BelongsTo protected function isImage(): Attribute { return new Attribute( - get: static fn(mixed $value, array $attributes): bool => Str::is('image/*', $attributes['mime_type']) + get: static fn (mixed $value, array $attributes): bool => Str::is('image/*', $attributes['mime_type']) ); } @@ -208,9 +208,9 @@ protected function isImage(): Attribute protected function urls(): Attribute { return new Attribute( - get: fn(): array => array_reduce( + get: fn (): array => array_reduce( $this->properties['conversions'] ?? [], - fn(array $urls, string $conversion): array => array_merge($urls, [$conversion => $this->getUrl($conversion)]), + fn (array $urls, string $conversion): array => array_merge($urls, [$conversion => $this->getUrl($conversion)]), ['original' => $this->getUrl()] ) ); @@ -236,7 +236,7 @@ protected function formattedSize(): Attribute protected function dimensions(): Attribute { return new Attribute( - get: static fn(mixed $value, array $attributes): ?string => isset($attributes['width'], $attributes['height']) + 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 2222fabf..da03bca2 100644 --- a/src/Models/Notification.php +++ b/src/Models/Notification.php @@ -68,7 +68,7 @@ public function getMorphClass(): string protected function formattedCreatedAt(): Attribute { return new Attribute( - get: fn(): ?string => $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') ); } @@ -80,7 +80,7 @@ protected function formattedCreatedAt(): Attribute protected function isRead(): Attribute { return new Attribute( - get: fn(): bool => ! is_null($this->read_at) + get: fn (): bool => ! is_null($this->read_at) ); } @@ -92,7 +92,7 @@ protected function isRead(): Attribute protected function url(): Attribute { return new Attribute( - get: fn(): ?string => $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 d063ece0..d448ed96 100644 --- a/src/Models/User.php +++ b/src/Models/User.php @@ -135,7 +135,7 @@ public function generateDeviceToken(Request $request): string protected function avatar(): Attribute { return new Attribute( - get: static fn(mixed $value, array $attributes): ?string => isset($attributes['email']) + 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/Resources/Resource.php b/src/Resources/Resource.php index 2739dda3..b47900e9 100644 --- a/src/Resources/Resource.php +++ b/src/Resources/Resource.php @@ -282,7 +282,7 @@ public function resolveRouteBindingQuery(Request $request): Builder ->withoutEagerLoads() ->when( $this->isSoftDeletable(), - static fn(Builder $query): Builder => $query->withTrashed() + static fn (Builder $query): Builder => $query->withTrashed() ); } @@ -349,9 +349,9 @@ public function resolveTranslationsField(Request $request): ?Translations { return $this->translatable() ? Translations::make() - ->withFields(fn(): array => $this->resolveFields($request) + ->withFields(fn (): array => $this->resolveFields($request) ->translatable() - ->map(static fn(Field $field): Field => (clone $field) + ->map(static fn (Field $field): Field => (clone $field) ->translatable(false) ->setModelAttribute($key = 'values->'.$field->getModelAttribute()) ->name($key) @@ -366,7 +366,7 @@ public function resolveTranslationsField(Request $request): ?Translations public function resolveFields(Request $request): Fields { if (is_null($this->fields)) { - $this->withFields(fn(): array => array_values(array_filter([ + $this->withFields(fn (): array => array_values(array_filter([ $this->resolveTranslationsField($request), $this->resolveEventsField($request), ]))); @@ -402,7 +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(fn(): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value()); + $field->resolveRouteKeyNameUsing(fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getKey())->value()); } } @@ -485,7 +485,7 @@ public function paginate(Request $request): LengthAwarePaginator ->latest() ->paginate($request->input($this->getPerPageKey())) ->withQueryString() - ->through(fn(Model $model): array => $this->mapModel($request, $model)); + ->through(fn (Model $model): array => $this->mapModel($request, $model)); } /** @@ -653,7 +653,7 @@ public function toIndex(Request $request): array 'filters' => $this->resolveFilters($request) ->authorized($request) ->renderable() - ->map(static fn(RenderableFilter $filter): array => $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(), @@ -709,7 +709,7 @@ public function toShow(Request $request, Model $model): array 'relations' => $this->resolveFields($request) ->subResource() ->authorized($request, $model) - ->map(static fn(Relation $relation): array => array_merge($relation->toSubResource($request, $model), [ + ->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( diff --git a/src/Resources/Resources.php b/src/Resources/Resources.php index 41c66779..5026d667 100644 --- a/src/Resources/Resources.php +++ b/src/Resources/Resources.php @@ -65,7 +65,7 @@ public function forModel(string|Model $model): ?Resource { $model = is_string($model) ? $model : $model::class; - return $this->first(static fn(Resource $resource): bool => $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 a48b6da8..d25b2309 100644 --- a/src/Root.php +++ b/src/Root.php @@ -113,9 +113,9 @@ public function boot(): void $this->breadcrumbs->patterns([ $this->getPath() => __('Dashboard'), - sprintf('%s/resources/{resource}', $this->getPath()) => static fn(Request $request): string => $request->route('_resource')->getName(), + sprintf('%s/resources/{resource}', $this->getPath()) => static fn (Request $request): string => $request->route('_resource')->getName(), sprintf('%s/resources/{resource}/create', $this->getPath()) => __('Create'), - sprintf('%s/resources/{resource}/{resourceModel}', $this->getPath()) => static fn(Request $request): string => $request->route('_resource')->modelTitle($request->route('resourceModel')), + sprintf('%s/resources/{resource}/{resourceModel}', $this->getPath()) => static fn (Request $request): string => $request->route('_resource')->modelTitle($request->route('resourceModel')), sprintf('%s/resources/{resource}/{resourceModel}/edit', $this->getPath()) => __('Edit'), ]); } diff --git a/src/RootServiceProvider.php b/src/RootServiceProvider.php index 8a03f174..0eb3327d 100644 --- a/src/RootServiceProvider.php +++ b/src/RootServiceProvider.php @@ -63,7 +63,7 @@ class RootServiceProvider extends ServiceProvider */ public function register(): void { - $this->app->singleton(Root::class, static fn(Application $app): Root => new Root($app)); + $this->app->singleton(Root::class, static fn (Application $app): Root => new Root($app)); $this->app->alias(Root::class, 'root'); @@ -79,7 +79,7 @@ public function register(): void $app->make(Root::class)->boot(); }); - $this->app['request']->macro('isTurboFrameRequest', fn(): bool => $this->hasHeader('Turbo-Frame')); + $this->app['request']->macro('isTurboFrameRequest', fn (): bool => $this->hasHeader('Turbo-Frame')); } /** @@ -147,7 +147,7 @@ protected function registerRoutes(): void } }); - $this->app['router']->bind('resourceModel', fn(string $id, Route $route): Model => $id === 'create' + $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)); @@ -168,7 +168,7 @@ protected function registerRoutes(): void $this->loadRoutesFrom(__DIR__.'/../routes/web.php'); }); - RateLimiter::for('root.auth', static fn(Request $request): Limit => 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())); } /** @@ -219,7 +219,7 @@ protected function registerExceptions(): void { $exceptions = $this->app->make(ExceptionHandler::class); - $exceptions->renderable(static fn(SaveFormDataException $exception): RedirectResponse => Redirect::back() + $exceptions->renderable(static fn (SaveFormDataException $exception): RedirectResponse => Redirect::back() ->withInput() ->with('alerts.form-save', Alert::error($exception->getMessage()))); } @@ -229,7 +229,7 @@ protected function registerExceptions(): void */ protected function registerAuth(): void { - Gate::define('viewRoot', static fn(User $user): bool => 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/Traits/HasAttributes.php b/src/Traits/HasAttributes.php index 301ef2c0..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' => (string)$this->classList(), + 'class' => (string) $this->classList(), default => $this->attributes[$key] ?? $default, }; } @@ -135,7 +135,7 @@ public function resolveAttributes(): array { return array_reduce( array_keys($this->attributes), - fn(array $attributes, string $key): array => 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/ResolvesFields.php b/src/Traits/ResolvesFields.php index 40c5a4d8..922c8f07 100644 --- a/src/Traits/ResolvesFields.php +++ b/src/Traits/ResolvesFields.php @@ -48,7 +48,7 @@ public function hasFileField(Request $request): bool return $this->resolveFields($request) ->subResource(false) ->visible(['update', 'create']) - ->some(static fn(Field $field): bool => ($field instanceof File && ! $field instanceof Media) + ->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 01a9ade0..463ff16b 100644 --- a/src/Traits/ResolvesVisibility.php +++ b/src/Traits/ResolvesVisibility.php @@ -33,7 +33,7 @@ public function visibleOn(string|array|Closure $context): static { $this->visibilityResolvers[] = $context instanceof Closure ? $context - : static fn(string|array $currentContext) => ! 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; } @@ -43,7 +43,7 @@ public function visibleOn(string|array|Closure $context): static */ public function hiddenOn(string|array|Closure $context): static { - return $this->visibleOn(static fn(array|string $currentContext): bool => $context instanceof Closure + 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/Widgets/Value.php b/src/Widgets/Value.php index 6deb14b4..27a04262 100644 --- a/src/Widgets/Value.php +++ b/src/Widgets/Value.php @@ -51,7 +51,7 @@ protected function aggregate(Builder $query, DatePeriod $period, string $fn, str return parent::aggregate($query, $extended, $fn, $column, $dateColumn)->when( $period->getStartDate()->getTimestamp() > 0, - fn(Builder $query): Builder => $query->selectRaw(sprintf( + 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'), From 9d73788843ed6e754b22e99f055a7f88f5a47dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Viktor=20Sz=C3=A9pe?= Date: Fri, 31 Jan 2025 16:44:04 +0100 Subject: [PATCH 19/20] fix CI --- .github/workflows/back-end.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 }}" From e5508676d43a14015f8689f4589f59c7e2ffcdd5 Mon Sep 17 00:00:00 2001 From: Mark <14284867+xHeaven@users.noreply.github.com> Date: Fri, 31 Jan 2025 17:11:05 +0100 Subject: [PATCH 20/20] fix most of eclint issues --- src/Fields/BelongsToMany.php | 25 ++++++++++++++----------- src/Fields/Fieldset.php | 4 +++- src/Fields/MorphTo.php | 4 +++- src/Fields/MorphToMany.php | 13 ++++++++----- src/Fields/Relation.php | 16 +++++++++++----- src/Fields/Repeater.php | 4 +++- src/Fields/Slug.php | 4 +++- src/Root.php | 12 +++++++----- 8 files changed, 52 insertions(+), 30 deletions(-) diff --git a/src/Fields/BelongsToMany.php b/src/Fields/BelongsToMany.php index 977c6308..784b170b 100644 --- a/src/Fields/BelongsToMany.php +++ b/src/Fields/BelongsToMany.php @@ -72,16 +72,17 @@ public function fields(Request $request): array $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 - ); - })->display(fn (Model $model): ?string => $this->resolveDisplay($model)), + )->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 + ); + })->display(fn (Model $model): ?string => $this->resolveDisplay($model)), ]; } @@ -107,7 +108,9 @@ protected function resolveField(Request $request, Field $field): void } if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn (): string => 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); diff --git a/src/Fields/Fieldset.php b/src/Fields/Fieldset.php index 1e5880d2..a7365046 100644 --- a/src/Fields/Fieldset.php +++ b/src/Fields/Fieldset.php @@ -47,7 +47,9 @@ protected function resolveField(Request $request, Field $field): void $field->resolveErrorsUsing($this->errorsResolver); if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn (): string => 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/MorphTo.php b/src/Fields/MorphTo.php index 766340ba..a7ba040c 100644 --- a/src/Fields/MorphTo.php +++ b/src/Fields/MorphTo.php @@ -98,7 +98,9 @@ public function routes(Router $router): void public function toArray(): array { return array_merge(parent::toArray(), [ - 'types' => array_map(static fn (string $type): string => __(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 c0cd5053..2a9671aa 100644 --- a/src/Fields/MorphToMany.php +++ b/src/Fields/MorphToMany.php @@ -33,11 +33,14 @@ public function fields(Request $request): array $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 { + )->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 diff --git a/src/Fields/Relation.php b/src/Fields/Relation.php index 8d01e5f0..a7c41b56 100644 --- a/src/Fields/Relation.php +++ b/src/Fields/Relation.php @@ -437,7 +437,9 @@ protected function resolveField(Request $request, Field $field): void } if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value()); + $field->resolveRouteKeyNameUsing( + fn (): string => Str::of($field->getRelationName())->singular()->ucfirst()->prepend($this->getRouteKeyName())->value() + ); } } @@ -486,7 +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), fn (Builder $query): Builder => 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])); } /** @@ -752,11 +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()) => fn (Request $request): string => $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'), ]); } diff --git a/src/Fields/Repeater.php b/src/Fields/Repeater.php index 1f469195..876140c3 100644 --- a/src/Fields/Repeater.php +++ b/src/Fields/Repeater.php @@ -114,7 +114,9 @@ protected function resolveField(Request $request, Field $field): void ); if ($field instanceof Relation) { - $field->resolveRouteKeyNameUsing(fn (): string => 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/Slug.php b/src/Fields/Slug.php index 8973136f..b6924d24 100644 --- a/src/Fields/Slug.php +++ b/src/Fields/Slug.php @@ -148,7 +148,9 @@ public function unique(bool $value = true): static $this->unique = $value; if ($value) { - $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)]); + $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; diff --git a/src/Root.php b/src/Root.php index d25b2309..8bef4738 100644 --- a/src/Root.php +++ b/src/Root.php @@ -111,12 +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 fn (Request $request): string => $request->route('_resource')->getName(), - sprintf('%s/resources/{resource}/create', $this->getPath()) => __('Create'), - sprintf('%s/resources/{resource}/{resourceModel}', $this->getPath()) => static fn (Request $request): string => $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'), ]); }