Passed
Push — master ( 651f47...1b6630 )
by Edward
03:03
created

StdDevAggregator   A

Complexity

Total Complexity 2

Size/Duplication

Total Lines 20
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 20
ccs 11
cts 11
cp 1
rs 10
c 0
b 0
f 0
wmc 2

1 Method

Rating   Name   Duplication   Size   Complexity  
A aggregateNumericData() 0 17 2
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Runtime\Aggregator;
5
6
use function array_map;
7
use function array_sum;
8
use function count;
9
use Remorhaz\JSON\Path\Value\LiteralScalarValue;
10
use Remorhaz\JSON\Data\Value\ScalarValueInterface;
11
use Remorhaz\JSON\Data\Value\ValueInterface;
12
use function sqrt;
13
14
final class StdDevAggregator extends NumericAggregator
15
{
16
17 2
    protected function aggregateNumericData(array $dataList, ScalarValueInterface ...$elements): ?ValueInterface
18
    {
19 2
        $count = count($dataList);
20 2
        if ($count < 2) {
21 1
            return null;
22
        }
23
24 1
        $meanValue = array_sum($dataList) / $count;
25
        $calculateSquaredDifferenceFromMean = function ($value) use ($meanValue): float {
26 1
            return ($value - $meanValue) ** 2;
27 1
        };
28
29 1
        $squaredDifferencesSum = array_sum(array_map($calculateSquaredDifferenceFromMean, $dataList));
30 1
        $variance = $squaredDifferencesSum / ($count - 1);
31 1
        $standardDeviation = sqrt($variance);
32
33 1
        return new LiteralScalarValue($standardDeviation);
34
    }
35
}
36