GroupByMutator::__invoke()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 5
nc 2
nop 2
crap 2
1
<?php
2
3
namespace Underscore\Mutator;
4
5
use Underscore\Collection;
6
use Underscore\Mutator;
7
8
class GroupByMutator extends Mutator
9
{
10
    /**
11
     * Creates an object composed of keys generated from the results
12
     * of running each element of a collection through the callback
13
     *
14
     * @param Collection $collection
15
     * @param callable   $iterator
16
     * @return Collection
17
     */
18 3
    public function __invoke($collection, $iterator)
19
    {
20 3
        $newCollection = [];
21
22 3
        foreach ($collection as $value) {
23 2
            $newCollection[call_user_func($iterator, $value)][] = $value;
24
        }
25
26 3
        return $this->copyCollectionWith($collection, $newCollection);
27
    }
28
}
29