Skip to content

Commit 3b26bb2

Browse files
authored
use short closures where possible (#46102)
short closures are a little more succinct for readability, and this gives is more consistency throughout the `Collection` class, as we had a mix of short vs long closures.
1 parent a501fc2 commit 3b26bb2

File tree

1 file changed

+7
-20
lines changed

1 file changed

+7
-20
lines changed

src/Illuminate/Collections/Collection.php

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -84,11 +84,8 @@ public function avg($callback = null)
8484
{
8585
$callback = $this->valueRetriever($callback);
8686

87-
$items = $this->map(function ($value) use ($callback) {
88-
return $callback($value);
89-
})->filter(function ($value) {
90-
return ! is_null($value);
91-
});
87+
$items = $this->map(fn ($value) => $callback($value))
88+
->filter(fn ($value) => ! is_null($value));
9289

9390
if ($count = $items->count()) {
9491
return $items->sum() / $count;
@@ -349,14 +346,10 @@ public function duplicatesStrict($callback = null)
349346
protected function duplicateComparator($strict)
350347
{
351348
if ($strict) {
352-
return function ($a, $b) {
353-
return $a === $b;
354-
};
349+
return fn ($a, $b) => $a === $b;
355350
}
356351

357-
return function ($a, $b) {
358-
return $a == $b;
359-
};
352+
return fn ($a, $b) => $a == $b;
360353
}
361354

362355
/**
@@ -1151,9 +1144,7 @@ public function sliding($size = 2, $step = 1)
11511144
{
11521145
$chunks = floor(($this->count() - $size) / $step) + 1;
11531146

1154-
return static::times($chunks, function ($number) use ($size, $step) {
1155-
return $this->slice(($number - 1) * $step, $size);
1156-
});
1147+
return static::times($chunks, fn ($number) => $this->slice(($number - 1) * $step, $size));
11571148
}
11581149

11591150
/**
@@ -1634,13 +1625,9 @@ public function values()
16341625
*/
16351626
public function zip($items)
16361627
{
1637-
$arrayableItems = array_map(function ($items) {
1638-
return $this->getArrayableItems($items);
1639-
}, func_get_args());
1628+
$arrayableItems = array_map(fn ($items) => $this->getArrayableItems($items), func_get_args());
16401629

1641-
$params = array_merge([function () {
1642-
return new static(func_get_args());
1643-
}, $this->items], $arrayableItems);
1630+
$params = array_merge([fn () => new static(func_get_args()), $this->items], $arrayableItems);
16441631

16451632
return new static(array_map(...$params));
16461633
}

0 commit comments

Comments
 (0)