Passed
Pull Request — master (#20)
by
unknown
02:41
created

Avg::__invoke()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 3
eloc 8
c 2
b 0
f 0
nc 4
nop 1
dl 0
loc 12
ccs 9
cts 9
cp 1
crap 3
rs 10
1
<?php
2
/**
3
 * @author Igor Pomiluyko <[email protected]>
4
 */
5
6
namespace WS\Utils\Collections\Functions\Group\Aggregator;
7
8
use WS\Utils\Collections\Collection;
9
10
class Avg extends AbstractFieldAggregator implements Aggregator
11
{
12
13 8
    public function __invoke(Collection $collection)
14
    {
15 8
        $acc = null;
16 8
        $cnt = 0;
17 8
        foreach ($collection as $element) {
18 7
            $acc += $this->getValue($element);
19 6
            $cnt++;
20
        }
21 7
        if (!$cnt) {
22 1
            return null;
23
        }
24 6
        return $acc / $cnt;
25
    }
26
}
27