Passed
Push — master ( 083ff7...c84473 )
by William
03:02
created

IncidentsHelper   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 140
Duplicated Lines 0 %

Test Coverage

Coverage 69.12%

Importance

Changes 0
Metric Value
eloc 74
dl 0
loc 140
ccs 47
cts 68
cp 0.6912
rs 10
c 0
b 0
f 0
wmc 21

5 Methods

Rating   Name   Duplication   Size   Complexity  
A linkToIncident() 0 5 1
C getStacktrace() 0 53 12
A incidentsDescriptions() 0 13 2
A createIncidentsLinks() 0 8 2
A getStackLevelInfo() 0 29 4
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
30
/**
31
 * Incidents View helper.
32
 */
33
class IncidentsHelper extends AppHelper
34
{
35
    /**
36
     * @param mixed $incidents
37
     * @return string comma separated list
38
     */
39 7
    public function createIncidentsLinks($incidents): string
40
    {
41 7
        $links = [];
42 7
        foreach ($incidents as $incident) {
43 7
            $links[] = $this->linkToIncident($incident);
44
        }
45
46 7
        return implode(', ', $links);
47
    }
48
49
    /**
50
     * @param mixed $incident The incident
51
     * @return string HTML <a> code
52
     */
53 7
    public function linkToIncident($incident): string
54
    {
55 7
        $incidentId = $incident['id'];
56
57 7
        return '<a href="/' . BASE_DIR . 'incidents/view/' . $incidentId . '">#' . $incidentId . '</a>';
58
    }
59
60
    /**
61
     * @param mixed $incidents The incidents
62
     * @return string HTML code
63
     */
64 7
    public function incidentsDescriptions($incidents): string
65
    {
66 7
        $descriptions = '';
67 7
        foreach ($incidents as $incident) {
68 7
            $descriptions .= '<span>Incident ';
69 7
            $descriptions .= $this->linkToIncident($incident);
70 7
            $descriptions .= ':</span>';
71 7
            $descriptions .= '<pre>';
72 7
            $descriptions .= htmlspecialchars($incident['steps']);
73 7
            $descriptions .= '</pre>';
74
        }
75
76 7
        return $descriptions;
77
    }
78
79
    /**
80
     * @param mixed  $incident The incident
81
     * @param string $divClass A class for the div
82
     * @return string HTML code
83
     */
84 14
    public function getStacktrace($incident, string $divClass): string
85
    {
86 14
        $html = '';
87 14
        $html .= '<div class="' . $divClass . '">';
88
89 14
        if (is_string($incident['stacktrace'])) {
90 7
            $incident['stacktrace'] =
91 7
                    json_decode($incident['stacktrace'], true);
92
        }
93
94 14
        foreach ($incident['stacktrace'] as $level) {
95 14
            $exception_type = ($incident['exception_type'] ? 'php' : 'js');
96 14
            $html .= $this->getStackLevelInfo($level, $exception_type);
97
            $html .= "<pre class='brush: "
98 14
                . $exception_type
99 14
                . '; tab-size: 2';
100 14
            if (isset($level['line']) && $level['line']) {
101
                if ($incident['exception_type']) {
102
                    $html .= '; first-line: ' . (int) $level['line'];
103
                } elseif ((int) $level['line'] > 5) {
104
                    $html .= '; first-line: ' . ((int) $level['line'] - 5);
105
                }
106
                $html .= '; highlight: [' . (int) $level['line'] . ']';
107
            }
108 14
            $html .= "'>";
109
110 14
            if ($exception_type === 'js') {
111 14
                if (isset($level['context'])) {
112 14
                    $html .= htmlspecialchars(implode("\n", $level['context']));
113
                }
114
            } else {
115
                $html .= htmlspecialchars($level['function']);
116
                $html .= '(';
117
                $argList = '';
118
                if (count($level['args']) > 0) {
119
                    foreach ($level['args'] as $arg) {
120
                        $argList .= "\n"
121
                            . gettype($arg)
122
                            . ' => '
123
                            . $arg;
124
                        $argList .= ',';
125
                    }
126
                    $argList = substr($argList, 0, strlen($argList) - 1);
127
                    $argList .= "\n";
128
                }
129
                $html .= htmlspecialchars($argList);
130
                $html .= ')';
131
            }
132 14
            $html .= '</pre>';
133
        }
134 14
        $html .= '</div>';
135
136 14
        return $html;
137
    }
138
139
    /**
140
     * @param mixed  $level          The level
141
     * @param string $exception_type The execption type (php/js)
142
     * @return string HTML code
143
     */
144 14
    protected function getStackLevelInfo($level, string $exception_type = 'js'): string
145
    {
146 14
        $html = '<span>';
147 14
        if ($exception_type === 'js') {
148 4
            $elements = [
149 10
                'filename',
150
                'scriptname',
151
                'line',
152
                'func',
153
                'column',
154
            ];
155
        } else {
156
            $elements = [
157
                'file',
158
                'line',
159
                'function',
160
                'class',
161
            ];
162
        }
163 14
        foreach ($elements as $element) {
164 14
            if (! isset($level[$element])) {
165 14
                continue;
166
            }
167
168
            $html .= $element . ': ' . $level[$element] . '; ';
169
        }
170 14
        $html .= '</span>';
171
172 14
        return $html;
173
    }
174
}
175