Passed
Pull Request — master (#817)
by Maxim
10:47 queued 04:55
created

Tracer::clean()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 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 410
    public function __toString(): string
18
    {
19 410
        $result = [];
20 410
        if ($this->traces !== []) {
21 410
            $result[] = 'Container trace list:';
22
23 410
            foreach ($this->traces as $item) {
24 410
                $result[] = (string) $item;
25
            }
26
        }
27
28 410
        return \implode(PHP_EOL, $result);
29
    }
30
31 627
    public function traceAutowire(string $alias, string $context = null): void
32
    {
33 627
        $this->trace($alias, 'Autowiring', $context);
34
    }
35
36 659
    public function traceBinding(string $alias, string|array|object $binding, string $context = null): void
37
    {
38 659
        $message = match (true) {
39 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

39
            \is_string($binding) => \sprintf('Binding found `%s`', /** @scrutinizer ignore-type */ $binding),
Loading history...
40 641
            \is_object($binding) => \sprintf('Binding found, the instance of `%s`', $binding::class),
41 343
            default => 'Binding found'
42
        };
43
44 659
        $this->trace($alias, $message, $context);
45
    }
46
47 404
    public function getRootConstructedClass(): string
48
    {
49 404
        return $this->traces[0]->alias;
50
    }
51
52 447
    public function clean(): void
53
    {
54 447
        $this->traces = [];
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