Passed
Pull Request — master (#817)
by Aleksei
06:27
created

Trace   A

Complexity

Total Complexity 9

Size/Duplication

Total Lines 57
Duplicated Lines 0 %

Test Coverage

Coverage 93.1%

Importance

Changes 0
Metric Value
wmc 9
eloc 32
dl 0
loc 57
ccs 27
cts 29
cp 0.931
rs 10
c 0
b 0
f 0

4 Methods

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