10000 more consistent and readable chaining (#53748) · tibbsa/laravel-framework@e4a7357 · GitHub
[go: up one dir, main page]

Skip to content

Commit e4a7357

Browse files
authored
more consistent and readable chaining (laravel#53748)
my goal with this commit is to make multiline chaining more consistent and readable. obviously there can be a subjective component to things like this, so I'll do my best to layout why I think these changes are objectively good. I have limited this first proof-of-concept commit to chaining on new `Collection`s, as they are a good simple example. what did I change: - convert closures to arrow functions for very simple expressions - always begin the start of a chained method (`->`) on a new line - use a single indent from the first line on new lines why I think this is better: - readability is better by not having multiple methods on a single line. no more hunting for the closing parentheses - each line serves a single grokable purpose. the first line instantiates the new `Collection` with some data. each following line runs an operation on that `Collection` - significantly improved GIT diffs. changes you see in the diff are more likely to be isolated to the purpose of the change - the single indent still gives us good alignment, without the pitfalls of trying to line it up with some arbitrary character or pattern in the starting line. it's very easy to apply consistently across many scenarios.
1 parent c07f426 commit e4a7357

33 files changed

+147
-157
lines changed

src/Illuminate/Auth/Middleware/Authorize.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@ protected function getGateArguments($request, $models)
7373
return [];
7474
}
7575

76-
return (new Collection($models))->map(function ($model) use ($request) {
77-
return $model instanceof Model ? $model : $this->getModel($request, $model);
78-
})->all();
76+
return (new Collection($models))
77+
->map(fn ($model) => $model instanceof Model ? $model : $this->getModel($request, $model))
78+
->all();
7979
}
8080

8181
/**

src/Illuminate/Cache/Repository.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,9 @@ public function many(array $keys)
146146
return is_string($key) ? $key : $value;
147147
})->values()->all());
148148

149-
return (new Collection($values))->map(function ($value, $key) use ($keys) {
150-
return $this->handleManyResult($keys, $key, $value);
151-
})->all();
149+
return (new Collection($values))
150+
->map(fn ($value, $key) => $this->handleManyResult($keys, $key, $value))
151+
->all();
152152
}
153153

154154
/**

src/Illuminate/Cache/RetrievesMultipleKeys.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ public function many(array $keys)
1818
{
1919
$return = [];
2020

21-
$keys = (new Collection($keys))->mapWithKeys(function ($value, $key) {
22-
return [is_string($key) ? $key : $value => is_string($key) ? $value : null];
23-
})->all();
21+
$keys = (new Collection($keys))
22+
->mapWithKeys(fn ($value, $key) => [is_string($key) ? $key : $value => is_string($key) ? $value : null])
23+
->all();
2424

2525
foreach ($keys as $key => $default) {
2626
/** @phpstan-ignore arguments.count (some clients don't accept a default) */

src/Illuminate/Console/Concerns/CallsCommands.php

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,16 @@ protected function createInputFromArguments(array $arguments)
9696
*/
9797
protected function context()
9898
{
99-
return (new Collection($this->option()))->only([
100-
'ansi',
101-
'no-ansi',
102-
'no-interaction',
103-
'quiet',
104-
'verbose',
105-
])->filter()->mapWithKeys(function ($value, $key) {
106-
return ["--{$key}" => $value];
107-
})->all();
99+
return (new Collection($this->option()))
100+
->only([
101+
'ansi',
102+
'no-ansi',
103+
'no-interaction',
104+
'quiet',
105+
'verbose',
106+
])
107+
->filter()
108+
->mapWithKeys(fn ($value, $key) => ["--{$key}" => $value])
109+
->all();
108110
}
109111
}

src/Illuminate/Database/Console/PruneCommand.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,9 @@ protected function models()
157157
protected function getPath()
158158
{
159159
if (! empty($path = $this->option('path'))) {
160-
return (new Collection($path))->map(function ($path) {
161-
return base_path($path);
162-
})->all();
160+
return (new Collection($path))
161+
->map(fn ($path) => base_path($path))
162+
->all();
163163
}
164164

165165
return app_path('Models');

src/Illuminate/Database/Eloquent/BroadcastableModelEventOccurred.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,9 +78,9 @@ public function broadcastOn()
7878
? ($this->model->broadcastOn($this->event) ?: [])
7979
: $this->channels;
8080

81-
return (new BaseCollection($channels))->map(function ($channel) {
82-
return $channel instanceof Model ? new PrivateChannel($channel) : $channel;
83-
})->all();
81+
return (new BaseCollection($channels))
82+
->map(fn ($channel) => $channel instanceof Model ? new PrivateChannel($channel) : $channel)
83+
->all();
8484
}
8585

8686
/**

src/Illuminate/Database/Eloquent/Concerns/HasAttributes.php

Lines changed: 9 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1942,9 +1942,9 @@ protected function getOriginalWithoutRewindingModel($key = null, $default = null
19421942
);
19431943
}
19441944

1945-
return (new Collection($this->original))->mapWithKeys(function ($value, $key) {
1946-
return [$key => $this->transformModelValue($key, $value)];
1947-
})->all();
1945+
return (new Collection($this->original))
1946+
->mapWithKeys(fn ($value, $key) => [$key => $this->transformModelValue($key, $value)])
1947+
->all();
19481948
}
19491949

19501950
/**
@@ -2315,17 +2315,14 @@ public static function cacheMutatedAttributes($classOrInstance)
23152315

23162316
$class = $reflection->getName();
23172317

2318-
static::$getAttributeMutatorCache[$class] =
2319-
(new Collection($attributeMutatorMethods = static::getAttributeMarkedMutatorMethods($classOrInstance)))
2320-
->mapWithKeys(function ($match) {
2321-
return [lcfirst(static::$snakeAttributes ? Str::snake($match) : $match) => true];
2322-
})->all();
2318+
static::$getAttributeMutatorCache[$class] = (new Collection($attributeMutatorMethods = static::getAttributeMarkedMutatorMethods($classOrInstance)))
2319+
->mapWithKeys(fn ($match) => [lcfirst(static::$snakeAttributes ? Str::snake($match) : $match) => true])
2320+
->all();
23232321

23242322
static::$mutatorCache[$class] = (new Collection(static::getMutatorMethods($class)))
2325-
->merge($attributeMutatorMethods)
2326-
->map(function ($match) {
2327-
return lcfirst(static::$snakeAttributes ? Str::snake($match) : $match);
2328-
})->all();
2323+
->merge($attributeMutatorMethods)
2324+
->map(fn ($match) => lcfirst(static::$snakeAttributes ? Str::snake($match) : $match))
2325+
->all();
23292326
}
23302327

23312328
/**

src/Illuminate/Database/Eloquent/Model.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -600,9 +600,9 @@ public function qualifyColumn($column)
600600
*/
601601
public function qualifyColumns($columns)
602602
{
603-
return (new BaseCollection($columns))->map(function ($column) {
604-
return $this->qualifyColumn($column);
605-
})->all();
603+
return (new BaseCollection($columns))
604+
->map(fn ($column) => $this->qualifyColumn($column))
605+
->all();
606606
}
607607

608608
/**

src/Illuminate/Database/Eloquent/Relations/Concerns/InteractsWithPivotTable.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -616,9 +616,9 @@ protected function parseIds($value)
616616
}
617617

618618
if ($value instanceof BaseCollection || is_array($value)) {
619-
return (new BaseCollection($value))->map(function ($item) {
620-
return $item instanceof Model ? $item->{$this->relatedKey} : $item;
621-
})->all();
619+
return (new BaseCollection($value))
620+
->map(fn ($item) => $item instanceof Model ? $item->{$this->relatedKey} : $item)
621+
->all();
622622
}
623623

624624
return (array) $value;

src/Illuminate/Database/Query/Builder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3949,9 +3949,9 @@ public function upsert(array $values, $uniqueBy, $update = null)
39493949

39503950
$bindings = $this->cleanBindings(array_merge(
39513951
Arr::flatten($values, 1),
3952-
(new Collection($update))->reject(function ($value, $key) {
3953-
return is_int($key);
3954-
})->all()
3952+
(new Collection($update))
3953+
->reject(fn ($value, $key) => is_int($key))
3954+
->all()
39553955
));
39563956

39573957
return $this->connection->affectingStatement(

0 commit comments

Comments
 (0)
0