Completed
Push — master ( 89830f...07e3a8 )
by William
20:06
created

IncidentsHelper::getStackLevelInfo()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 29
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 4

Importance

Changes 0
Metric Value
cc 4
eloc 20
nc 6
nop 2
dl 0
loc 29
ccs 4
cts 4
cp 1
crap 4
rs 9.6
c 0
b 0
f 0
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 12
/**
31
 * Incidents View helper.
32 12
 */
33 12
class IncidentsHelper extends AppHelper
34
{
35 4
    /**
36
     * @param mixed $incidents
37 4
     * @return string comma separated list
38 4
     */
39 4
    public function createIncidentsLinks($incidents): string
40
    {
41 4
        $links = [];
42
        foreach ($incidents as $incident) {
43 4
            $links[] = $this->linkToIncident($incident);
44
        }
45
46 4
        return implode(', ', $links);
47
    }
48 4
49 4
    /**
50
     * @param mixed $incident The incident
51 4
     * @return string HTML <a> code
52
     */
53
    public function linkToIncident($incident): string
54 4
    {
55
        $incidentId = $incident['id'];
56 4
57 4
        return '<a href="/' . BASE_DIR . 'incidents/view/' . $incidentId . '">#' . $incidentId . '</a>';
58 4
    }
59 4
60 4
    /**
61 4
     * @param mixed $incidents The incidents
62 4
     * @return string HTML code
63 4
     */
64
    public function incidentsDescriptions($incidents): string
65
    {
66 4
        $descriptions = '';
67
        foreach ($incidents as $incident) {
68
            $descriptions .= '<span>Incident ';
69 8
            $descriptions .= $this->linkToIncident($incident);
70
            $descriptions .= ':</span>';
71 8
            $descriptions .= '<pre>';
72 8
            $descriptions .= htmlspecialchars($incident['steps']);
73
            $descriptions .= '</pre>';
74 8
        }
75 4
76 4
        return $descriptions;
77
    }
78
79 8
    /**
80 8
     * @param mixed  $incident The incident
81 8
     * @param stirng $divClass A class for the div
0 ignored issues
show
Bug introduced by
The type App\View\Helper\stirng was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
82
     * @return string HTML code
83 8
     */
84 8
    public function getStacktrace($incident, string $divClass): string
85 8
    {
86
        $html = '';
87
        $html .= '<div class="' . $divClass . '">';
88
89
        if (is_string($incident['stacktrace'])) {
90
            $incident['stacktrace'] =
91
                    json_decode($incident['stacktrace'], true);
92
        }
93 8
94
        foreach ($incident['stacktrace'] as $level) {
95 8
            $exception_type = ($incident['exception_type'] ? 'php' : 'js');
96 8
            $html .= $this->getStackLevelInfo($level, $exception_type);
97 8
            $html .= "<pre class='brush: "
98
                . $exception_type
99
                . '; tab-size: 2';
100
            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
            $html .= "'>";
109
110
            if ($exception_type === 'js') {
111
                if (isset($level['context'])) {
112
                    $html .= htmlspecialchars(implode("\n", $level['context']));
113
                }
114
            } else {
115
                $html .= htmlspecialchars($level['function']);
116
                $html .= '(';
117 8
                $argList = '';
118
                if (count($level['args']) > 0) {
119 8
                    foreach ($level['args'] as $arg) {
120
                        $argList .= "\n"
121 8
                            . gettype($arg)
122
                            . ' => '
123
                            . $arg;
124 8
                        $argList .= ',';
125
                    }
126 8
                    $argList = substr($argList, 0, (strlen($argList) - 1));
127 8
                    $argList .= "\n";
128
                }
129 8
                $html .= htmlspecialchars($argList);
130
                $html .= ')';
131
            }
132
            $html .= '</pre>';
133
        }
134
        $html .= '</div>';
135
136
        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 8
     */
144 8
    protected function getStackLevelInfo($level, string $exception_type = 'js'): string
145 2
    {
146
        $html = '<span>';
147
        if ($exception_type === 'js') {
148 8
            $elements = [
149
                'filename',
150 8
                'scriptname',
151
                'line',
152
                'func',
153
                'column',
154
            ];
155
        } else {
156
            $elements = [
157
                'file',
158
                'line',
159
                'function',
160
                'class',
161
            ];
162
        }
163
        foreach ($elements as $element) {
164
            if (! isset($level[$element])) {
165
                continue;
166
            }
167
168
            $html .= $element . ': ' . $level[$element] . '; ';
169
        }
170
        $html .= '</span>';
171
172
        return $html;
173
    }
174
}
175