PreparesValue   A
last analyzed

Complexity

Total Complexity 7

Size/Duplication

Total Lines 38
Duplicated Lines 0 %

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
eloc 19
dl 0
loc 38
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0
wmc 7

2 Methods

Rating   Name   Duplication   Size   Complexity  
A getExceptionMessage() 0 18 3
A prepare() 0 15 4
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Conia\Log\Formatter;
6
7
use DateTimeInterface;
8
use Stringable;
9
use Throwable;
10
11
trait PreparesValue
12
{
13 5
    public function prepare(mixed $value, bool $includeTraceback, string $tracebackIndent = ''): string
14
    {
15 5
        return match (true) {
16
            // Exceptions must be first as they are Stringable
17 5
            is_object($value) && is_subclass_of($value, Throwable::class) => $this->getExceptionMessage(
18 5
                $value,
19 5
                $includeTraceback,
20 5
                $tracebackIndent
21 5
            ),
22 5
            (is_scalar($value) || (is_object($value) && ($value instanceof Stringable))) => (string)$value,
23 5
            $value instanceof DateTimeInterface => $value->format('Y-m-d H:i:s T'),
24 5
            is_object($value) => '[Instance of ' . $value::class . ']',
25 5
            is_array($value) => '[Array ' . json_encode($value, JSON_UNESCAPED_SLASHES) . ']',
26 5
            is_null($value) => '[null]',
27 5
            default => '[' . get_debug_type($value) . ']',
28 5
        };
29
    }
30
31 2
    protected function getExceptionMessage(
32
        Throwable $exception,
33
        bool $includeTraceback,
34
        string $tracebackIndent
35
    ): string {
36 2
        $message = $exception::class . ': ' . $exception->getMessage();
37
38 2
        if ($includeTraceback) {
39 2
            $trace = $exception->getTraceAsString();
40
41 2
            if ($tracebackIndent) {
42 1
                $trace = implode($tracebackIndent . '#', explode('#', $trace));
43
            }
44
45 2
            $message .= "\n" . $trace . "\n";
46
        }
47
48 2
        return $message;
49
    }
50
}
51