Passed
Pull Request — master (#1115)
by Maxim
22:29
created

Trace   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 58
Duplicated Lines 0 %

Test Coverage

Coverage 90.63%

Importance

Changes 0
Metric Value
wmc 10
eloc 32
dl 0
loc 58
ccs 29
cts 32
cp 0.9063
rs 10
c 0
b 0
f 0

4 Methods

Rating   Name   Duplication   Size   Complexity  
A __toString() 0 7 2
A __construct() 0 4 1
A renderArray() 0 21 5
A stringifyValue() 0 11 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal\Tracer;
6
7
use Spiral\Core\Exception\Traits\ClosureRendererTrait;
8
9
/**
10
 * @internal
11
 */
12
final class Trace implements \Stringable
13
{
14
    use ClosureRendererTrait;
0 ignored issues
show
Bug introduced by
The trait Spiral\Core\Exception\Traits\ClosureRendererTrait requires the property $class which is not provided by Spiral\Core\Internal\Tracer\Trace.
Loading history...
15
16
    private const ARRAY_MAX_LEVEL = 3;
17
18
    public readonly ?string $alias;
19
20 1176
    public function __construct(
21
        public array $info = [],
22
    ) {
23 1176
        $this->alias = $info['alias'] ?? null;
0 ignored issues
show
Bug introduced by
The property alias is declared read-only in Spiral\Core\Internal\Tracer\Trace.
Loading history...
24
    }
25
26 731
    public function __toString(): string
27
    {
28 731
        $info = [];
29 731
        foreach ($this->info as $key => $item) {
30 731
            $info[] = "$key: {$this->stringifyValue($item)}";
31
        }
32 731
        return \implode("\n", $info);
33
    }
34
35 731
    private function stringifyValue(mixed $item): string
36
    {
37
        return match (true) {
38 731
            \is_string($item) => "'$item'",
39 731
            \is_scalar($item) => \var_export($item, true),
40 731
            $item instanceof \Closure => $this->renderClosureSignature(new \ReflectionFunction($item)),
41 731
            $item instanceof \ReflectionFunctionAbstract => $this->renderClosureSignature($item),
42 731
            $item instanceof \UnitEnum => $item::class . "::$item->name",
43 731
            \is_object($item) => $item instanceof \Stringable ? (string) $item : 'instance of ' . $item::class,
44 724
            \is_array($item) => $this->renderArray($item),
45 731
            default => \get_debug_type($item),
46
        };
47
    }
48
49 8
    private function renderArray(array $array, int $level = 0): string
50
    {
51 8
        if ($array === []) {
52
            return '[]';
53
        }
54 8
        if ($level >= self::ARRAY_MAX_LEVEL) {
55
            return 'array';
56
        }
57 8
        $result = [];
58 8
        foreach ($array as $key => $value) {
59 8
            $result[] = \sprintf(
60 8
                '%s: %s',
61 8
                $key,
62 8
                \is_array($value)
63
                    ? $this->renderArray($value, $level + 1)
64 8
                    : $this->stringifyValue($value),
65 8
            );
66
        }
67
68 8
        $pad = \str_repeat('  ', $level);
69 8
        return "[\n  $pad" . \implode(",\n  $pad", $result) . "\n$pad]";
70
    }
71
}
72