Passed
Pull Request — master (#817)
by Maxim
07:12
created

Tracer::__toString()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 2

Importance

Changes 3
Bugs 0 Features 0
Metric Value
eloc 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
c 3
b 0
f 0
cc 2
nc 2
nop 0
crap 2
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
     * Trace blocks
14
     *
15
     * @var Trace[][]
16
     */
17
    private array $traces = [];
18
19 421
    public function __toString(): string
20
    {
21 421
        return $this->traces === [] ? '' : "Container trace list:\n" . $this->renderTraceList($this->traces);
22
    }
23
24
    /**
25
     * @param string $header Message before stack list
26
     * @param bool $lastBlock Generate trace list only for last block
27
     * @param bool $clear Remove touched trace list
28
     */
29 421
    public function combineTraceMessage(string $header, bool $lastBlock = false, bool $clear = false): string
0 ignored issues
show
Unused Code introduced by
The parameter $lastBlock is not used and could be removed. ( Ignorable by Annotation )

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

29
    public function combineTraceMessage(string $header, /** @scrutinizer ignore-unused */ bool $lastBlock = false, bool $clear = false): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
Unused Code introduced by
The parameter $clear is not used and could be removed. ( Ignorable by Annotation )

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

29
    public function combineTraceMessage(string $header, bool $lastBlock = false, /** @scrutinizer ignore-unused */ bool $clear = false): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
30
    {
31 421
        return "$header\n$this";
32
    }
33
34 742
    public function push(string $alias, bool $nextLevel = false, mixed ...$details): void
35
    {
36 742
        $trace = new Trace($alias, $details);
37 742
        if ($nextLevel || $this->traces === []) {
38 742
            $this->traces[] = [$trace];
39
        } else {
40 594
            $this->traces[\array_key_last($this->traces)][] = $trace;
41
        }
42
    }
43
44 742
    public function pop(bool $previousLevel = false): void
45
    {
46 742
        if ($this->traces === []) {
47
            return;
48
        }
49 742
        if ($previousLevel) {
50 543
            \array_pop($this->traces);
51 543
            return;
52
        }
53 742
        $key = \array_key_last($this->traces);
54 742
        $list = &$this->traces[$key];
55 742
        \array_pop($list);
56 742
        if ($list === []) {
57 742
            unset($this->traces[$key]);
58
        }
59
    }
60
61 415
    public function getRootAlias(): string
62
    {
63 415
        return $this->traces[0][0]->alias ?? '';
64
    }
65
66
    /**
67
     * @param Trace[][] $blocks
68
     */
69 421
    private function renderTraceList(array $blocks): string
70
    {
71 421
        $result = [];
72 421
        $i = 0;
73 421
        foreach ($blocks as $block) {
74 421
            \array_push($result, ...$this->blockToStringList($block, $i++));
75
        }
76 421
        return \implode("\n", $result);
77
    }
78
79
    /**
80
     * @param Trace[] $items
81
     * @param int<0, max> $level
82
     *
83
     * @return string[]
84
     */
85 421
    private function blockToStringList(array $items, int $level = 0): array
86
    {
87 421
        $result = [];
88 421
        $padding = \str_repeat('  ', $level);
89 421
        $firstPrefix = "$padding- ";
90
        // Separator
91 421
        $s = "\n";
92 421
        $nexPrefix = "$s$padding  ";
93 421
        foreach ($items as $item) {
94 421
            $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item);
95
        }
96 421
        return $result;
97
    }
98
}
99