Serialization   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 40
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 7
lcom 0
cbo 0
dl 0
loc 40
ccs 12
cts 12
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A filterUnset() 0 14 4
A serialize() 0 6 1
A serializeOr() 0 4 2
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent;
5
6
use JsonSerializable;
7
use function array_filter;
8
use function array_map;
9
use function is_array;
10
11
final class Serialization
12
{
13
    /**
14
     * @param mixed[] $input
15
     * @return mixed[]
16
     */
17 101
    public static function filterUnset(array $input): array
18
    {
19
        return array_filter($input, function ($value): bool {
20 101
            if ($value === null) {
21 92
                return false;
22
            }
23
24 98
            if (is_array($value) && empty($value)) {
0 ignored issues
show
Unused Code introduced by
This if statement, and the following return statement can be replaced with return !(\is_array($value) && empty($value));.
Loading history...
25 83
                return false;
26
            }
27
28 96
            return true;
29 101
        });
30
    }
31
32
    /**
33
     * @return mixed[]
34
     */
35 69
    public static function serialize(JsonSerializable ...$serializable): array
36
    {
37
        return array_map(function (JsonSerializable $frame) {
38 53
            return $frame->jsonSerialize();
39 69
        }, $serializable);
40
    }
41
42
    /**
43
     * @param mixed $fallback
44
     * @return mixed
45
     */
46 72
    public static function serializeOr(?JsonSerializable $serializable, $fallback = null)
47
    {
48 72
        return $serializable === null ? $fallback : $serializable->jsonSerialize();
49
    }
50
}
51