Passed
Pull Request — master (#853)
by butschster
07:53
created

PlainRenderer::renderTrace()   B

Complexity

Conditions 8
Paths 14

Size

Total Lines 45
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 26
CRAP Score 8.0231

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 28
c 2
b 1
f 0
dl 0
loc 45
ccs 26
cts 28
cp 0.9286
rs 8.4444
cc 8
nc 14
nop 2
crap 8.0231
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Spiral\Exceptions\Renderer;
6
7
use Spiral\Exceptions\Style\PlainStyle;
8
use Spiral\Exceptions\Verbosity;
9
10
final class PlainRenderer extends AbstractRenderer
11
{
12
    protected const FORMATS = ['text/plain', 'text', 'plain', 'cli', 'console'];
13
    // Lines to show around targeted line.
14
    private const SHOW_LINES = 2;
15
16
    private array $lines = [];
17
18 9
    public function render(
19
        \Throwable $exception,
20
        ?Verbosity $verbosity = null,
21
        string $format = null
22
    ): string {
23 9
        $verbosity ??= $this->defaultVerbosity;
24 9
        $exceptions = [$exception];
25 9
        while ($exception = $exception->getPrevious()) {
26 1
            $exceptions[] = $exception;
27
        }
28
29 9
        $exceptions = \array_reverse($exceptions);
30
31 9
        $result = [];
32
33 9
        foreach ($exceptions as $exception) {
34 9
            $row = \sprintf(
35 9
                "[%s]\n%s in %s:%s\n",
36 9
                $exception::class,
37 9
                $exception->getMessage(),
38 9
                $exception->getFile(),
39 9
                $exception->getLine()
40 9
            );
41
42 9
            if ($verbosity->value >= Verbosity::DEBUG->value) {
43 3
                $row .= $this->renderTrace($exception, new Highlighter(new PlainStyle()));
44 6
            } elseif ($verbosity->value >= Verbosity::VERBOSE->value) {
45 1
                $row .= $this->renderTrace($exception);
46
            }
47
48 9
            $result[] = $row;
49
        }
50
51 9
        $this->lines = [];
52
53 9
        return \implode('', \array_reverse($result));
54
    }
55
56
    /**
57
     * Render exception call stack.
58
     */
59 4
    private function renderTrace(\Throwable $e, Highlighter $h = null): string
60
    {
61 4
        $stacktrace = $this->getStacktrace($e);
62 4
        if (empty($stacktrace)) {
63
            return '';
64
        }
65
66 4
        $result = "\nException Trace:\n";
67
68 4
        foreach ($stacktrace as $trace) {
69 4
            if (isset($trace['type'], $trace['class'])) {
70 4
                $line = \sprintf(
71 4
                    ' %s%s%s()',
72 4
                    $trace['class'],
73 4
                    $trace['type'],
74 4
                    $trace['function']
75 4
                );
76
            } else {
77 4
                $line = $trace['function'];
78
            }
79
80 4
            if (isset($trace['file'])) {
81 4
                $line .= \sprintf(' at %s:%s', $trace['file'], $trace['line']);
82
            } else {
83
                $line .= \sprintf(' at %s:%s', 'n/a', 'n/a');
84
            }
85
86 4
            if (\in_array($line, $this->lines, true)) {
87 4
                continue;
88
            }
89
90 4
            $this->lines[] = $line;
91
92 4
            $result .= $line . "\n";
93
94 4
            if ($h !== null && !empty($trace['file'])) {
95 3
                $result .= $h->highlightLines(
96 3
                    \file_get_contents($trace['file']),
97 3
                    $trace['line'],
98 3
                    self::SHOW_LINES
99 3
                ) . "\n";
100
            }
101
        }
102
103 4
        return $result . "\n";
104
    }
105
}
106