Passed
Pull Request — master (#20)
by
unknown
05:02 queued 02:27
created

Sum::__invoke()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 2
eloc 4
c 3
b 0
f 0
nc 2
nop 1
dl 0
loc 7
ccs 5
cts 5
cp 1
crap 2
rs 10
1
<?php
2
3
namespace WS\Utils\Collections\Functions\Group\Aggregator;
4
5
use WS\Utils\Collections\Collection;
6
use WS\Utils\Collections\Functions\ObjectFunctions;
7
8
class Sum
9
{
10
11
    private $fieldName;
12
13 8
    public function __construct(string $fieldName)
14
    {
15 8
        $this->fieldName = $fieldName;
16 8
    }
17
18 8
    public function __invoke(Collection $collection)
19
    {
20 8
        $sum = 0;
21 8
        foreach ($collection as $element) {
22 7
            $sum += ObjectFunctions::getPropertyValue($element, $this->fieldName);
23
        }
24 8
        return $sum;
25
    }
26
}
27