Passed
Push — master ( c1ff08...0c4511 )
by Aleksei
06:40 queued 19s
created

PlainRenderer::renderTrace()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 52
Code Lines 33

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 10.0025

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 33
c 1
b 0
f 0
dl 0
loc 52
ccs 33
cts 34
cp 0.9706
rs 7.6666
cc 10
nc 20
nop 2
crap 10.0025

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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 9
        $rootDir = \getcwd();
33
34 9
        foreach ($exceptions as $exception) {
35 9
            $file = \str_starts_with($exception->getFile(), $rootDir)
36 9
                ? \substr($exception->getFile(), \strlen($rootDir) + 1)
37
                : $exception->getFile();
38
39 9
            $row = \sprintf(
40 9
                "[%s]\n%s in %s:%s\n",
41 9
                $exception::class,
42 9
                $exception->getMessage(),
43 9
                $file,
44 9
                $exception->getLine(),
45 9
            );
46
47 9
            if ($verbosity->value >= Verbosity::DEBUG->value) {
48 4
                $row .= $this->renderTrace($exception, new Highlighter(new PlainStyle()));
49 5
            } elseif ($verbosity->value >= Verbosity::VERBOSE->value) {
50 2
                $row .= $this->renderTrace($exception);
51
            }
52
53 9
            $result[] = $row;
54
        }
55
56 9
        $this->lines = [];
57
58 9
        return \implode('', \array_reverse($result));
59
    }
60
61
    /**
62
     * Render exception call stack.
63
     */
64 6
    private function renderTrace(\Throwable $e, Highlighter $h = null): string
65
    {
66 6
        $stacktrace = $this->getStacktrace($e);
67 6
        if ($stacktrace === []) {
68
            return '';
69
        }
70
71 6
        $result = "\n";
72 6
        $rootDir = \getcwd();
73
74 6
        $pad = \strlen((string)\count($stacktrace));
75
76 6
        foreach ($stacktrace as $i => $trace) {
77 6
            if (isset($trace['type'], $trace['class'])) {
78 6
                $line = \sprintf(
79 6
                    '%s. %s%s%s()',
80 6
                    \str_pad((string)((int) $i + 1), $pad, ' ', \STR_PAD_LEFT),
81 6
                    $trace['class'],
82 6
                    $trace['type'],
83 6
                    $trace['function']
84 6
                );
85
            } else {
86 6
                $line = $trace['function'];
87
            }
88
89 6
            if (isset($trace['file'])) {
90 6
                $file = (string) $trace['file'];
91 6
                \str_starts_with($file, $rootDir) and $file = \substr($file, \strlen($rootDir) + 1);
92
93 6
                $line .= \sprintf(' at %s:%s', $file, $trace['line']);
94
            }
95
96 6
            if (\in_array($line, $this->lines, true)) {
97 1
                continue;
98
            }
99
100 6
            $this->lines[] = $line;
101
102 6
            $result .= $line . "\n";
103
104 6
            if ($h !== null && !empty($trace['file']) && \is_file($trace['file'])) {
105 4
                $str = @\file_get_contents($trace['file']);
106 4
                $result .= $h->highlightLines(
107 4
                    $str,
0 ignored issues
show
Bug introduced by
It seems like $str can also be of type false; however, parameter $source of Spiral\Exceptions\Render...ghter::highlightLines() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

107
                    /** @scrutinizer ignore-type */ $str,
Loading history...
108 4
                    $trace['line'],
109 4
                    self::SHOW_LINES
110 4
                ) . "\n";
111 4
                unset($str);
112
            }
113
        }
114
115 6
        return $result . "\n";
116
    }
117
}
118