Passed
Push — master ( 41e710...91ffae )
by Aleksei
06:19 queued 21s
created

Tracer::pop()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3.0123

Importance

Changes 0
Metric Value
eloc 8
c 0
b 0
f 0
dl 0
loc 12
ccs 8
cts 9
cp 0.8889
rs 10
cc 3
nc 3
nop 1
crap 3.0123
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Spiral\Core\Internal\Tracer\Trace;
8
9
/**
10
 * @internal
11
 */
12
final class Tracer implements \Stringable
13
{
14
    /**
15
     * Trace blocks
16
     *
17
     * @var Trace[][]
18
     */
19
    private array $traces = [];
20
21
    /**
22
     * @param string $header Message before stack list
23
     */
24
    public function combineTraceMessage(string $header): string
25
    {
26
        return "$header\n$this";
27
    }
28
29 791
    public function getTraces(): array
30
    {
31 791
        return $this->traces;
32
    }
33
34 1237
    public function push(bool $nextLevel, mixed ...$details): void
35
    {
36 1237
        $trace = $details === [] ? null : new Trace($details);
37 1237
        if ($nextLevel || $this->traces === []) {
38 1237
            $this->traces[] = $trace === null ? [] : [$trace];
39 945
        } elseif ($trace !== null) {
40 945
            $this->traces[\array_key_last($this->traces)][] = $trace;
41
        }
42
    }
43
44 1237
    public function pop(bool $previousLevel = false): void
45
    {
46 1237
        if ($this->traces === []) {
47
            return;
48
        }
49 1237
        if ($previousLevel) {
50 1191
            \array_pop($this->traces);
51 1191
            return;
52
        }
53 1237
        $key = \array_key_last($this->traces);
54 1237
        $list = &$this->traces[$key];
55 1237
        \array_pop($list);
56
    }
57
58 29
    public function getRootAlias(): string
59
    {
60 29
        return $this->traces[0][0]->alias ?? '';
61
    }
62
63
    public function __toString(): string
64
    {
65
        return $this->traces === [] ? '' : "Resolving trace:\n" . self::renderTraceList($this->traces);
66
    }
67
68
    /**
69
     * @param Trace[][] $blocks
70
     */
71 791
    public static function renderTraceList(array $blocks): string
72
    {
73 791
        $result = [];
74 791
        $i = 0;
75 791
        foreach ($blocks as $block) {
76 791
            \array_push($result, ...self::blockToStringList($block, $i++));
77
        }
78 791
        return \implode("\n", $result);
79
    }
80
81
    /**
82
     * @param Trace[] $items
83
     * @param int<0, max> $level
84
     *
85
     * @return string[]
86
     */
87 791
    private static function blockToStringList(array $items, int $level = 0): array
88
    {
89 791
        $result = [];
90 791
        $padding = \str_repeat('  ', $level);
91 791
        $firstPrefix = "$padding- ";
92
        // Separator
93 791
        $s = "\n";
94 791
        $nexPrefix = "$s$padding  ";
95 791
        foreach ($items as $item) {
96 791
            $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string) $item);
97
        }
98 791
        return $result;
99
    }
100
}
101