From 89b83033fe22887b185df6cdfb7fb81456ec6788 Mon Sep 17 00:00:00 2001 From: Andrew Brown Date: Mon, 14 Jul 2025 18:51:07 -0500 Subject: [PATCH] minor code formatting improvements utilize short closures and new line chaining to improve readability and prep for better diffs. no behavior change, purely formatting. --- src/Illuminate/Bus/Queueable.php | 8 ++--- src/Illuminate/Collections/LazyCollection.php | 6 ++-- .../Scheduling/ScheduleFinishCommand.php | 12 ++++---- .../Eloquent/Casts/AsEnumArrayObject.php | 6 ++-- .../Eloquent/Casts/AsEnumCollection.php | 6 ++-- .../Database/Query/Grammars/Grammar.php | 12 ++++---- .../Query/Grammars/PostgresGrammar.php | 6 ++-- .../Database/Query/Grammars/SQLiteGrammar.php | 16 +++++----- .../Query/Grammars/SqlServerGrammar.php | 12 ++++---- src/Illuminate/Database/Schema/Blueprint.php | 10 +++---- .../Console/ConfigPublishCommand.php | 4 +-- .../Exceptions/RegisterErrorViewPaths.php | 8 +++-- .../Testing/Concerns/MakesHttpRequests.php | 7 +++-- src/Illuminate/Log/LogManager.php | 26 ++++++++++------- src/Illuminate/Pagination/Cursor.php | 6 ++-- src/Illuminate/Process/Factory.php | 6 ++-- .../Failed/DynamoDbFailedJobProvider.php | 29 ++++++++++--------- .../Routing/ControllerDispatcher.php | 7 +++-- src/Illuminate/Routing/Router.php | 9 +++--- .../Testing/ParallelConsoleOutput.php | 5 ++-- .../Validation/Rules/DatabaseRule.php | 6 ++-- .../View/Compilers/BladeCompiler.php | 6 ++-- 22 files changed, 109 insertions(+), 104 deletions(-) diff --git a/src/Illuminate/Bus/Queueable.php b/src/Illuminate/Bus/Queueable.php index b8a439dad2ac..917f6540995e 100644 --- a/src/Illuminate/Bus/Queueable.php +++ b/src/Illuminate/Bus/Queueable.php @@ -204,11 +204,9 @@ public function through($middleware) */ public function chain($chain) { - $jobs = ChainedBatch::prepareNestedBatches(new Collection($chain)); - - $this->chained = $jobs->map(function ($job) { - return $this->serializeJob($job); - })->all(); + $this->chained = ChainedBatch::prepareNestedBatches(new Collection($chain)) + ->map(fn ($job) => $this->serializeJob($job)) + ->all(); return $this; } diff --git a/src/Illuminate/Collections/LazyCollection.php b/src/Illuminate/Collections/LazyCollection.php index 48e436c7335c..30188ff19f92 100644 --- a/src/Illuminate/Collections/LazyCollection.php +++ b/src/Illuminate/Collections/LazyCollection.php @@ -1766,9 +1766,9 @@ public function zip($items) $iterables = func_get_args(); return new static(function () use ($iterables) { - $iterators = (new Collection($iterables))->map(function ($iterable) { - return $this->makeIterator($iterable); - })->prepend($this->getIterator()); + $iterators = (new Collection($iterables)) + ->map(fn ($iterable) => $this->makeIterator($iterable)) + ->prepend($this->getIterator()); while ($iterators->contains->valid()) { yield new static($iterators->map->current()); diff --git a/src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php b/src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php index 575e590623b9..2fee1ac8fac3 100644 --- a/src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php +++ b/src/Illuminate/Console/Scheduling/ScheduleFinishCommand.php @@ -40,12 +40,12 @@ class ScheduleFinishCommand extends Command */ public function handle(Schedule $schedule) { - (new Collection($schedule->events()))->filter(function ($value) { - return $value->mutexName() == $this->argument('id'); - })->each(function ($event) { - $event->finish($this->laravel, $this->argument('code')); + (new Collection($schedule->events())) + ->filter(fn ($value) => $value->mutexName() == $this->argument('id')) + ->each(function ($event) { + $event->finish($this->laravel, $this->argument('code')); - $this->laravel->make(Dispatcher::class)->dispatch(new ScheduledBackgroundTaskFinished($event)); - }); + $this->laravel->make(Dispatcher::class)->dispatch(new ScheduledBackgroundTaskFinished($event)); + }); } } diff --git a/src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php b/src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php index 273089b2318a..061dcbf57e96 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsEnumArrayObject.php @@ -68,9 +68,9 @@ public function set($model, $key, $value, $attributes) public function serialize($model, string $key, $value, array $attributes) { - return (new Collection($value->getArrayCopy()))->map(function ($enum) { - return $this->getStorableEnumValue($enum); - })->toArray(); + return (new Collection($value->getArrayCopy())) + ->map(fn ($enum) => $this->getStorableEnumValue($enum)) + ->toArray(); } protected function getStorableEnumValue($enum) diff --git a/src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php b/src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php index 044c4578652c..fa7116a0d0ed 100644 --- a/src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php +++ b/src/Illuminate/Database/Eloquent/Casts/AsEnumCollection.php @@ -64,9 +64,9 @@ public function set($model, $key, $value, $attributes) public function serialize($model, string $key, $value, array $attributes) { - return (new Collection($value))->map(function ($enum) { - return $this->getStorableEnumValue($enum); - })->toArray(); + return (new Collection($value)) + ->map(fn ($enum) => $this->getStorableEnumValue($enum)) + ->toArray(); } protected function getStorableEnumValue($enum) diff --git a/src/Illuminate/Database/Query/Grammars/Grammar.php b/src/Illuminate/Database/Query/Grammars/Grammar.php index 07d23d665a5c..27effd7c4a34 100755 --- a/src/Illuminate/Database/Query/Grammars/Grammar.php +++ b/src/Illuminate/Database/Query/Grammars/Grammar.php @@ -1195,9 +1195,9 @@ public function compileInsert(Builder $query, array $values) // We need to build a list of parameter place-holders of values that are bound // to the query. Each insert should have the exact same number of parameter // bindings so we will loop through the record and parameterize them all. - $parameters = (new Collection($values))->map(function ($record) { - return '('.$this->parameterize($record).')'; - })->implode(', '); + $parameters = (new Collection($values)) + ->map(fn ($record) => '('.$this->parameterize($record).')') + ->implode(', '); return "insert into $table ($columns) values $parameters"; } @@ -1294,9 +1294,9 @@ public function compileUpdate(Builder $query, array $values) */ protected function compileUpdateColumns(Builder $query, array $values) { - return (new Collection($values))->map(function ($value, $key) { - return $this->wrap($key).' = '.$this->parameter($value); - })->implode(', '); + return (new Collection($values)) + ->map(fn ($value, $key) => $this->wrap($key).' = '.$this->parameter($value)) + ->implode(', '); } /** diff --git a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php index 9207fe54565f..615852e2ae7d 100755 --- a/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/PostgresGrammar.php @@ -167,9 +167,9 @@ public function whereFullText(Builder $query, $where) $language = 'english'; } - $columns = (new Collection($where['columns']))->map(function ($column) use ($language) { - return "to_tsvector('{$language}', {$this->wrap($column)})"; - })->implode(' || '); + $columns = (new Collection($where['columns'])) + ->map(fn ($column) => "to_tsvector('{$language}', {$this->wrap($column)})") + ->implode(' || '); $mode = 'plainto_tsquery'; diff --git a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php index 9fb8d8a31589..a70571d2ceb9 100755 --- a/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SQLiteGrammar.php @@ -289,15 +289,17 @@ protected function compileUpdateColumns(Builder $query, array $values) { $jsonGroups = $this->groupJsonColumnsForUpdate($values); - return (new Collection($values))->reject(function ($value, $key) { - return $this->isJsonSelector($key); - })->merge($jsonGroups)->map(function ($value, $key) use ($jsonGroups) { - $column = last(explode('.', $key)); + return (new Collection($values)) + ->reject(fn ($value, $key) => $this->isJsonSelector($key)) + ->merge($jsonGroups) + ->map(function ($value, $key) use ($jsonGroups) { + $column = last(explode('.', $key)); - $value = isset($jsonGroups[$key]) ? $this->compileJsonPatch($column, $value) : $this->parameter($value); + $value = isset($jsonGroups[$key]) ? $this->compileJsonPatch($column, $value) : $this->parameter($value); - return $this->wrap($column).' = '.$value; - })->implode(', '); + return $this->wrap($column).' = '.$value; + }) + ->implode(', '); } /** diff --git a/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php b/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php index c5e91c50e1bf..6426abbfde0f 100755 --- a/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php +++ b/src/Illuminate/Database/Query/Grammars/SqlServerGrammar.php @@ -419,15 +419,15 @@ public function compileUpsert(Builder $query, array $values, array $uniqueBy, ar $sql = 'merge '.$this->wrapTable($query->from).' '; - $parameters = (new Collection($values))->map(function ($record) { - return '('.$this->parameterize($record).')'; - })->implode(', '); + $parameters = (new Collection($values)) + ->map(fn ($record) => '('.$this->parameterize($record).')') + ->implode(', '); $sql .= 'using (values '.$parameters.') '.$this->wrapTable('laravel_source').' ('.$columns.') '; - $on = (new Collection($uniqueBy))->map(function ($column) use ($query) { - return $this->wrap('laravel_source.'.$column).' = '.$this->wrap($query->from.'.'.$column); - })->implode(' and '); + $on = (new Collection($uniqueBy)) + ->map(fn ($column) => $this->wrap('laravel_source.'.$column).' = '.$this->wrap($query->from.'.'.$column)) + ->implode(' and '); $sql .= 'on '.$on.' '; diff --git a/src/Illuminate/Database/Schema/Blueprint.php b/src/Illuminate/Database/Schema/Blueprint.php index 07cb721eefbc..980940dd57be 100755 --- a/src/Illuminate/Database/Schema/Blueprint.php +++ b/src/Illuminate/Database/Schema/Blueprint.php @@ -179,9 +179,8 @@ protected function ensureCommandsAreValid() */ protected function commandsNamed(array $names) { - return (new Collection($this->commands))->filter(function ($command) use ($names) { - return in_array($command->name, $names); - }); + return (new Collection($this->commands)) + ->filter(fn ($command) => in_array($command->name, $names)); } /** @@ -316,9 +315,8 @@ public function addAlterCommands() */ public function creating() { - return (new Collection($this->commands))->contains(function ($command) { - return ! $command instanceof ColumnDefinition && $command->name === 'create'; - }); + return (new Collection($this->commands)) + ->contains(fn ($command) => ! $command instanceof ColumnDefinition && $command->name === 'create'); } /** diff --git a/src/Illuminate/Foundation/Console/ConfigPublishCommand.php b/src/Illuminate/Foundation/Console/ConfigPublishCommand.php index 7053830a17c6..fc24a425f0c5 100644 --- a/src/Illuminate/Foundation/Console/ConfigPublishCommand.php +++ b/src/Illuminate/Foundation/Console/ConfigPublishCommand.php @@ -48,9 +48,7 @@ public function handle() $name = (string) (is_null($this->argument('name')) ? select( label: 'Which configuration file would you like to publish?', - options: (new Collection($config))->map(function (string $path) { - return basename($path, '.php'); - }), + options: (new Collection($config))->map(fn (string $path) => basename($path, '.php')), ) : $this->argument('name')); if (! is_null($name) && ! isset($config[$name])) { diff --git a/src/Illuminate/Foundation/Exceptions/RegisterErrorViewPaths.php b/src/Illuminate/Foundation/Exceptions/RegisterErrorViewPaths.php index f96d231e1db3..f1dedbbace9b 100644 --- a/src/Illuminate/Foundation/Exceptions/RegisterErrorViewPaths.php +++ b/src/Illuminate/Foundation/Exceptions/RegisterErrorViewPaths.php @@ -14,8 +14,10 @@ class RegisterErrorViewPaths */ public function __invoke() { - View::replaceNamespace('errors', (new Collection(config('view.paths')))->map(function ($path) { - return "{$path}/errors"; - })->push(__DIR__.'/views')->all()); + View::replaceNamespace('errors', (new Collection(config('view.paths'))) + ->map(fn ($path) => "{$path}/errors") + ->push(__DIR__.'/views') + ->all() + ); } } diff --git a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php index 3c37c95e4e00..1ace8556feef 100644 --- a/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php +++ b/src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php @@ -702,9 +702,10 @@ protected function prepareCookiesForRequest() return array_merge($this->defaultCookies, $this->unencryptedCookies); } - return (new Collection($this->defaultCookies))->map(function ($value, $key) { - return encrypt(CookieValuePrefix::create($key, app('encrypter')->getKey()).$value, false); - })->merge($this->unencryptedCookies)->all(); + return (new Collection($this->defaultCookies)) + ->map(fn ($value, $key) => encrypt(CookieValuePrefix::create($key, app('encrypter')->getKey()).$value, false)) + ->merge($this->unencryptedCookies) + ->all(); } /** diff --git a/src/Illuminate/Log/LogManager.php b/src/Illuminate/Log/LogManager.php index 270b716f9d8d..2987694f1941 100644 --- a/src/Illuminate/Log/LogManager.php +++ b/src/Illuminate/Log/LogManager.php @@ -271,17 +271,21 @@ protected function createStackDriver(array $config) $config['channels'] = explode(',', $config['channels']); } - $handlers = (new Collection($config['channels']))->flatMap(function ($channel) { - return $channel instanceof LoggerInterface - ? $channel->getHandlers() - : $this->channel($channel)->getHandlers(); - })->all(); - - $processors = (new Collection($config['channels']))->flatMap(function ($channel) { - return $channel instanceof LoggerInterface - ? $channel->getProcessors() - : $this->channel($channel)->getProcessors(); - })->all(); + $handlers = (new Collection($config['channels'])) + ->flatMap(function ($channel) { + return $channel instanceof LoggerInterface + ? $channel->getHandlers() + : $this->channel($channel)->getHandlers(); + }) + ->all(); + + $processors = (new Collection($config['channels'])) + ->flatMap(function ($channel) { + return $channel instanceof LoggerInterface + ? $channel->getProcessors() + : $this->channel($channel)->getProcessors(); + }) + ->all(); if ($config['ignore_exceptions'] ?? false) { $handlers = [new WhatFailureGroupHandler($handlers)]; diff --git a/src/Illuminate/Pagination/Cursor.php b/src/Illuminate/Pagination/Cursor.php index 73e5cd4e3a56..433e33e0ae1c 100644 --- a/src/Illuminate/Pagination/Cursor.php +++ b/src/Illuminate/Pagination/Cursor.php @@ -60,9 +60,9 @@ public function parameter(string $parameterName) */ public function parameters(array $parameterNames) { - return (new Collection($parameterNames))->map(function ($parameterName) { - return $this->parameter($parameterName); - })->toArray(); + return (new Collection($parameterNames)) + ->map(fn ($parameterName) => $this->parameter($parameterName)) + ->toArray(); } /** diff --git a/src/Illuminate/Process/Factory.php b/src/Illuminate/Process/Factory.php index e30fa8c6c99f..ac1d5f3150f0 100644 --- a/src/Illuminate/Process/Factory.php +++ b/src/Illuminate/Process/Factory.php @@ -205,9 +205,9 @@ public function assertRanTimes(Closure|string $callback, int $times = 1) { $callback = is_string($callback) ? fn ($process) => $process->command === $callback : $callback; - $count = (new Collection($this->recorded))->filter(function ($pair) use ($callback) { - return $callback($pair[0], $pair[1]); - })->count(); + $count = (new Collection($this->recorded)) + ->filter(fn ($pair) => $callback($pair[0], $pair[1])) + ->count(); PHPUnit::assertSame( $times, $count, diff --git a/src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php b/src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php index 86c1635ab477..a7450c3969a8 100644 --- a/src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php +++ b/src/Illuminate/Queue/Failed/DynamoDbFailedJobProvider.php @@ -109,20 +109,21 @@ public function all() 'ScanIndexForward' => false, ]); - return (new Collection($results['Items']))->sortByDesc(function ($result) { - return (int) $result['failed_at']['N']; - })->map(function ($result) { - return (object) [ - 'id' => $result['uuid']['S'], - 'connection' => $result['connection']['S'], - 'queue' => $result['queue']['S'], - 'payload' => $result['payload']['S'], - 'exception' => $result['exception']['S'], - 'failed_at' => Carbon::createFromTimestamp( - (int) $result['failed_at']['N'], date_default_timezone_get() - )->format(DateTimeInterface::ISO8601), - ]; - })->all(); + return (new Collection($results['Items'])) + ->sortByDesc(fn ($result) => (int) $result['failed_at']['N']) + ->map(function ($result) { + return (object) [ + 'id' => $result['uuid']['S'], + 'connection' => $result['connection']['S'], + 'queue' => $result['queue']['S'], + 'payload' => $result['payload']['S'], + 'exception' => $result['exception']['S'], + 'failed_at' => Carbon::createFromTimestamp( + (int) $result['failed_at']['N'], date_default_timezone_get() + )->format(DateTimeInterface::ISO8601), + ]; + }) + ->all(); } /** diff --git a/src/Illuminate/Routing/ControllerDispatcher.php b/src/Illuminate/Routing/ControllerDispatcher.php index f82e768d0f7b..edf039630663 100644 --- a/src/Illuminate/Routing/ControllerDispatcher.php +++ b/src/Illuminate/Routing/ControllerDispatcher.php @@ -74,8 +74,9 @@ public function getMiddleware($controller, $method) return []; } - return (new Collection($controller->getMiddleware()))->reject(function ($data) use ($method) { - return static::methodExcludedByOptions($method, $data['options']); - })->pluck('middleware')->all(); + return (new Collection($controller->getMiddleware())) + ->reject(fn ($data) => static::methodExcludedByOptions($method, $data['options'])) + ->pluck('middleware') + ->all(); } } diff --git a/src/Illuminate/Routing/Router.php b/src/Illuminate/Routing/Router.php index eb9c986bfe9e..ccadac1fe2bd 100644 --- a/src/Illuminate/Routing/Router.php +++ b/src/Illuminate/Routing/Router.php @@ -837,9 +837,9 @@ public function resolveMiddleware(array $middleware, array $excluded = []) ->values() ->all(); - $middleware = (new Collection($middleware))->map(function ($name) { - return (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups); - })->flatten() + $middleware = (new Collection($middleware)) + ->map(fn ($name) => (array) MiddlewareNameResolver::resolve($name, $this->middleware, $this->middlewareGroups)) + ->flatten() ->when( ! empty($excluded), fn ($collection) => $collection->reject(function ($name) use ($excluded) { @@ -861,7 +861,8 @@ public function resolveMiddleware(array $middleware, array $excluded = []) fn ($exclude) => class_exists($exclude) && $reflection->isSubclassOf($exclude) ); }) - )->values(); + ) + ->values(); return $this->sortMiddleware($middleware); } diff --git a/src/Illuminate/Testing/ParallelConsoleOutput.php b/src/Illuminate/Testing/ParallelConsoleOutput.php index 6dd351e6d26a..1ee388a83aeb 100644 --- a/src/Illuminate/Testing/ParallelConsoleOutput.php +++ b/src/Illuminate/Testing/ParallelConsoleOutput.php @@ -51,9 +51,8 @@ public function __construct($output) */ public function write($messages, bool $newline = false, int $options = 0): void { - $messages = (new Collection($messages))->filter(function ($message) { - return ! Str::contains($message, $this->ignore); - }); + $messages = (new Collection($messages)) + ->filter(fn ($message) => ! Str::contains($message, $this->ignore)); $this->output->write($messages->toArray(), $newline, $options); } diff --git a/src/Illuminate/Validation/Rules/DatabaseRule.php b/src/Illuminate/Validation/Rules/DatabaseRule.php index 49d929b44c99..bc879ee0ee18 100644 --- a/src/Illuminate/Validation/Rules/DatabaseRule.php +++ b/src/Illuminate/Validation/Rules/DatabaseRule.php @@ -231,8 +231,8 @@ public function queryCallbacks() */ protected function formatWheres() { - return (new Collection($this->wheres))->map(function ($where) { - return $where['column'].','.'"'.str_replace('"', '""', $where['value']).'"'; - })->implode(','); + return (new Collection($this->wheres)) + ->map(fn ($where) => $where['column'].','.'"'.str_replace('"', '""', $where['value']).'"') + ->implode(','); } } diff --git a/src/Illuminate/View/Compilers/BladeCompiler.php b/src/Illuminate/View/Compilers/BladeCompiler.php index 8d86b5628468..d63cc7bce0d5 100644 --- a/src/Illuminate/View/Compilers/BladeCompiler.php +++ b/src/Illuminate/View/Compilers/BladeCompiler.php @@ -777,9 +777,9 @@ public function component($class, $alias = null, $prefix = '') if (is_null($alias)) { $alias = str_contains($class, '\\View\\Components\\') - ? (new Collection(explode('\\', Str::after($class, '\\View\\Components\\'))))->map(function ($segment) { - return Str::kebab($segment); - })->implode(':') + ? (new Collection(explode('\\', Str::after($class, '\\View\\Components\\')))) + ->map(fn ($segment) => Str::kebab($segment)) + ->implode(':') : Str::kebab(class_basename($class)); }