Passed
Pull Request — master (#1214)
by Aleksei
15:47 queued 04:25
created

AbstractTracer::runScope()   B

Complexity

Conditions 6
Paths 33

Size

Total Lines 43
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 6.0118

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 29
c 2
b 0
f 1
dl 0
loc 43
ccs 27
cts 29
cp 0.931
rs 8.8337
cc 6
nc 33
nop 2
crap 6.0118
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\Scope;
12
use Spiral\Core\ScopeInterface;
13
14
/**
15
 * @internal The component is under development.
16
 * Something may be changed in the future. We will stable it soon.
17
 * Feedback is welcome {@link https://github.com/spiral/framework/discussions/822}.
18
 */
19
abstract class AbstractTracer implements TracerInterface
20
{
21 308
    public function __construct(
22
        private readonly ?ScopeInterface $scope = new Container(),
23 308
    ) {}
24
25
    /**
26
     * @throws \Throwable
27
     */
28 178
    final protected function runScope(SpanInterface $span, callable $callback): mixed
29
    {
30 178
        $container = ContainerScope::getContainer();
31 178
        if ($container === null) {
32 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

32
            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...
33 112
                SpanInterface::class => $span,
34 112
                TracerInterface::class => $this,
35 112
            ], static fn(InvokerInterface $invoker): mixed => $invoker->invoke($callback));
36
        }
37
38 126
        if ($container instanceof Container) {
39 126
            $invoker = $container;
40 126
            $binder = $container;
41
        } else {
42
            /** @var InvokerInterface $invoker */
43
            $invoker = $container->get(InvokerInterface::class);
44
            /** @var BinderInterface $binder */
45
            $binder = $container->get(BinderInterface::class);
46
        }
47
48
        try {
49 126
            $prevSpan = $container->get(SpanInterface::class);
50 66
        } catch (\Throwable) {
51 66
            $prevSpan = null;
52
        }
53
54 126
        $binder->bindSingleton(SpanInterface::class, $span);
55
56
        try {
57 126
            return $prevSpan === null
58 66
                ? $this->scope->runScope(
59 66
                    new Scope(
0 ignored issues
show
Bug introduced by
new Spiral\Core\Scope(ar...rface::class => $this)) of type Spiral\Core\Scope is incompatible with the type array expected by parameter $bindings of Spiral\Core\ScopeInterface::runScope(). ( Ignorable by Annotation )

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

59
                    /** @scrutinizer ignore-type */ new Scope(
Loading history...
60 66
                        bindings: [
61 66
                            TracerInterface::class => $this,
62 66
                        ],
63 66
                    ),
64 66
                    static fn(): mixed => $invoker->invoke($callback),
65 66
                )
66 119
                : $invoker->invoke($callback);
67
        } finally {
68 126
            $prevSpan === null
69 66
                ? $binder->removeBinding(SpanInterface::class)
70 126
                : $binder->bindSingleton(SpanInterface::class, $prevSpan);
71
        }
72
    }
73
}
74