Test Failed
Pull Request — master (#1214)
by
unknown
12:10
created

AbstractTracer::runScope()   B

Complexity

Conditions 6
Paths 33

Size

Total Lines 39
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 6

Importance

Changes 4
Bugs 0 Features 1
Metric Value
eloc 27
c 4
b 0
f 1
dl 0
loc 39
ccs 20
cts 20
cp 1
rs 8.8657
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\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
        if ($prevSpan === null) {
55 125
            $binder->bindSingleton(TracerInterface::class, $this, 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

55
            $binder->/** @scrutinizer ignore-call */ 
56
                     bindSingleton(TracerInterface::class, $this, 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...
56
        }
57 125
58 65
        try {
59 125
            return $invoker->invoke($callback);
60
        } finally {
61
            if ($prevSpan === null) {
62
                $binder->removeBinding(SpanInterface::class);
63
                $binder->removeBinding(TracerInterface::class);
64
            } else {
65
                $binder->bindSingleton(SpanInterface::class, $prevSpan);
66
            }
67
        }
68
    }
69
}
70