NumericAggregator   A
last analyzed

Complexity

Total Complexity 12

Size/Duplication

Total Lines 59
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 22
dl 0
loc 59
ccs 27
cts 27
cp 1
rs 10
c 0
b 0
f 0
wmc 12

5 Methods

Rating   Name   Duplication   Size   Complexity  
A tryAggregate() 0 10 2
A findNumericElement() 0 9 4
A findNumericElements() 0 13 4
A getElementDataList() 0 3 1
A getElementData() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Remorhaz\JSON\Path\Runtime\Aggregator;
6
7
use Remorhaz\JSON\Data\Value\ArrayValueInterface;
8
use Remorhaz\JSON\Data\Value\ScalarValueInterface;
9
use Remorhaz\JSON\Data\Value\ValueInterface;
10
11
use function array_map;
12
use function is_float;
13
use function is_int;
14
15
abstract class NumericAggregator implements ValueAggregatorInterface
16
{
17
18
    abstract protected function aggregateNumericData(
19
        array $dataList,
20
        ScalarValueInterface ...$elements
21
    ): ?ValueInterface;
22
23 8
    final public function tryAggregate(ValueInterface $value): ?ValueInterface
24
    {
25 8
        $numericElements = $this->findNumericElements($value);
26 8
        if (empty($numericElements)) {
27 3
            return null;
28
        }
29
30 5
        return $this->aggregateNumericData(
31 5
            $this->getElementDataList(...$numericElements),
32 5
            ...$numericElements
33
        );
34
    }
35
36 7
    protected function findNumericElement(ValueInterface $element): ?ScalarValueInterface
37
    {
38 7
        if (!$element instanceof ScalarValueInterface) {
39 1
            return null;
40
        }
41 6
        $elementData = $element->getData();
42 6
        return is_int($elementData) || is_float($elementData)
43 5
            ? $element
44 6
            : null;
45
    }
46
47
    /**
48
     * @param ValueInterface $value
49
     * @return ScalarValueInterface[]
50
     */
51 8
    protected function findNumericElements(ValueInterface $value): array
52
    {
53 8
        $numericElements = [];
54 8
        if (!$value instanceof ArrayValueInterface) {
55 1
            return $numericElements;
56
        }
57 7
        foreach ($value->createChildIterator() as $element) {
58 7
            $numericElement = $this->findNumericElement($element);
59 7
            if (isset($numericElement)) {
60 5
                $numericElements[] = $numericElement;
61
            }
62
        }
63 7
        return $numericElements;
64
    }
65
66 5
    protected function getElementDataList(ScalarValueInterface ...$elements): array
67
    {
68 5
        return array_map([$this, 'getElementData'], $elements);
69
    }
70
71 5
    private function getElementData(ScalarValueInterface $element)
72
    {
73 5
        return $element->getData();
74
    }
75
}
76