Passed
Push — master ( 6765d2...f88508 )
by Murilo
01:30
created

handleStatusCode()   F

Complexity

Conditions 38
Paths 38

Size

Total Lines 117
Code Lines 115

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 115
c 0
b 0
f 0
dl 0
loc 117
rs 3.3333
cc 38
nc 38
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
/**
4
 * ####################
5
 * ###   VALIDATE   ###
6
 * ####################
7
 */
8
9
/**
10
 * @param string $email
11
 * @return bool
12
 */
13
function is_email(string $email): bool
14
{
15
    return filter_var($email, FILTER_VALIDATE_EMAIL);
16
}
17
18
/**
19
 * @param string $password
20
 * @return bool
21
 */
22
function is_passwd(string $password): bool
23
{
24
    if (password_get_info($password)['algo'] || (mb_strlen($password) >= CONF_PASSWD_MIN_LEN && mb_strlen($password) <= CONF_PASSWD_MAX_LEN)) {
25
        return true;
26
    }
27
28
    return false;
29
}
30
31
/**
32
 * ##################
33
 * ###   STRING   ###
34
 * ##################
35
 */
36
37
/**
38
 * @param string $string
39
 * @return string
40
 */
41
function str_slug(string $string): string
42
{
43
    $string = filter_var(mb_strtolower($string), FILTER_SANITIZE_STRIPPED);
44
    $formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª';
45
    $replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr                                 ';
46
47
    $slug = str_replace(
48
        ["-----", "----", "---", "--"],
49
        "-",
50
        str_replace(
51
            " ",
52
            "-",
53
            trim(strtr(utf8_decode($string), utf8_decode($formats), $replace))
54
        )
55
    );
56
57
    return $slug;
58
}
59
60
/**
61
 * @param string $string
62
 * @return string
63
 */
64
function str_studly_case(string $string): string
65
{
66
    $string = str_slug($string);
67
    $studlyCase = str_replace(
68
        " ",
69
        "",
70
        mb_convert_case(str_replace("-", " ", $string), MB_CASE_TITLE)
71
    );
72
73
    return $studlyCase;
74
}
75
76
/**
77
 * @param string $string
78
 * @return string
79
 */
80
function str_camel_case(string $string): string
81
{
82
    return lcfirst(str_studly_case($string));
83
}
84
85
/**
86
 * @param string $string
87
 * @return string
88
 */
89
function str_title(string $string): string
90
{
91
    return mb_convert_case(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS), MB_CASE_TITLE);
92
}
93
94
/**
95
 * @param string $string
96
 * @param int $limit
97
 * @param string $pointer
98
 * @return string
99
 */
100
function str_limit_words(string $string, int $limit, string $pointer = "..."): string
101
{
102
    $string = trim(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS));
103
    $arrWords = explode(" ", $string);
104
    $numWords = count($arrWords);
105
106
    if ($numWords < $limit) {
107
        return $string;
108
    }
109
110
    $words = implode(" ", array_slice($arrWords, 0, $limit));
111
    return "{$words}{$pointer}";
112
}
113
114
/**
115
 * @param string $string
116
 * @param int $limit
117
 * @param string $pointer
118
 * @return string
119
 */
120
function str_limit_chars(string $string, int $limit, string $pointer = "..."): string
121
{
122
    $string = trim(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS));
123
    if (mb_strlen($string) <= $limit) {
124
        return $string;
125
    }
126
127
    $chars = mb_substr($string, 0, mb_strrpos(mb_substr($string, 0, $limit), " "));
128
    return "{$chars}{$pointer}";
129
}
130
131
/**
132
 * @param string $text
133
 * @return string
134
 */
135
function base64UrlEncode(string $text): string
136
{
137
    return str_replace(
138
        ['+', '/', '='],
139
        ['-', '_', ''],
140
        base64_encode($text)
141
    );
142
}
143
144
/**
145
 * ###############
146
 * ###   URL   ###
147
 * ###############
148
 */
149
150
/**
151
 * @param string $path
152
 * @return string
153
 */
154
function url(string $path = null): string
155
{
156
    if (strpos($_SERVER["HTTP_HOST"], "localhost")) {
157
        if ($path) {
158
            return CONF_URL_TEST . "/" . ($path[0] == "/" ? mb_substr($path, 1) : $path);
159
        }
160
161
        return CONF_URL_TEST;
162
    }
163
164
    if ($path) {
165
        return CONF_URL_BASE . "/" . ($path[0] == "/" ? mb_substr($path, 1) : $path);
166
    }
167
168
    return CONF_URL_BASE;
169
}
170
171
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

171
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...
172
{
173
    return ($_SERVER['HTTP_REFERER'] ?? url());
174
}
175
176
/**
177
 * @param string $url
178
 */
179
function redirect(string $url): void
180
{
181
    header("HTTP/1.1 302 Redirect");
182
    if (filter_var($url, FILTER_VALIDATE_URL)) {
183
        header("Location: {$url}");
184
        exit;
185
    }
186
187
    if (filter_input(INPUT_GET, "route", FILTER_DEFAULT) != $url) {
188
        $location = url($url);
189
        header("Location: {$location}");
190
        exit;
191
    }
192
}
193
194
/**
195
 * ################
196
 * ###   DATE   ###
197
 * ################
198
 */
199
200
/**
201
 * @param string $date
202
 * @param string $format
203
 * @return string
204
 */
205
function date_fmt(string $date = "now", string $format = "d/m/Y H\hi"): string
206
{
207
    return (new DateTime($date))->format($format);
208
}
209
210
/**
211
 * @param string $date
212
 * @return string
213
 */
214
function date_fmt_br(string $date = "now"): string
215
{
216
    return (new DateTime($date))->format(CONF_DATE_BR);
217
}
218
219
/**
220
 * @param string $date
221
 * @return string
222
 */
223
function date_fmt_app(string $date = "now"): string
224
{
225
    return (new DateTime($date))->format(CONF_DATE_APP);
226
}
227
228
/**
229
 * ####################
230
 * ###   PASSWORD   ###
231
 * ####################
232
 */
233
234
/**
235
 * @param string $password
236
 * @return string
237
 */
238
function passwd(string $password): string
239
{
240
    if (!empty(password_get_info($password)['algo'])) {
241
        return $password;
242
    }
243
244
    return password_hash($password, CONF_PASSWD_ALGO, CONF_PASSWD_OPTION);
245
}
246
247
/**
248
 * @param string $password
249
 * @param string $hash
250
 * @return bool
251
 */
252
function passwd_verify(string $password, string $hash): bool
253
{
254
    return password_verify($password, $hash);
255
}
256
257
/**
258
 * @param string $hash
259
 * @return bool
260
 */
261
function passwd_rehash(string $hash): bool
262
{
263
    return password_needs_rehash($hash, CONF_PASSWD_ALGO, CONF_PASSWD_OPTION);
264
}
265
266
/**
267
 * ####################
268
 * ###     HTTP     ###
269
 * ####################
270
 */
271
272
function handleStatusCode(int $statusCode): string
273
{
274
    switch ($statusCode) {
275
        case 100:
276
            return 'Continue';
277
        break;
0 ignored issues
show
Unused Code introduced by
break is not strictly necessary here and could be removed.

The break statement is not necessary if it is preceded for example by a return statement:

switch ($x) {
    case 1:
        return 'foo';
        break; // This break is not necessary and can be left off.
}

If you would like to keep this construct to be consistent with other case statements, you can safely mark this issue as a false-positive.

Loading history...
278
        case 101:
279
            return 'Switching Protocols';
280
        break;
281
        case 200:
282
            return 'OK';
283
        break;
284
        case 201:
285
            return 'Created';
286
        break;
287
        case 202:
288
            return 'Accepted';
289
        break;
290
        case 203:
291
            return 'Non-Authoritative Information';
292
        break;
293
        case 204:
294
            return 'No Content';
295
        break;
296
        case 205:
297
            return 'Reset Content';
298
        break;
299
        case 206:
300
            return 'Partial Content';
301
        break;
302
        case 300:
303
            return 'Multiple Choices';
304
        break;
305
        case 301:
306
            return 'Moved Permanently';
307
        break;
308
        case 302:
309
            return 'Moved Temporarily';
310
        break;
311
        case 303:
312
            return 'See Other';
313
        break;
314
        case 304:
315
            return 'Not Modified';
316
        break;
317
        case 305:
318
            return 'Use Proxy';
319
        break;
320
        case 400:
321
            return 'Bad Request';
322
        break;
323
        case 401:
324
            return 'Unauthorized';
325
        break;
326
        case 402:
327
            return 'Payment Required';
328
        break;
329
        case 403:
330
            return 'Forbidden';
331
        break;
332
        case 404:
333
            return 'Not Found';
334
        break;
335
        case 405:
336
            return 'Method Not Allowed';
337
        break;
338
        case 406:
339
            return 'Not Acceptable';
340
        break;
341
        case 407:
342
            return 'Proxy Authentication Required';
343
        break;
344
        case 408:
345
            return 'Request Time-out';
346
        break;
347
        case 409:
348
            return 'Conflict';
349
        break;
350
        case 410:
351
            return 'Gone';
352
        break;
353
        case 411:
354
            return 'Length Required';
355
        break;
356
        case 412:
357
            return 'Precondition Failed';
358
        break;
359
        case 413:
360
            return 'Request Entity Too Large';
361
        break;
362
        case 414:
363
            return 'Request-URI Too Large';
364
        break;
365
        case 415:
366
            return 'Unsupported Media Type';
367
        break;
368
        case 500:
369
            return 'Internal Server Error';
370
        break;
371
        case 501:
372
            return 'Not Implemented';
373
        break;
374
        case 502:
375
            return 'Bad Gateway';
376
        break;
377
        case 503:
378
            return 'Service Unavailable';
379
        break;
380
        case 504:
381
            return 'Gateway Time-out';
382
        break;
383
        case 505:
384
            return 'HTTP Version not supported';
385
        break;
386
        default:
387
            return 'unknown';
388
        break;
389
    }
390
}
391