Passed
Pull Request — master (#1122)
by Aleksei
24:08
created

PlainRenderer::renderTrace()   B

Complexity

Conditions 10
Paths 20

Size

Total Lines 53
Code Lines 34

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 33
CRAP Score 10.0186

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 34
c 1
b 0
f 0
dl 0
loc 53
ccs 33
cts 35
cp 0.9429
rs 7.6666
cc 10
nc 20
nop 2
crap 10.0186

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 = \str_starts_with($trace['file'], $rootDir)
91 6
                    ? \substr($trace['file'], \strlen($rootDir) + 1)
92
                    : $trace['file'];
93
94 6
                $line .= \sprintf(' at %s:%s', $file, $trace['line']);
95
            }
96
97 6
            if (\in_array($line, $this->lines, true)) {
98 1
                continue;
99
            }
100
101 6
            $this->lines[] = $line;
102
103 6
            $result .= $line . "\n";
104
105 6
            if ($h !== null && !empty($trace['file']) && \is_file($trace['file'])) {
106 4
                $str = @\file_get_contents($trace['file']);
107 4
                $result .= $h->highlightLines(
108 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

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