GroupByModel   A
last analyzed

Complexity

Total Complexity 1

Size/Duplication

Total Lines 18
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 1
lcom 0
cbo 1
dl 0
loc 18
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
A __invoke() 0 15 1
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
 * @mixin \Illuminate\Support\Collection
16
 *
17
 * @return \Illuminate\Support\Collection
18
 */
19
class GroupByModel
20
{
21
    public function __invoke()
22
    {
23
        return function ($callback, bool $preserveKeys = false, $modelKey = 0, $itemsKey = 1): Collection {
24
            $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...
25
26
            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...
27
                return $callback($item)->getKey();
28
            }, $preserveKeys)->map(function (Collection $items) use ($callback, $modelKey, $itemsKey) {
29
                return [
30
                    $modelKey => $callback($items->first()),
31
                    $itemsKey => $items,
32
                ];
33
            })->values();
34
        };
35
    }
36
}
37