Test Failed
Pull Request — master (#1214)
by Aleksei
13:10
created

AbstractTracer::runScope()   B

Complexity

Conditions 6
Paths 33

Size

Total Lines 46
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 6

Importance

Changes 2
Bugs 0 Features 1
Metric Value
eloc 31
c 2
b 0
f 1
dl 0
loc 46
ccs 19
cts 19
cp 1
rs 8.8017
cc 6
nc 33
nop 2
crap 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\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 626
{
21
    public function __construct(
22 626
        private readonly ?ScopeInterface $scope = new Container(),
23
    ) {}
24
25
    /**
26
     * @throws \Throwable
27 177
     */
28
    final protected function runScope(SpanInterface $span, callable $callback): mixed
29 177
    {
30 177
        $container = ContainerScope::getContainer();
31 112
        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
            ], static fn(InvokerInterface $invoker): mixed => $invoker->invoke($callback));
36
        }
37 125
38 124
        if ($container instanceof Container) {
39 124
            $invoker = $container;
40
            $binder = $container;
41
            $scope = $container;
42 1
        } else {
43
            /** @var ScopeInterface $scope */
44 1
            $scope = $container->get(ScopeInterface::class);
45
            /** @var InvokerInterface $invoker */
46
            $invoker = $container->get(InvokerInterface::class);
47
            /** @var BinderInterface $binder */
48 125
            $binder = $container->get(BinderInterface::class);
49 65
        }
50 65
51
        try {
52
            $prevSpan = $container->get(SpanInterface::class);
53 125
        } catch (\Throwable) {
54
            $prevSpan = null;
55 125
        }
56
57 125
        $binder->bindSingleton(SpanInterface::class, $span, true);
0 ignored issues
show
Unused Code introduced by
The call to Spiral\Core\BinderInterface::bindSingleton() has too many arguments starting with true. ( Ignorable by Annotation )

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

57
        $binder->/** @scrutinizer ignore-call */ 
58
                 bindSingleton(SpanInterface::class, $span, true);

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
58 65
59 125
        try {
60
            return $prevSpan === null
61
                ? $scope->runScope(
62
                    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

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