IncidentsHelper::getStacktrace()   C
last analyzed

Complexity

Conditions 15
Paths 82

Size

Total Lines 60
Code Lines 40

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 20
CRAP Score 38.9162

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 15
eloc 40
nc 82
nop 2
dl 0
loc 60
ccs 20
cts 38
cp 0.5263
crap 38.9162
rs 5.9166
c 1
b 0
f 0

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
/**
4
 * Incidents View helper.
5
 *
6
 * phpMyAdmin Error reporting server
7
 * Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
8
 *
9
 * Licensed under The MIT License
10
 * For full copyright and license information, please see the LICENSE.txt
11
 * Redistributions of files must retain the above copyright notice.
12
 *
13
 * @copyright Copyright (c) phpMyAdmin project (https://www.phpmyadmin.net/)
14
 * @license   https://opensource.org/licenses/mit-license.php MIT License
15
 *
16
 * @see      https://www.phpmyadmin.net/
17
 */
18
19
namespace App\View\Helper;
20
21
use function count;
22
use function gettype;
23
use function htmlspecialchars;
24
use function implode;
25
use function is_array;
26
use function is_string;
27
use function json_decode;
28
use function strlen;
29
use function substr;
30
31
/**
32
 * Incidents View helper.
33
 */
34
class IncidentsHelper extends AppHelper
35
{
36
    /**
37
     * @param mixed $incidents
38
     * @return string comma separated list
39
     */
40 7
    public function createIncidentsLinks($incidents): string
41
    {
42 7
        $links = [];
43 7
        foreach ($incidents as $incident) {
44 7
            $links[] = $this->linkToIncident($incident);
45
        }
46
47 7
        return implode(', ', $links);
48
    }
49
50
    /**
51
     * @param mixed $incident The incident
52
     * @return string HTML <a> code
53
     */
54 7
    public function linkToIncident($incident): string
55
    {
56 7
        $incidentId = $incident['id'];
57
58 7
        return '<a href="/' . BASE_DIR . 'incidents/view/' . $incidentId . '">#' . $incidentId . '</a>';
59
    }
60
61
    /**
62
     * @param mixed $incidents The incidents
63
     * @return string HTML code
64
     */
65 7
    public function incidentsDescriptions($incidents): string
66
    {
67 7
        $descriptions = '';
68 7
        foreach ($incidents as $incident) {
69 7
            $descriptions .= '<span>Incident ';
70 7
            $descriptions .= $this->linkToIncident($incident);
71 7
            $descriptions .= ':</span>';
72 7
            $descriptions .= '<pre>';
73 7
            $descriptions .= htmlspecialchars($incident['steps']);
74 7
            $descriptions .= '</pre>';
75
        }
76
77 7
        return $descriptions;
78
    }
79
80
    /**
81
     * @param mixed  $incident The incident
82
     * @param string $divClass A class for the div
83
     * @return string HTML code
84
     */
85 14
    public function getStacktrace($incident, string $divClass): string
86
    {
87 14
        $html = '';
88 14
        $html .= '<div class="' . $divClass . '">';
89
90 14
        if (is_string($incident['stacktrace'])) {
91 7
            $incident['stacktrace'] =
92 7
                    json_decode($incident['stacktrace'], true);
93
        }
94
95 14
        foreach ($incident['stacktrace'] as $level) {
96 14
            $exception_type = ($incident['exception_type'] ? 'php' : 'js');
97 14
            $html .= $this->getStackLevelInfo($level, $exception_type);
98
            $html .= "<pre class='brush: "
99 14
                . $exception_type
100 14
                . '; tab-size: 2';
101 14
            if (isset($level['line']) && $level['line']) {
102
                if ($incident['exception_type']) {
103
                    $html .= '; first-line: ' . (int) $level['line'];
104
                } elseif ((int) $level['line'] > 5) {
105
                    $html .= '; first-line: ' . ((int) $level['line'] - 5);
106
                }
107
108
                $html .= '; highlight: [' . (int) $level['line'] . ']';
109
            }
110
111 14
            $html .= "'>";
112
113 14
            if ($exception_type === 'js') {
114 14
                if (isset($level['context'])) {
115 14
                    $ctx = is_string($level['context']) ? $level['context'] : implode("\n", $level['context']);
116 14
                    $html .= htmlspecialchars($ctx);
117
                }
118
            } else {
119
                $html .= htmlspecialchars($level['function']);
120
                $html .= '(';
121
                $argList = '';
122
                if (isset($level['args']) && is_array($level['args']) && count($level['args']) > 0) {
123
                    foreach ($level['args'] as $arg) {
124
                        $argList .= "\n"
125
                            . gettype($arg)
126
                            . ' => '
127
                            . $arg;
128
                        $argList .= ',';
129
                    }
130
131
                    $argList = substr($argList, 0, strlen($argList) - 1);
132
                    $argList .= "\n";
133
                }
134
135
                $html .= htmlspecialchars($argList);
136
                $html .= ')';
137
            }
138
139 14
            $html .= '</pre>';
140
        }
141
142 14
        $html .= '</div>';
143
144 14
        return $html;
145
    }
146
147
    /**
148
     * @param mixed  $level          The level
149
     * @param string $exception_type The execption type (php/js)
150
     * @return string HTML code
151
     */
152 14
    protected function getStackLevelInfo($level, string $exception_type = 'js'): string
153
    {
154 14
        $html = '<span>';
155 14
        if ($exception_type === 'js') {
156 4
            $elements = [
157 10
                'filename',
158
                'scriptname',
159
                'line',
160
                'func',
161
                'column',
162
            ];
163
        } else {
164
            $elements = [
165
                'file',
166
                'line',
167
                'function',
168
                'class',
169
            ];
170
        }
171
172 14
        foreach ($elements as $element) {
173 14
            if (! isset($level[$element])) {
174 14
                continue;
175
            }
176
177
            $html .= $element . ': ' . $level[$element] . '; ';
178
        }
179
180 14
        $html .= '</span>';
181
182 14
        return $html;
183
    }
184
}
185