starRate()   C
last analyzed

Complexity

Conditions 12
Paths 12

Size

Total Lines 104
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 22
c 1
b 0
f 0
dl 0
loc 104
rs 6.9666
cc 12
nc 12
nop 1

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
if (!extension_loaded('curl')) {
4
    throw new Exception('Curl não suportado!', 1);
5
    exit;
6
}
7
8
function site(string $param = null): string
9
{
10
    if ($param && !empty(SITE[$param])) {
11
        return SITE[$param];
12
    }
13
14
    return SITE['root'];
15
}
16
17
/**
18
 * @param string $path
19
 * @return string
20
 */
21
function url(string $path = null): string
22
{
23
    if ($path) {
24
        return CONF_URL_BASE . '/' . ($path[0] == '/' ? mb_substr($path, 1) : $path);
25
    }
26
27
    return CONF_URL_BASE;
28
}
29
30
function url_back(string $path = null): string
0 ignored issues
show
Unused Code introduced by
The parameter $path is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

30
function url_back(/** @scrutinizer ignore-unused */ string $path = null): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
31
{
32
    return ($_SERVER['HTTP_REFERER'] ?? url());
33
}
34
35
function routeImage(string $imageUrl): string
36
{
37
    return "https://via.placeholder.com/1200x628/0984e3/FFFFFF?text={$imageUrl}";
38
}
39
40
function asset(string $path, $time = true): string
0 ignored issues
show
Unused Code introduced by
The parameter $time is not used and could be removed. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-unused  annotation

40
function asset(string $path, /** @scrutinizer ignore-unused */ $time = true): string

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
41
{
42
    return SITE['root'] . '/views/assets' . $path;
43
}
44
45
function flash(string $type = null, string $message = null): ?string
46
{
47
    if ($type && $message) {
48
        $_SESSION['flash'] = [
49
            'type' => $type,
50
            'message' => $message
51
        ];
52
        return null;
53
    }
54
55
    if (!empty($_SESSION['flash']) && $flash = $_SESSION['flash']) {
56
        unset($_SESSION['flash']);
57
        return "<div class=\"message {$flash["type"]}\">{$flash["message"]}</div>";
58
    }
59
60
    return null;
61
}
62
63
function productImage($product, int $index = 0)
64
{
65
    return (!empty($product->ProductImage[$index]->image)) ? $product->ProductImage[$index]->image : asset('/images/no-product-image.png');
66
}
67
68
/**
69
 * @param \Throwable $exception
70
 * @return void
71
 */
72
function exceptionHandler($exception)
73
{
74
    $errorCode = @$exception->getCode();
75
    $errorMessage = @$exception->getMessage();
76
77
    if (!empty($exception->getTrace())) {
78
        $errorLine = @$exception->getTrace()[0]['line'] ?? '';
79
        $errorFile = @$exception->getTrace()[0]['file'] ?? '';
80
    }
81
82
    error_log((empty($errorLine)) ? "[{$errorCode}] {$errorMessage}" : "[{$errorCode}] {$errorMessage} on line {$errorLine} of file {$errorFile}");
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable $errorFile does not seem to be defined for all execution paths leading up to this point.
Loading history...
83
84
    header('Location: /500');
85
    exit;
86
}
87
88
/**
89
 * @param int $errno
90
 * @param string $errstr
91
 * @param string $errfile
92
 * @param int $errline
93
 * @return void
94
 */
95
function errorHandler($errno, $errstr, $errfile, $errline)
96
{
97
    exceptionHandler(new ErrorException($errstr, 0, $errno, $errfile, $errline));
98
}
99
100
/**
101
 * ################
102
 * ###   DATE   ###
103
 * ################
104
 */
105
106
/**
107
 * @param string $date
108
 * @param string $format
109
 * @return string
110
 */
111
function date_fmt(string $date = 'now', string $format = 'd/m/Y H\hi'): string
112
{
113
    return (new DateTime($date))->format($format);
114
}
115
116
/**
117
 * @param string $date
118
 * @return string
119
 */
120
function date_fmt_br(string $date = 'now'): string
121
{
122
    return (new DateTime($date))->format(CONF_DATE_BR);
123
}
124
125
/**
126
 * @param string $date
127
 * @return string
128
 */
129
function date_fmt_app(string $date = 'now'): string
130
{
131
    return (new DateTime($date))->format(CONF_DATE_APP);
132
}
133
134
/**
135
 * @param string $string
136
 * @param int $limit
137
 * @param string $pointer
138
 * @return string
139
 */
140
function str_limit_chars(string $string, int $limit, string $pointer = "..."): string
141
{
142
    $string = trim(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS));
143
    if (mb_strlen($string) <= $limit) {
144
        return $string;
145
    }
146
147
    $chars = mb_substr($string, 0, mb_strrpos(mb_substr($string, 0, $limit), " "));
148
    return "{$chars}{$pointer}";
149
}
150
151
function callAPI(string $url, string $method, $data = null, string $jwt = null)
152
{
153
    $curl = curl_init(BASE_API . $url); // Montar url da API
154
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); // Desligar a verificação do TLS
155
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false); // Desligar a verificação do TLS
156
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true); // Obter retorno em $resultado // remover var_dump automatico
157
158
    $headers = [];
159
160
    if (!empty($data)) {
161
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data); // Definir corpo
162
        $headers[] = 'Content-Type:multipart/form-data';
163
    }
164
165
    if (!empty($jwt)) {
166
        $jwt = "Authorization: Bearer {$jwt}";
167
        $headers[] = $jwt;
168
    }
169
170
    if (!empty($headers)) {
171
        curl_setopt($curl, CURLOPT_HTTPHEADER, $headers); // Setar HEADER como json
172
    }
173
174
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method); // Definir metodo
175
176
    $resultado = curl_exec($curl);
177
    $httpCode = curl_getinfo($curl, CURLINFO_HTTP_CODE);
178
    $curlError = curl_error($curl);
179
180
    curl_close($curl);
181
182
    if ($curlError) {
183
        return ['curl_error' => $curlError];
184
    }
185
186
    return [
187
        'result' => $resultado,
188
        'code' => $httpCode
189
    ];
190
}
191
192
function starRate(float $rate): string
193
{
194
    if ($rate <= 0.4) {
195
        return '
196
            <i class="fa fa-star-o"></i>
197
            <i class="fa fa-star-o"></i>
198
            <i class="fa fa-star-o"></i>
199
            <i class="fa fa-star-o"></i>
200
            <i class="fa fa-star-o"></i>
201
        ';
202
    }
203
204
    if ($rate <= 0.9) {
205
        return '
206
            <i class="fa fa-star-half-o"></i>
207
            <i class="fa fa-star-o"></i>
208
            <i class="fa fa-star-o"></i>
209
            <i class="fa fa-star-o"></i>
210
            <i class="fa fa-star-o"></i>
211
        ';
212
    }
213
214
    if ($rate <= 1.4) {
215
        return '
216
            <i class="fa fa-star"></i>
217
            <i class="fa fa-star-o"></i>
218
            <i class="fa fa-star-o"></i>
219
            <i class="fa fa-star-o"></i>
220
            <i class="fa fa-star-o"></i>
221
        ';
222
    }
223
224
    if ($rate <= 1.9) {
225
        return '
226
            <i class="fa fa-star"></i>
227
            <i class="fa fa-star-half-o"></i>
228
            <i class="fa fa-star-o"></i>
229
            <i class="fa fa-star-o"></i>
230
            <i class="fa fa-star-o"></i>
231
        ';
232
    }
233
234
    if ($rate <= 2.4) {
235
        return '
236
            <i class="fa fa-star"></i>
237
            <i class="fa fa-star"></i>
238
            <i class="fa fa-star-o"></i>
239
            <i class="fa fa-star-o"></i>
240
            <i class="fa fa-star-o"></i>
241
        ';
242
    }
243
244
    if ($rate <= 2.9) {
245
        return '
246
            <i class="fa fa-star"></i>
247
            <i class="fa fa-star"></i>
248
            <i class="fa fa-star-half-o"></i>
249
            <i class="fa fa-star-o"></i>
250
            <i class="fa fa-star-o"></i>
251
        ';
252
    }
253
254
    if ($rate <= 3.4) {
255
        return '
256
            <i class="fa fa-star"></i>
257
            <i class="fa fa-star"></i>
258
            <i class="fa fa-star"></i>
259
            <i class="fa fa-star-o"></i>
260
            <i class="fa fa-star-o"></i>
261
        ';
262
    }
263
264
    if ($rate <= 3.9) {
265
        return '
266
            <i class="fa fa-star"></i>
267
            <i class="fa fa-star"></i>
268
            <i class="fa fa-star"></i>
269
            <i class="fa fa-star-half-o"></i>
270
            <i class="fa fa-star-o"></i>
271
        ';
272
    }
273
274
    if ($rate <= 4.4) {
275
        return '
276
            <i class="fa fa-star"></i>
277
            <i class="fa fa-star"></i>
278
            <i class="fa fa-star"></i>
279
            <i class="fa fa-star"></i>
280
            <i class="fa fa-star-o"></i>
281
        ';
282
    }
283
284
    if ($rate <= 4.9) {
285
        return '
286
            <i class="fa fa-star"></i>
287
            <i class="fa fa-star"></i>
288
            <i class="fa fa-star"></i>
289
            <i class="fa fa-star"></i>
290
            <i class="fa fa-star-half-o"></i>
291
        ';
292
    }
293
294
    if ($rate > 4.9) {
295
        return '
296
            <i class="fa fa-star"></i>
297
            <i class="fa fa-star"></i>
298
            <i class="fa fa-star"></i>
299
            <i class="fa fa-star"></i>
300
            <i class="fa fa-star"></i>
301
        ';
302
    }
303
}
304