From 68d18311b4ebde11f64ed3f0bf96fa8ada528f99 Mon Sep 17 00:00:00 2001 From: Rico van Zelst Date: Tue, 4 Mar 2025 12:50:19 +0100 Subject: [PATCH] feat: refactor code and improve readability --- app/Console/Commands/UserCreateCommand.php | 6 ++---- app/Helpers/HelperFunctions.php | 6 ++---- .../Controllers/ContactSubmissionController.php | 9 ++++++--- app/Http/Controllers/PostsController.php | 4 +--- app/Http/Controllers/SaleController.php | 6 +++--- app/Models/ChampionRoles.php | 2 +- app/Models/Streamer.php | 10 +++++----- app/Providers/AppServiceProvider.php | 16 +++++----------- 8 files changed, 25 insertions(+), 34 deletions(-) diff --git a/app/Console/Commands/UserCreateCommand.php b/app/Console/Commands/UserCreateCommand.php index a352450..d040ea6 100644 --- a/app/Console/Commands/UserCreateCommand.php +++ b/app/Console/Commands/UserCreateCommand.php @@ -13,10 +13,8 @@ class UserCreateCommand extends Command public function handle(): void { - if (config('app.env') === 'production') { - if (! $this->confirm('You are in production mode. Are you sure you want to continue?')) { - return; - } + if (config('app.env') === 'production' && ! $this->confirm('You are in production mode. Are you sure you want to continue?')) { + return; } $this->info('Creating a new user...'); diff --git a/app/Helpers/HelperFunctions.php b/app/Helpers/HelperFunctions.php index 1a02536..856733e 100644 --- a/app/Helpers/HelperFunctions.php +++ b/app/Helpers/HelperFunctions.php @@ -24,7 +24,7 @@ function getAverageColorFromImageUrl($imageUrl): string try { $img = $imgManager->read(file_get_contents($imageUrl)); - } catch (Exception $e) { + } catch (Exception) { return '#904f2c'; } @@ -90,9 +90,7 @@ function getChampionImage($full_id, $type): string function getCommitHash(): string { /** @var string $commit */ - $commit = Cache::remember('commit_hash', 60 * 72, function () { - return trim(exec('git log --pretty="%h" -n1 HEAD')); - }); + $commit = Cache::remember('commit_hash', 60 * 72, fn() => trim(exec('git log --pretty="%h" -n1 HEAD'))); return $commit; } diff --git a/app/Http/Controllers/ContactSubmissionController.php b/app/Http/Controllers/ContactSubmissionController.php index 391cb12..ada2cbf 100644 --- a/app/Http/Controllers/ContactSubmissionController.php +++ b/app/Http/Controllers/ContactSubmissionController.php @@ -20,12 +20,15 @@ class ContactSubmissionController extends Controller $descriptionContent = "**Name**: {$contactSubmission->name}\n\n**Email**: {$contactSubmission->email}\n\n**Category**: {$contactSubmission->category->humanReadable()}\n\n**Subject**: {$contactSubmission->subject}\n\n**Message**: {$contactSubmission->message}"; if ($contactSubmission->discord) { - $descriptionContent .= "\n\n\n**Discord**: {$contactSubmission->discord}"; + $descriptionContent .= ' + + +**Discord**: ' . $contactSubmission->discord; } - DiscordAlert::message("There is a new contact submission from {$contactSubmission->name} ({$contactSubmission->email}).", [ + DiscordAlert::message(sprintf('There is a new contact submission from %s (%s).', $contactSubmission->name, $contactSubmission->email), [ [ - 'title' => "{$contactSubmission->category->humanReadable()} - {$contactSubmission->subject}", + 'title' => sprintf('%s - %s', $contactSubmission->category->humanReadable(), $contactSubmission->subject), 'description' => $descriptionContent, 'color' => '#ff8a4c', ], diff --git a/app/Http/Controllers/PostsController.php b/app/Http/Controllers/PostsController.php index c1db647..3efd004 100644 --- a/app/Http/Controllers/PostsController.php +++ b/app/Http/Controllers/PostsController.php @@ -10,9 +10,7 @@ class PostsController extends Controller { public function index() { - $posts = Sheets::all()->filter(function ($post) { - return ! $post->hidden; - })->sortByDesc('date'); + $posts = Sheets::all()->filter(fn($post) => ! $post->hidden)->sortByDesc('date'); $paginatedPosts = Paginate::collection($posts, 6); return view('posts.index', [ diff --git a/app/Http/Controllers/SaleController.php b/app/Http/Controllers/SaleController.php index e4579d0..4ccf6c8 100644 --- a/app/Http/Controllers/SaleController.php +++ b/app/Http/Controllers/SaleController.php @@ -27,12 +27,12 @@ class SaleController extends Controller return $response; }); - } catch (\Exception $e) { - if ($e->getMessage() === 'Trying to access array offset on value of type null') { + } catch (\Exception $exception) { + if ($exception->getMessage() === 'Trying to access array offset on value of type null') { logger()->error('LMI has broken'); abort(503, 'Sorry, the Sale Rotation is currently under maintenance. Please try again later.'); } else { - logger()->error('An error occurred while trying to fetch the Sale Rotation', ['error' => $e->getMessage()]); + logger()->error('An error occurred while trying to fetch the Sale Rotation', ['error' => $exception->getMessage()]); abort(500, 'Sorry, an error occurred while trying to fetch the Sale Rotation. Please try again later.'); } } diff --git a/app/Models/ChampionRoles.php b/app/Models/ChampionRoles.php index 821aa69..b482322 100644 --- a/app/Models/ChampionRoles.php +++ b/app/Models/ChampionRoles.php @@ -23,7 +23,7 @@ class ChampionRoles extends Model public function getRolesAttribute($value): array { - $value = json_decode($value); + $value = json_decode((string) $value); $roleNames = [ 'TOP' => 'Toplane', diff --git a/app/Models/Streamer.php b/app/Models/Streamer.php index 5ad7b52..40be5b7 100644 --- a/app/Models/Streamer.php +++ b/app/Models/Streamer.php @@ -28,11 +28,11 @@ class Streamer extends Model public function getStreamerUrlAttribute(): string { return match ($this->platform) { - 'Twitch' => "https://www.twitch.tv/{$this->username}", - 'YouTube' => "https://www.youtube.com/@{$this->username}", - 'Kick' => "https://kick.com/{$this->username}", - 'Douyu' => "https://www.douyu.com/{$this->username}", - 'Huya' => "https://www.huya.com/{$this->username}", + 'Twitch' => 'https://www.twitch.tv/' . $this->username, + 'YouTube' => 'https://www.youtube.com/@' . $this->username, + 'Kick' => 'https://kick.com/' . $this->username, + 'Douyu' => 'https://www.douyu.com/' . $this->username, + 'Huya' => 'https://www.huya.com/' . $this->username, }; } diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index a0a5bdd..599aa00 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -46,21 +46,15 @@ class AppServiceProvider extends ServiceProvider public function bootAuth(): void { - Gate::define('viewPulse', function (User $user) { - return $user->admin; - }); + Gate::define('viewPulse', fn(User $user) => $user->admin); } public function bootRoute(): void { - RateLimiter::for('api', function (Request $request) { - return Limit::perMinute(60)->by($request->user()?->id ?: $request->ip()); - }); + RateLimiter::for('api', fn(Request $request) => Limit::perMinute(60)->by($request->user()?->id ?: $request->ip())); - Route::bind('post', function ($path) { - return $this->app->make(Sheets::class) - ->collection('posts') - ->get($path) ?? abort(404); - }); + Route::bind('post', fn($path) => $this->app->make(Sheets::class) + ->collection('posts') + ->get($path) ?? abort(404)); } }