StdDevAggregator::aggregateNumericData()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 10
c 0
b 0
f 0
nc 2
nop 2
dl 0
loc 17
ccs 12
cts 12
cp 1
crap 2
rs 9.9332
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Runtime\Aggregator;
6
7
use Remorhaz\JSON\Path\Value\LiteralScalarValue;
8
use Remorhaz\JSON\Data\Value\ScalarValueInterface;
9
use Remorhaz\JSON\Data\Value\ValueInterface;
10
11
use function array_map;
12
use function array_sum;
13
use function count;
14
use function sqrt;
15
16
final class StdDevAggregator extends NumericAggregator
17
{
18
19 2
    protected function aggregateNumericData(array $dataList, ScalarValueInterface ...$elements): ?ValueInterface
20
    {
21 2
        $count = count($dataList);
22 2
        if ($count < 2) {
23 1
            return null;
24
        }
25
26 1
        $meanValue = array_sum($dataList) / $count;
27 1
        $calculateSquaredDifferenceFromMean = function ($value) use ($meanValue): float {
28 1
            return ($value - $meanValue) ** 2;
29 1
        };
30
31 1
        $squaredDifferencesSum = array_sum(array_map($calculateSquaredDifferenceFromMean, $dataList));
32 1
        $variance = $squaredDifferencesSum / ($count - 1);
33 1
        $standardDeviation = sqrt($variance);
34
35 1
        return new LiteralScalarValue($standardDeviation);
36
    }
37
}
38