Skip to content

Commit

Permalink
more consistent and readable chaining (#53748)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
browner12 authored Dec 6, 2024
1 parent 0d3c881 commit bde0abf
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions Middleware/Authorize.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,9 +73,9 @@ protected function getGateArguments($request, $models)
return [];
}

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

/**
Expand Down

0 comments on commit bde0abf

Please sign in to comment.