Completed
Push — master ( 785bde...8b4d11 )
by Freek
01:29
created

GroupByModel::__invoke()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
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
    {
21
        return function ($callback, bool $preserveKeys = false, $modelKey = 0, $itemsKey = 1): Collection {
22
            $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...
23
24
            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...
25
                return $callback($item)->getKey();
26
            }, $preserveKeys)->map(function (Collection $items) use ($callback, $modelKey, $itemsKey) {
27
                return [
28
                    $modelKey => $callback($items->first()),
29
                    $itemsKey => $items,
30
                ];
31
            })->values();
32
        };
33
    }
34
}
35