Completed
Push — master ( cb0ced...7754cf )
by Edward
04:45
created

LengthAggregator::findElementCount()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 6
c 1
b 0
f 0
nc 2
nop 1
dl 0
loc 11
ccs 0
cts 6
cp 0
crap 6
rs 10
1
<?php
2
declare(strict_types=1);
3
4
namespace Remorhaz\JSON\Path\Runtime\Aggregator;
5
6
use function iterator_count;
7
use Remorhaz\JSON\Data\Value\ArrayValueInterface;
8
use Remorhaz\JSON\Path\Value\LiteralScalarValue;
9
use Remorhaz\JSON\Data\Value\ValueInterface;
10
11
final class LengthAggregator implements ValueAggregatorInterface
12
{
13
14
    public function tryAggregate(ValueInterface $value): ?ValueInterface
15
    {
16
        $length = $value instanceof ArrayValueInterface
17
            ? iterator_count($value->createChildIterator())
18
            : null;
19
20
        return isset($length)
21
            ? new LiteralScalarValue($length)
22
            : null;
23
    }
24
}
25