Passed
Pull Request — master (#817)
by Aleksei
15:00 queued 07:59
created

Tracer   A

Complexity

Total Complexity 17

Size/Duplication

Total Lines 84
Duplicated Lines 0 %

Test Coverage

Coverage 97.22%

Importance

Changes 5
Bugs 1 Features 0
Metric Value
wmc 17
eloc 31
dl 0
loc 84
ccs 35
cts 36
cp 0.9722
rs 10
c 5
b 1
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 3 2
A combineTraceMessage() 0 3 1
A blockToStringList() 0 12 2
A getRootAlias() 0 3 1
A pop() 0 12 3
A push() 0 7 6
A renderTraceList() 0 8 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(bool $nextLevel, mixed ...$details): void
35
    {
36 742
        $trace = $details === [] ? null : new Trace($details);
37 742
        if ($nextLevel || $this->traces === []) {
38 742
            $this->traces[] = $trace === null ? [] : [$trace];
39 683
        } elseif ($trace !== null) {
40 683
            $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 709
            \array_pop($this->traces);
51 709
            return;
52
        }
53 742
        $key = \array_key_last($this->traces);
54 742
        $list = &$this->traces[$key];
55 742
        \array_pop($list);
56
    }
57
58 415
    public function getRootAlias(): string
59
    {
60 415
        return $this->traces[0][0]->alias ?? '';
61
    }
62
63
    /**
64
     * @param Trace[][] $blocks
65
     */
66 421
    private function renderTraceList(array $blocks): string
67
    {
68 421
        $result = [];
69 421
        $i = 0;
70 421
        foreach ($blocks as $block) {
71 421
            \array_push($result, ...$this->blockToStringList($block, $i++));
72
        }
73 421
        return \implode("\n", $result);
74
    }
75
76
    /**
77
     * @param Trace[] $items
78
     * @param int<0, max> $level
79
     *
80
     * @return string[]
81
     */
82 421
    private function blockToStringList(array $items, int $level = 0): array
83
    {
84 421
        $result = [];
85 421
        $padding = \str_repeat('  ', $level);
86 421
        $firstPrefix = "$padding- ";
87
        // Separator
88 421
        $s = "\n";
89 421
        $nexPrefix = "$s$padding  ";
90 421
        foreach ($items as $item) {
91 421
            $result[] = $firstPrefix . \str_replace($s, $nexPrefix, (string)$item);
92
        }
93 421
        return $result;
94
    }
95
}
96