Passed
Push — master ( b58a23...eebb3c )
by Murilo
01:31
created

getLogLevelName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 10
c 1
b 0
f 0
dl 0
loc 14
rs 9.9332
cc 1
nc 1
nop 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * Simple shutdown function
7
 */
8
function shutdown()
9
{
10
    global $argv;
11
    global $startMicrotime;
12
13
    $endMicrotime = round((microtime(true) - $startMicrotime), 2);
14
15
    $usedMemory    = round(memory_get_usage(false) / 1024);
16
    $allocedMemory = round(memory_get_usage(true)  / 1024);
17
18
    \Source\Infra\Logger\Log::debug("[*] Used memory: {$usedMemory}KB, Alocated memory: {$allocedMemory}KB");
19
    \Source\Infra\Logger\Log::debug("[*] Execution time: {$endMicrotime}, Exiting...");
20
21
    if (isset($argv) && in_array('--debug', $argv)) {
22
        @closelog();
0 ignored issues
show
Security Best Practice introduced by
It seems like you do not handle an error condition for closelog(). This can introduce security issues, and is generally not recommended. ( Ignorable by Annotation )

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

22
        /** @scrutinizer ignore-unhandled */ @closelog();

If you suppress an error, we recommend checking for the error condition explicitly:

// For example instead of
@mkdir($dir);

// Better use
if (@mkdir($dir) === false) {
    throw new \RuntimeException('The directory '.$dir.' could not be created.');
}
Loading history...
23
    }
24
25
    exit(0);
0 ignored issues
show
Best Practice introduced by
Using exit here is not recommended.

In general, usage of exit should be done with care and only when running in a scripting context like a CLI script.

Loading history...
26
}
27
28
/**
29
 * @param \Throwable $exception
30
 * @return void
31
 */
32
function exceptionHandler($exception)
33
{
34
    \Source\Infra\Logger\Log::exception($exception);
35
}
36
37
/**
38
 * @param string $email
39
 * @return bool
40
 */
41
function is_email(string $email): bool
42
{
43
    return filter_var($email, FILTER_VALIDATE_EMAIL);
44
}
45
46
/**
47
 * ##################
48
 * ###   STRING   ###
49
 * ##################
50
 */
51
52
/**
53
 * @param string $string
54
 * @return string
55
 */
56
function str_slug(string $string): string
57
{
58
    $string = filter_var(mb_strtolower($string), FILTER_SANITIZE_STRIPPED);
59
    $formats = 'ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜüÝÞßàáâãäåæçèéêëìíîïðñòóôõöøùúûýýþÿRr"!@#$%&*()_-+={[}]/?;:.,\\\'<>°ºª';
60
    $replace = 'aaaaaaaceeeeiiiidnoooooouuuuuybsaaaaaaaceeeeiiiidnoooooouuuyybyRr                                 ';
61
62
    $slug = str_replace(['-----', '----', '---', '--'], '-',
63
        str_replace(' ', '-',
64
            trim(strtr(utf8_decode($string), utf8_decode($formats), $replace))
65
        )
66
    );
67
68
    return $slug;
69
}
70
71
/**
72
 * @param string $string
73
 * @return string
74
 */
75
function str_studly_case(string $string): string
76
{
77
    $string = str_slug($string);
78
    $studlyCase = str_replace(' ', '',
79
        mb_convert_case(str_replace('-', ' ', $string), MB_CASE_TITLE)
80
    );
81
82
    return $studlyCase;
83
}
84
85
/**
86
 * @param string $string
87
 * @return string
88
 */
89
function str_camel_case(string $string): string
90
{
91
    return lcfirst(str_studly_case($string));
92
}
93
94
/**
95
 * @param string $string
96
 * @return string
97
 */
98
function str_title(string $string): string
99
{
100
    return mb_convert_case(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS), MB_CASE_TITLE);
101
}
102
103
/**
104
 * @param string $string
105
 * @param int $limit
106
 * @param string $pointer
107
 * @return string
108
 */
109
function str_limit_words(string $string, int $limit, string $pointer = '...'): string
110
{
111
    $string = trim(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS));
112
    $arrWords = explode(' ', $string);
113
    $numWords = count($arrWords);
114
115
    if ($numWords < $limit) {
116
        return $string;
117
    }
118
119
    $words = implode(' ', array_slice($arrWords, 0, $limit));
120
121
    return "{$words}{$pointer}";
122
}
123
124
/**
125
 * @param string $string
126
 * @param int $limit
127
 * @param string $pointer
128
 * @return string
129
 */
130
function str_limit_chars(string $string, int $limit, string $pointer = '...'): string
131
{
132
    $string = trim(filter_var($string, FILTER_SANITIZE_SPECIAL_CHARS));
133
    if (mb_strlen($string) <= $limit) {
134
        return $string;
135
    }
136
137
    $chars = mb_substr($string, 0, mb_strrpos(mb_substr($string, 0, $limit), ' '));
138
139
    return "{$chars}{$pointer}";
140
}
141
142
/**
143
 * ################
144
 * ###   DATE   ###
145
 * ################
146
 */
147
148
/**
149
 * @param string $date
150
 * @param string $format
151
 * @return string
152
 */
153
function date_fmt(string $date = 'now', string $format = 'd/m/Y H\hi'): string
154
{
155
    return (new DateTime($date))->format($format);
156
}
157
158
/**
159
 * @param string $date
160
 * @return string
161
 */
162
function date_fmt_br(string $date = 'now'): string
163
{
164
    return (new DateTime($date))->format(CONF_DATE_BR);
165
}
166
167
/**
168
 * @param string $date
169
 * @return string
170
 */
171
function date_fmt_app(string $date = 'now'): string
172
{
173
    return (new DateTime($date))->format(CONF_DATE_APP);
174
}
175
176
/**
177
 * ##############
178
 * ###  LOGS  ###
179
 * ##############
180
 */
181
182
/**
183
 * Get log level name by number
184
 *
185
 * @param int $logLevel
186
 * @return string
187
 */
188
function getLogLevelName(int $logLevel): string
189
{
190
    $logLevelHashTable = [
191
        LOG_EMERG => 'LOG_EMERG',
192
        LOG_ALERT => 'LOG_ALERT',
193
        LOG_CRIT => 'LOG_CRIT',
194
        LOG_ERR => 'LOG_ERR',
195
        LOG_WARNING => 'LOG_WARNING',
196
        LOG_NOTICE => 'LOG_NOTICE',
197
        LOG_INFO => 'LOG_INFO',
198
        LOG_DEBUG => 'LOG_DEBUG'
199
    ];
200
201
    return $logLevelHashTable[$logLevel] ?? 'UNRECOGNIZED';
202
}
203
204