Passed
Pull Request — master (#817)
by Maxim
06:45
created

Tracer   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 50
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 8
eloc 17
dl 0
loc 50
ccs 21
cts 21
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A traceAutowire() 0 3 1
A clean() 0 3 1
A trace() 0 3 1
A traceBinding() 0 9 1
A __toString() 0 12 3
A getRootConstructedClass() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
/**
8
 * @internal
9
 */
10
final class Tracer implements \Stringable
11
{
12
    /**
13
     * @var Trace[]
14
     */
15
    private array $traces = [];
16
17 627
    public function traceAutowire(string $alias, string $context = null): void
18
    {
19 627
        $this->trace($alias, 'Autowiring', $context);
20
    }
21
22 659
    public function traceBinding(string $alias, string|array|object $binding, string $context = null): void
23
    {
24 659
        $message = match (true) {
25 659
            \is_string($binding) => \sprintf('Binding found `%s`', $binding),
0 ignored issues
show
Bug introduced by
$binding of type array is incompatible with the type double|integer|string expected by parameter $values of sprintf(). ( Ignorable by Annotation )

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

25
            \is_string($binding) => \sprintf('Binding found `%s`', /** @scrutinizer ignore-type */ $binding),
Loading history...
26 641
            \is_object($binding) => \sprintf('Binding found, the instance of `%s`', $binding::class),
27 343
            default => 'Binding found'
28
        };
29
30 659
        $this->trace($alias, $message, $context);
31
    }
32
33 404
    public function getRootConstructedClass(): string
34
    {
35 404
        return $this->traces[0]->alias;
36
    }
37
38 343
    public function clean(): void
39
    {
40 343
        $this->traces = [];
41
    }
42
43 410
    public function __toString(): string
44
    {
45 410
        $result = [];
46 410
        if ($this->traces !== []) {
47 410
            $result[] = 'Container trace list:';
48
49 410
            foreach ($this->traces as $item) {
50 410
                $result[] = (string) $item;
51
            }
52
        }
53
54 410
        return \implode(PHP_EOL, $result);
55
    }
56
57 736
    private function trace(string $alias, string $information, string $context = null): void
58
    {
59 736
        $this->traces[] = new Trace($alias, $information, $context);
60
    }
61
}
62