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

Trace::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 0
dl 0
loc 4
ccs 1
cts 1
cp 1
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Core\Internal;
6
7
use Closure;
8
use ReflectionFunction;
9
use Spiral\Core\Exception\Traits\ClosureRendererTrait;
10
11
/**
12
 * @internal
13
 */
14
final class Trace implements \Stringable
15
{
16
    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...
17
18 742
    public function __construct(
19
        public readonly string $alias,
20
        public array $info,
21
    ) {
22
    }
23
24 421
    public function __toString(): string
25
    {
26 421
        $info = [$this->alias];
27 421
        foreach ($this->info as $key => $item) {
28 421
            $info[] = "$key: {$this->stringifyValue($item)}";
29
        }
30 421
        return \implode("\n", $info);
31
    }
32
33 421
    private function stringifyValue(mixed $item): string
34
    {
35
        return match (true) {
36 421
            \is_string($item) => "'$item'",
37 414
            \is_scalar($item) => (string)$item,
38 414
            $item instanceof Closure => $this->renderClosureSignature(new ReflectionFunction($item)),
39 414
            \is_object($item) => 'instance of ' . $item::class,
40 421
            default => \gettype($item),
41
        };
42
    }
43
}
44