Passed
Push — master ( 6245ad...909f9b )
by William
02:36
created

IncidentsHelper::getStacktrace()   C

Complexity

Conditions 15
Paths 82

Size

Total Lines 54
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
c 1
b 0
f 0
nc 82
nop 2
dl 0
loc 54
ccs 20
cts 38
cp 0.5263
crap 38.9162
rs 5.9166

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_string;
26
use function json_decode;
27
use function strlen;
28
use function substr;
29
use function is_array;
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
                $html .= '; highlight: [' . (int) $level['line'] . ']';
108
            }
109 14
            $html .= "'>";
110
111 14
            if ($exception_type === 'js') {
112 14
                if (isset($level['context'])) {
113 14
                    $ctx = is_string($level['context']) ? $level['context'] : implode("\n", $level['context']);
114 14
                    $html .= htmlspecialchars($ctx);
115
                }
116
            } else {
117
                $html .= htmlspecialchars($level['function']);
118
                $html .= '(';
119
                $argList = '';
120
                if (isset($level['args']) && is_array($level['args']) && count($level['args']) > 0) {
121
                    foreach ($level['args'] as $arg) {
122
                        $argList .= "\n"
123
                            . gettype($arg)
124
                            . ' => '
125
                            . $arg;
126
                        $argList .= ',';
127
                    }
128
                    $argList = substr($argList, 0, strlen($argList) - 1);
129
                    $argList .= "\n";
130
                }
131
                $html .= htmlspecialchars($argList);
132
                $html .= ')';
133
            }
134 14
            $html .= '</pre>';
135
        }
136 14
        $html .= '</div>';
137
138 14
        return $html;
139
    }
140
141
    /**
142
     * @param mixed  $level          The level
143
     * @param string $exception_type The execption type (php/js)
144
     * @return string HTML code
145
     */
146 14
    protected function getStackLevelInfo($level, string $exception_type = 'js'): string
147
    {
148 14
        $html = '<span>';
149 14
        if ($exception_type === 'js') {
150 4
            $elements = [
151 10
                'filename',
152
                'scriptname',
153
                'line',
154
                'func',
155
                'column',
156
            ];
157
        } else {
158
            $elements = [
159
                'file',
160
                'line',
161
                'function',
162
                'class',
163
            ];
164
        }
165 14
        foreach ($elements as $element) {
166 14
            if (! isset($level[$element])) {
167 14
                continue;
168
            }
169
170
            $html .= $element . ': ' . $level[$element] . '; ';
171
        }
172 14
        $html .= '</span>';
173
174 14
        return $html;
175
    }
176
}
177