Completed
Push — master ( 44df6b...8d835c )
by Dmitry
02:13
created

BasisExporter::restoreSpan()   B

Complexity

Conditions 10
Paths 32

Size

Total Lines 35
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 18
CRAP Score 10.0145

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 18
c 1
b 0
f 0
nc 32
nop 1
dl 0
loc 35
rs 7.6666
ccs 18
cts 19
cp 0.9474
crap 10.0145

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
            $span->setAttributes($attributes);
30
        }
31
32 1
        if ($events) {
33 1
            foreach ($events as $info) {
34 1
                @[$name, $timestamp, $attributes] = $info;
35 1
                $span->addEvent($name, $attributes ?: [], $timestamp);
36
            }
37
        }
38
39 1
        $span->setInterval($start, $end);
40
41 1
        if ($end !== null && !$span->getStatus()) {
42 1
            $span->setStatus(new Status(Status::OK));
43
        }
44
45 1
        if ($statusCode) {
46
            $span->setStatus(new Status($statusCode));
47
        }
48
49
50 1
        return $span;
51
    }
52
53 1
    public function convertSpan(Span $span): array
54
    {
55
        return [
56 1
            $span->getParentSpanContext() ? $span->getParentSpanContext()->getSpanId() : null,
57 1
            $span->getSpanContext()->getTraceId(),
58 1
            $span->getSpanContext()->getSpanId(),
59 1
            $span->getName(),
60 1
            $span->getStart(),
61 1
            $span->getEnd(),
62 1
            $span->getStatus() ? $span->getStatus()->getCanonicalCode() : null,
63 1
            $span->getAttributes() ?: null,
64 1
            array_map([$this, 'convertEvent'], $span->getEvents()) ?: null
65
        ];
66
    }
67
68 1
    public function convertEvent(Event $event): array
69
    {
70
        $result = [
71 1
            $event->getName(),
72 1
            $event->getTimestamp(),
73
        ];
74
75 1
        if (count($event->getAttributes())) {
76 1
            $result[] = $event->getAttributes();
77
        }
78
79 1
        return $result;
80
    }
81
}
82