Test Failed
Pull Request — master (#1214)
by
unknown
16:58 queued 04:04
created

AbstractTracer::runScope()   B

Complexity

Conditions 7
Paths 33

Size

Total Lines 46
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 7

Importance

Changes 6
Bugs 0 Features 1
Metric Value
eloc 33
c 6
b 0
f 1
dl 0
loc 46
ccs 20
cts 20
cp 1
rs 8.4586
cc 7
nc 33
nop 2
crap 7
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 125
        try {
56
            return $prevSpan === null
57 125
                ? $this->scope->runScope([
58 65
                    TracerInterface::class => $this,
59 125
                ], static fn(): mixed => $invoker->invoke($callback))
60
                : $invoker->invoke($callback);
61
        } finally {
62
            $prevSpan === null
63
                ? $binder->removeBinding(SpanInterface::class)
64
                : $binder->bindSingleton(SpanInterface::class, $prevSpan);
65
        }
66
67
        try {
0 ignored issues
show
Unused Code introduced by
TryCatchNode is not reachable.

This check looks for unreachable code. It uses sophisticated control flow analysis techniques to find statements which will never be executed.

Unreachable code is most often the result of return, die or exit statements that have been added for debug purposes.

function fx() {
    try {
        doSomething();
        return true;
    }
    catch (\Exception $e) {
        return false;
    }

    return false;
}

In the above example, the last return false will never be executed, because a return statement has already been met in every possible execution path.

Loading history...
68
            return $invoker->invoke($callback);
69
        } finally {
70
            $prevSpan === null
71
                ? $binder->removeBinding(SpanInterface::class)
72
                : $binder->bindSingleton(SpanInterface::class, $prevSpan);
73
        }
74
    }
75
}
76