BasisExporter::restoreSpan()   B
last analyzed

Complexity

Conditions 11
Paths 48

Size

Total Lines 38
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 11.0151

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 11
eloc 20
c 2
b 0
f 0
nc 48
nop 1
dl 0
loc 38
ccs 19
cts 20
cp 0.95
crap 11.0151
rs 7.3166

How to fix   Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
3
declare(strict_types=1);
4
5
namespace OpenTelemetry\Exporter;
6
7
use OpenTelemetry\Exporter;
8
use OpenTelemetry\Tracing\Event;
9
use OpenTelemetry\Tracing\Span;
10
use OpenTelemetry\Tracing\Status;
11
use OpenTelemetry\Tracing\SpanContext;
12
use OpenTelemetry\Tracing\Tracer;
13
14
class BasisExporter extends Exporter
15
{
16 1
    public function restoreSpan(array $source): Span
17
    {
18 1
        [$parentSpanId, $traceId, $spanId, $name, $start, $end, $statusCode, $attributes, $events] = $source;
19 1
        $context = SpanContext::restore($traceId, $spanId);
20
21 1
        $parent = null;
22 1
        if ($parentSpanId) {
23 1
            $parent = SpanContext::restore($traceId, $parentSpanId);
24
        }
25
26 1
        $span = (new Span($name ?: 'tracer', $context, $parent));
27
28 1
        if ($attributes) {
29 1
            if (is_object($attributes)) {
30
                $attributes = get_object_vars($attributes);
31
            }
32 1
            $span->setAttributes($attributes);
33 1
        }
34 1
35 1
        if ($events) {
36
            foreach ($events as $info) {
37
                @[$name, $timestamp, $attributes] = $info;
38
                $span->addEvent($name, $attributes ?: [], $timestamp);
39 1
            }
40
        }
41 1
42 1
        $span->setInterval($start, $end);
43
44
        if ($end !== null && !$span->getStatus()) {
45 1
            $span->setStatus(new Status(Status::OK));
46
        }
47
48
        if ($statusCode) {
49
            $span->setStatus(new Status($statusCode));
50 1
        }
51
52
53 1
        return $span;
54
    }
55
56 1
    public function convertSpan(Span $span): array
57 1
    {
58 1
        return [
59 1
            $span->getParentSpanContext() ? $span->getParentSpanContext()->getSpanId() : null,
60 1
            $span->getSpanContext()->getTraceId(),
61 1
            $span->getSpanContext()->getSpanId(),
62 1
            $span->getName(),
63 1
            $span->getStart(),
64 1
            $span->getEnd(),
65
            $span->getStatus() ? $span->getStatus()->getCanonicalCode() : null,
66
            $span->getAttributes() ?: null,
67
            array_map([$this, 'convertEvent'], $span->getEvents()) ?: null
68 1
        ];
69
    }
70
71 1
    public function convertEvent(Event $event): array
72 1
    {
73
        $result = [
74
            $event->getName(),
75 1
            $event->getTimestamp(),
76 1
        ];
77
78
        if (count($event->getAttributes())) {
79 1
            $result[] = $event->getAttributes();
80
        }
81
82
        return $result;
83
    }
84
}
85