Span   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 102
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
dl 0
loc 102
c 0
b 0
f 0
wmc 6
lcom 1
cbo 1
ccs 32
cts 32
cp 1
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A withId() 0 7 1
A withContext() 0 7 1
A withParent() 0 7 1
A withStackTraceFrame() 0 7 1
A jsonSerialize() 0 13 1
1
<?php
2
declare(strict_types=1);
3
4
namespace TechDeCo\ElasticApmAgent\Message;
5
6
use JsonSerializable;
7
use TechDeCo\ElasticApmAgent\Serialization;
8
use function array_merge;
9
10
final class Span implements JsonSerializable
11
{
12
    /**
13
     * @var int|null
14
     */
15
    private $id;
16
17
    /**
18
     * @var mixed[]
19
     */
20
    private $context = [];
21
22
    /**
23
     * @var float
24
     */
25
    private $duration;
26
27
    /**
28
     * @var string
29
     */
30
    private $name;
31
32
    /**
33
     * @var int|null
34
     */
35
    private $parentId;
36
37
    /**
38
     * @var StackTraceFrame[]
39
     */
40
    private $stackTraceFrameList = [];
41
42
    /**
43
     * @var float
44
     */
45
    private $startOffset;
46
47
    /**
48
     * @var string
49
     */
50
    private $type;
51
52 61
    public function __construct(float $duration, string $name, float $startOffset, string $type)
53
    {
54 61
        $this->duration    = $duration;
55 61
        $this->name        = $name;
56 61
        $this->startOffset = $startOffset;
57 61
        $this->type        = $type;
58 61
    }
59
60 1
    public function withId(int $id): self
61
    {
62 1
        $me     = clone $this;
63 1
        $me->id = $id;
64
65 1
        return $me;
66
    }
67
68
    /**
69
     * @param mixed $value
70
     */
71 1
    public function withContext(string $name, $value): self
72
    {
73 1
        $me                 = clone $this;
74 1
        $me->context[$name] = $value;
75
76 1
        return $me;
77
    }
78
79 1
    public function withParent(Span $parent): self
80
    {
81 1
        $me           = clone $this;
82 1
        $me->parentId = $parent->id;
83
84 1
        return $me;
85
    }
86
87 1
    public function withStackTraceFrame(StackTraceFrame ...$frame): self
88
    {
89 1
        $me                      = clone $this;
90 1
        $me->stackTraceFrameList = array_merge($me->stackTraceFrameList, $frame);
91
92 1
        return $me;
93
    }
94
95
    /**
96
     * @return mixed[]
97
     */
98 26
    public function jsonSerialize(): array
99
    {
100 26
        return Serialization::filterUnset([
101 26
            'id' => $this->id,
102 26
            'context' => $this->context,
103 26
            'duration' => $this->duration,
104 26
            'name' => $this->name,
105 26
            'parent' => $this->parentId,
106 26
            'stacktrace' => Serialization::serialize(...$this->stackTraceFrameList),
107 26
            'start' => $this->startOffset,
108 26
            'type' => $this->type,
109
        ]);
110
    }
111
}
112