Completed
Pull Request — master (#162)
by
unknown
01:20
created

GroupByModel::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Spatie\CollectionMacros\Macros;
4
5
use Illuminate\Support\Collection;
6
7
/*
8
 * Group a collection by an Eloquent model.
9
 *
10
 * @param string|callable $callback
11
 * @param bool $preserveKeys
12
 * @param mixed $modelKey
13
 * @param mixed $itemsKey
14
 *
15
 * @return \Illuminate\Support\Collection
16
 */
17
class GroupByModel
18
{
19
    public function __invoke() {
20
        return function ($callback, bool $preserveKeys = false, $modelKey = 0, $itemsKey = 1): Collection {
21
            $callback = $this->valueRetriever($callback);
0 ignored issues
show
Bug introduced by
The method valueRetriever() does not seem to exist on object<Spatie\Collection...os\Macros\GroupByModel>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
22
23
            return $this->groupBy(function ($item) use ($callback) {
0 ignored issues
show
Bug introduced by
The method groupBy() does not seem to exist on object<Spatie\Collection...os\Macros\GroupByModel>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
24
                return $callback($item)->getKey();
25
            }, $preserveKeys)->map(function (Collection $items) use ($callback, $modelKey, $itemsKey) {
26
                return [
27
                    $modelKey => $callback($items->first()),
28
                    $itemsKey => $items,
29
                ];
30
            })->values();
31
        };
32
    }
33
}
34