Passed
Pull Request — master (#1214)
by
unknown
16:56 queued 02:37
created

AbstractTracer   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 47
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 6
Bugs 0 Features 1
Metric Value
wmc 7
eloc 28
c 6
b 0
f 1
dl 0
loc 47
ccs 26
cts 26
cp 1
rs 10

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
B runScope() 0 38 6
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Telemetry;
6
7
use Spiral\Core\BinderInterface;
8
use Spiral\Core\Container;
9
use Spiral\Core\ContainerScope;
10
use Spiral\Core\InvokerInterface;
11
use Spiral\Core\ScopeInterface;
12
13
/**
14
 * @internal The component is under development.
15
 * Something may be changed in the future. We will stable it soon.
16
 * Feedback is welcome {@link https://github.com/spiral/framework/discussions/822}.
17
 */
18
abstract class AbstractTracer implements TracerInterface
19
{
20 626
    public function __construct(
21
        private readonly ?ScopeInterface $scope = new Container(),
22 626
    ) {}
23
24
    /**
25
     * @throws \Throwable
26
     */
27 177
    final protected function runScope(SpanInterface $span, callable $callback): mixed
28
    {
29 177
        $container = ContainerScope::getContainer();
30 177
        if ($container === null) {
31 112
            return $this->scope->runScope([
0 ignored issues
show
Bug introduced by
The method runScope() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

31
            return $this->scope->/** @scrutinizer ignore-call */ runScope([

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
32 112
                SpanInterface::class => $span,
33 112
                TracerInterface::class => $this,
34 112
            ], static fn(InvokerInterface $invoker): mixed => $invoker->invoke($callback));
35
        }
36
37 125
        if ($container instanceof Container) {
38 124
            $invoker = $container;
39 124
            $binder = $container;
40
        } else {
41
            /** @var InvokerInterface $invoker */
42 1
            $invoker = $container->get(InvokerInterface::class);
43
            /** @var BinderInterface $binder */
44 1
            $binder = $container->get(BinderInterface::class);
45
        }
46
47
        try {
48 125
            $prevSpan = $container->get(SpanInterface::class);
49 65
        } catch (\Throwable) {
50 65
            $prevSpan = null;
51
        }
52
53 125
        $binder->bindSingleton(SpanInterface::class, $span);
54
55
        try {
56 125
            return $prevSpan === null
57 65
                ? $this->scope->runScope([
58 65
                    TracerInterface::class => $this,
59 65
                ], static fn(): mixed => $invoker->invoke($callback))
60 118
                : $invoker->invoke($callback);
61
        } finally {
62 125
            $prevSpan === null
63 65
                ? $binder->removeBinding(SpanInterface::class)
64 125
                : $binder->bindSingleton(SpanInterface::class, $prevSpan);
65
        }
66
    }
67
}
68