Samples::jsonSerialize()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 18
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 9
c 1
b 0
f 0
nc 2
nop 0
dl 0
loc 18
ccs 12
cts 12
cp 1
crap 2
rs 9.9666
1
<?php
2
3
namespace ZoiloMora\ElasticAPM\Events\MetricSet;
4
5
use ZoiloMora\ElasticAPM\Utils\Collection;
6
7
final class Samples extends Collection
8
{
9
    /**
10
     * @param array $items
11
     *
12
     * @return Samples
13
     */
14 5
    public static function from(array $items)
15
    {
16 5
        foreach ($items as $item) {
17 4
            if (false === $item instanceof Sample) {
18 1
                throw new \InvalidArgumentException('All elements must be of type Sample.');
19
            }
20 4
        }
21
22 4
        if (0 === count($items)) {
23 1
            throw new \InvalidArgumentException('There must be at least one element.');
24
        }
25
26 3
        return parent::from($items);
27
    }
28
29
    /**
30
     * @return array
31
     */
32 2
    public function jsonSerialize()
33
    {
34 2
        $result = [];
35
36 2
        $items = parent::jsonSerialize();
37
38
        /** @var Sample $item */
39 2
        foreach ($items as $item) {
40 2
            $result = array_merge(
41 2
                $result,
42 2
                (array) json_decode(
43 2
                    (string) json_encode($item),
44
                    true
45 2
                )
46 2
            );
47 2
        }
48
49 2
        return $result;
50
    }
51
}
52