Code Coverage
 
Lines
Branches
Paths
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
20 / 20
93.10% covered (success)
93.10%
27 / 29
52.63% covered (warning)
52.63%
10 / 19
33.33% covered (danger)
33.33%
1 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
PreparesValue
100.00% covered (success)
100.00%
20 / 20
93.10% covered (success)
93.10%
27 / 29
52.63% covered (warning)
52.63%
10 / 19
100.00% covered (success)
100.00%
3 / 3
34.83
100.00% covered (success)
100.00%
1 / 1
 prepare
100.00% covered (success)
100.00%
11 / 11
100.00% covered (success)
100.00%
17 / 17
53.85% covered (warning)
53.85%
7 / 13
100.00% covered (success)
100.00%
1 / 1
16.96
 prepareArray
100.00% covered (success)
100.00%
2 / 2
75.00% covered (warning)
75.00%
3 / 4
50.00% covered (danger)
50.00%
1 / 2
100.00% covered (success)
100.00%
1 / 1
2.50
 getExceptionMessage
100.00% covered (success)
100.00%
7 / 7
87.50% covered (warning)
87.50%
7 / 8
50.00% covered (danger)
50.00%
2 / 4
100.00% covered (success)
100.00%
1 / 1
4.12
1<?php
2
3declare(strict_types=1);
4
5namespace Celemas\Log\Formatter;
6
7use DateTimeInterface;
8use Stringable;
9use Throwable;
10
11trait PreparesValue
12{
13    private function prepare(
14        mixed $value,
15        bool $includeTraceback,
16        string $tracebackIndent = '',
17    ): string {
18        return match (true) {
19            // Exceptions must be first as they are Stringable
20            $value instanceof Throwable => $this->getExceptionMessage(
21                $value,
22                $includeTraceback,
23                $tracebackIndent,
24            ),
25            is_scalar($value) || $value instanceof Stringable => (string) $value,
26            $value instanceof DateTimeInterface => $value->format('Y-m-d H:i:s T'),
27            is_object($value) => '[Instance of ' . $value::class . ']',
28            is_array($value) => $this->prepareArray($value),
29            is_null($value) => '[null]',
30            default => '[' . get_debug_type($value) . ']',
31        };
32    }
33
34    /** @param array<array-key, mixed> $value */
35    private function prepareArray(array $value): string
36    {
37        $encoded = json_encode($value, JSON_UNESCAPED_SLASHES);
38
39        return '[Array ' . ($encoded !== false ? $encoded : '...') . ']';
40    }
41
42    private function getExceptionMessage(
43        Throwable $exception,
44        bool $includeTraceback,
45        string $tracebackIndent,
46    ): string {
47        $message = $exception::class . ': ' . $exception->getMessage();
48
49        if ($includeTraceback) {
50            $trace = $exception->getTraceAsString();
51
52            if ($tracebackIndent) {
53                // Indent each frame: split on '#', rejoin with indent+'#'
54                $trace = implode($tracebackIndent . '#', explode('#', $trace));
55            }
56
57            $message .= "\n" . $trace;
58        }
59
60        return $message;
61    }
62}

Branches

Below are the source code lines that represent each code branch as identified by Xdebug. Please note a branch is not necessarily coterminous with a line, a line may contain multiple branches and therefore show up more than once. Please also be aware that some branches may be implicit rather than explicit, e.g. an if statement always has an else as part of its logical flow even if you didn't write one.

PreparesValue->getExceptionMessage
43        Throwable $exception,
44        bool $includeTraceback,
45        string $tracebackIndent,
46    ): string {
47        $message = $exception::class . ': ' . $exception->getMessage();
48
49        if ($includeTraceback) {
50            $trace = $exception->getTraceAsString();
51
52            if ($tracebackIndent) {
54                $trace = implode($tracebackIndent . '#', explode('#', $trace));
54                $trace = implode($tracebackIndent . '#', explode('#', $trace));
54                $trace = implode($tracebackIndent . '#', explode('#', $trace));
54                $trace = implode($tracebackIndent . '#', explode('#', $trace));
55            }
56
57            $message .= "\n" . $trace;
57            $message .= "\n" . $trace;
58        }
59
60        return $message;
60        return $message;
61    }
PreparesValue->prepare
14        mixed $value,
15        bool $includeTraceback,
16        string $tracebackIndent = '',
17    ): string {
18        return match (true) {
19            // Exceptions must be first as they are Stringable
20            $value instanceof Throwable => $this->getExceptionMessage(
25            is_scalar($value) || $value instanceof Stringable => (string) $value,
25            is_scalar($value) || $value instanceof Stringable => (string) $value,
25            is_scalar($value) || $value instanceof Stringable => (string) $value,
26            $value instanceof DateTimeInterface => $value->format('Y-m-d H:i:s T'),
27            is_object($value) => '[Instance of ' . $value::class . ']',
28            is_array($value) => $this->prepareArray($value),
29            is_null($value) => '[null]',
29            is_null($value) => '[null]',
20            $value instanceof Throwable => $this->getExceptionMessage(
21                $value,
25            is_scalar($value) || $value instanceof Stringable => (string) $value,
26            $value instanceof DateTimeInterface => $value->format('Y-m-d H:i:s T'),
27            is_object($value) => '[Instance of ' . $value::class . ']',
28            is_array($value) => $this->prepareArray($value),
29            is_null($value) => '[null]',
30            default => '[' . get_debug_type($value) . ']',
30            default => '[' . get_debug_type($value) . ']',
31        };
32    }
PreparesValue->prepareArray
35    private function prepareArray(array $value): string
36    {
37        $encoded = json_encode($value, JSON_UNESCAPED_SLASHES);
38
39        return '[Array ' . ($encoded !== false ? $encoded : '...') . ']';
39        return '[Array ' . ($encoded !== false ? $encoded : '...') . ']';
39        return '[Array ' . ($encoded !== false ? $encoded : '...') . ']';
39        return '[Array ' . ($encoded !== false ? $encoded : '...') . ']';
40    }