Test Failed
Pull Request — master (#19)
by
unknown
04:32 queued 01:40
created

SentryTraceLogTarget::export()   B

Complexity

Conditions 10
Paths 1

Size

Total Lines 47
Code Lines 30

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 10
eloc 30
nc 1
nop 0
dl 0
loc 47
rs 7.6666
c 0
b 0
f 0

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 Yiisoft\Yii\Sentry\Tracing;
6
7
use Sentry\Tracing\SpanContext;
8
use Yiisoft\Log\Message;
9
use Yiisoft\Log\Target;
10
use Yiisoft\Yii\Sentry\Integration\Integration;
11
12
final class SentryTraceLogTarget extends Target
13
{
14
    protected function export(): void
15
    {
16
        array_map(
17
            static function (Message $message): Message {
18
                /** @var array<string, mixed> $context */
19
                $context = $message->context();
20
                $category = (string)($context['category'] ?? 'log');
21
                $time = (float)($message->context()['time'] ?? microtime(true));
22
                unset($context['category'], $context['time']);
23
                if (array_key_exists('trace', $context)
24
                    && empty($context['trace'])
25
                ) {
26
                    unset($context['trace']);
27
                }
28
                if (!empty($context['memory']) && is_numeric($context['memory'])) {
29
                    $context['memory'] = round(
30
                        ((float)$context['memory'] / (1024 * 1024)),
31
                        2
32
                    ) . 'MB';
33
                }
34
                $parentSpan = Integration::currentTracingSpan();
35
36
                // If there is no tracing span active there is no need to handle the event
37
                if ($parentSpan === null) {
38
                    return $message;
39
                }
40
                $spanContext = new SpanContext();
41
                if (isset($context['tags']) && is_array($context['tags'])) {
42
                    /**
43
                    * @psalm-suppress MixedArgumentTypeCoercion
44
                    */
45
                    $spanContext->setTags($context['tags']);
46
                    unset($context['tags']);
47
                }
48
                $spanContext->setOp($category);
49
                $spanContext->setDescription($message->message());
50
                $spanContext->setStartTimestamp($time);
51
                $elapsed = empty($context['elapsed']) ? null : (float)$context['elapsed'];
52
                if ($elapsed) {
53
                    $spanContext->setEndTimestamp($time + $elapsed);
54
                }
55
                $spanContext->setData($context);
56
57
                $parentSpan->startChild($spanContext);
58
59
                return $message;
60
            }, $this->getMessages()
61
        );
62
    }
63
}
64