Conditions | 10 |
Paths | 1 |
Total Lines | 45 |
Code Lines | 30 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
14 | protected function export(): void |
||
15 | { |
||
16 | array_map(function (Message $message): Message { |
||
17 | /** @var array<string, mixed> $context */ |
||
18 | $context = $message->context(); |
||
19 | $category = (string)($context['category'] ?? 'log'); |
||
20 | $time = (float)($message->context()['time'] ?? microtime(true)); |
||
21 | unset($context['category'], $context['time']); |
||
22 | if ( |
||
23 | 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 | /** @psalm-suppress MixedArgumentTypeCoercion */ |
||
43 | $spanContext->setTags($context['tags']); |
||
44 | unset($context['tags']); |
||
45 | } |
||
46 | $spanContext->setOp($category); |
||
47 | $spanContext->setDescription($message->message()); |
||
48 | $spanContext->setStartTimestamp($time); |
||
49 | $elapsed = empty($context['elapsed']) ? null : (float)$context['elapsed']; |
||
50 | if ($elapsed) { |
||
51 | $spanContext->setEndTimestamp($time + $elapsed); |
||
52 | } |
||
53 | $spanContext->setData($context); |
||
54 | |||
55 | $parentSpan->startChild($spanContext); |
||
56 | |||
57 | return $message; |
||
58 | }, $this->getMessages()); |
||
59 | } |
||
61 |