niceTime()   B
last analyzed

Complexity

Conditions 10
Paths 10

Size

Total Lines 26
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 14
CRAP Score 10.0296

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 10
eloc 23
c 1
b 0
f 0
nc 10
nop 1
dl 0
loc 26
ccs 14
cts 15
cp 0.9333
crap 10.0296
rs 7.6666

How to fix   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
declare(strict_types=1);
4
5
use Suricate\Suricate;
6
7
// Debug
8
//
9
if (!function_exists('_p')) {
10
    function _p()
11
    {
12 1
        echo '<pre>' . "\n";
13 1
        foreach (func_get_args() as $var) {
14 1
            print_r($var);
15 1
            echo "\n";
16
        }
17 1
        echo '</pre>';
18 1
    }
19
}
20
21
if (!function_exists('_d')) {
22
    function _d()
23
    {
24
        echo '<pre>' . "\n";
25
        foreach (func_get_args() as $var) {
26
            var_dump($var);
27
            echo "\n";
28
        }
29
        echo '</pre>';
30
    }
31
}
32
33
if (!function_exists('e')) {
34
    function e($str)
35
    {
36
        return htmlentities((string) $str, ENT_COMPAT, 'UTF-8');
37
    }
38
}
39
40
// Arrays
41
42
if (!function_exists('head')) {
43
    function head($arr)
44
    {
45 1
        return reset($arr);
46
    }
47
}
48
49
if (!function_exists('last')) {
50
    function last($arr)
51
    {
52 1
        return end($arr);
53
    }
54
}
55
56
if (!function_exists('flatten')) {
57
    function flatten(array $array)
58
    {
59 5
        $return = [];
60
        array_walk_recursive($array, function ($a) use (&$return) {
61 5
            $return[] = $a;
62 5
        });
63 5
        return $return;
64
    }
65
}
66
67
// Inspired from laravel helper
68
if (!function_exists('dataGet')) {
69
    function dataGet($target, $key, $default = null)
70
    {
71 9
        if (is_null($key)) {
72 1
            return $target;
73
        }
74
75 9
        foreach (explode('.', $key) as $segment) {
76 9
            if (is_array($target)) {
77 9
                if (!array_key_exists($segment, $target)) {
78 1
                    return value($default);
79
                }
80
81 9
                $target = $target[$segment];
82
            } elseif ($target instanceof \ArrayAccess) {
83
                if (!isset($target[$segment])) {
84
                    return value($default);
85
                }
86
                $target = $target[$segment];
87
            } elseif (is_object($target)) {
88
                if (!isset($target->$segment)) {
89
                    return value($default);
90
                }
91
                $target = $target->$segment;
92
            } else {
93
                return value($default);
94
            }
95
        }
96 9
        return $target;
97
    }
98
}
99
100
if (!function_exists('value')) {
101
    function value($value)
102
    {
103 2
        return $value instanceof \Closure ? $value() : $value;
104
    }
105
}
106
107
// Classes
108
109
if (!function_exists('classBasename')) {
110
    function classBasename($class)
111
    {
112
        $class = is_object($class) ? get_class($class) : $class;
113
114
        return basename(str_replace('\\', '/', $class));
115
    }
116
}
117
118
if (!function_exists('with')) {
119
    function with($class)
120
    {
121 4
        return $class;
122
    }
123
}
124
125
// Strings
126
127
if (!function_exists('camelCase')) {
128
    function camelCase($str)
129
    {
130 1
        return str_replace(' ', '', ucwords(str_replace('_', ' ', $str)));
131
    }
132
}
133
134
if (!function_exists('snakeCase')) {
135
    function snakeCase(string $str, $delimiter)
136
    {
137
        $replace = '$1' . $delimiter . '$2';
138
139
        return ctype_lower($str)
140
            ? $str
141
            : strtolower((string) preg_replace('/(.)([A-Z])/', $replace, $str));
142
    }
143
}
144
145
if (!function_exists('contains')) {
146
    function contains($haystack, $needles)
147
    {
148 1
        foreach ((array) $needles as $currentNeedle) {
149 1
            if (strpos($haystack, $currentNeedle) !== false) {
150 1
                return true;
151
            }
152
        }
153
154 1
        return false;
155
    }
156
}
157
158
if (!function_exists('startsWith')) {
159
    function startsWith($haystack, $needles)
160
    {
161 1
        foreach ((array) $needles as $currentNeedle) {
162 1
            if (strpos($haystack, $currentNeedle) === 0) {
163 1
                return true;
164
            }
165
        }
166
167 1
        return false;
168
    }
169
}
170
171
if (!function_exists('endsWith')) {
172
    function endsWith($haystack, $needles)
173
    {
174 1
        foreach ((array) $needles as $currentNeedle) {
175
            if (
176
                $currentNeedle ==
177 1
                substr($haystack, strlen($haystack) - strlen($currentNeedle))
178
            ) {
179 1
                return true;
180
            }
181
        }
182
183 1
        return false;
184
    }
185
}
186
187
if (!function_exists('wordLimit')) {
188
    function wordLimit($str, $limit = 100, $end = '...')
189
    {
190 1
        if (strlen($str) < $limit) {
191 1
            return $str;
192
        }
193
194 1
        $substr = substr($str, 0, $limit);
195 1
        $spacePos = strrpos($substr, ' ');
196 1
        if ($spacePos !== false) {
197 1
            return substr($substr, 0, $spacePos) . $end;
198
        } else {
199 1
            return $substr . $end;
200
        }
201
    }
202
}
203
204
if (!function_exists('slug')) {
205
    function slug($str)
206
    {
207 1
        if (class_exists('Transliterator')) {
208 1
            $translit = \Transliterator::create(
209 1
                'Any-Latin; NFD; [:Nonspacing Mark:] Remove; NFC; [:Punctuation:] Remove; Lower();'
210
            );
211 1
            if ($translit !== null) {
212 1
                return preg_replace(
213 1
                    '/\s/',
214 1
                    '-',
215 1
                    (string) $translit->transliterate($str)
216
                );
217
            }
218
            throw new \RuntimeException("Cannot instanciate Transliterator");
219
        }
220
    }
221
}
222
223
if (!function_exists('app')) {
224
    function app()
225
    {
226 1
        return Suricate::App();
227
    }
228
}
229
230
if (!function_exists('app_path')) {
231
    function app_path($str = '')
232
    {
233 2
        return Suricate::App()->getParameter('path.app') .
234 2
            ($str ? '/' . $str : $str);
235
    }
236
}
237
238
if (!function_exists('base_path')) {
239
    function base_path($str = '')
240
    {
241
        return Suricate::App()->getParameter('path.base') .
242
            ($str ? '/' . $str : $str);
243
    }
244
}
245
246
if (!function_exists('public_path')) {
247
    function public_path($str = '')
248
    {
249
        return Suricate::App()->getParameter('path.public') .
250
            ($str ? '/' . $str : $str);
251
    }
252
}
253
254
if (!function_exists('url')) {
255
    function url($str = '/')
256
    {
257
        return Suricate::App()->getParameter('url') . $str;
258
    }
259
}
260
261
if (!function_exists('baseUri')) {
262
    function baseUri(): string
263
    {
264
        $uri = Suricate::App()->getParameter('base_uri');
265
        return $uri !== '/' ? $uri : '';
266
    }
267
}
268
269
270
if (!function_exists('getPostParam')) {
271
    function getPostParam($param, $defaultValue = null)
272
    {
273
        return Suricate::Request()->getPostParam($param, $defaultValue);
274
    }
275
}
276
277
if (!function_exists('getEnvParam')) {
278
    function getEnvParam($param, $defaultValue = null)
279
    {
280
        $env = getenv($param);
281
282
        return $env === false ? $defaultValue : $env;
283
    }
284
}
285
286
if (!function_exists('getParam')) {
287
    function getParam($param, $defaultValue = null)
288
    {
289
        return Suricate::Request()->getParam($param, $defaultValue);
290
    }
291
}
292
293
/**
294
 * i18n
295
 *
296
 * @return void
297
 * @SuppressWarnings(PHPMD.StaticAccess)
298
 */
299
if (!function_exists('i18n')) {
300
    function i18n()
301
    {
302
        return call_user_func_array([Suricate::I18n(), 'get'], func_get_args());
303
    }
304
}
305
306
if (!function_exists('generateUuid')) {
307
    // Via https://rogerstringer.com/2013/11/15/generate-uuids-php/
308
    function generateUuid()
309
    {
310
        return sprintf(
311
            '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
312
            mt_rand(0, 0xffff),
313
            mt_rand(0, 0xffff),
314
            mt_rand(0, 0xffff),
315
            mt_rand(0, 0x0fff) | 0x4000,
316
            mt_rand(0, 0x3fff) | 0x8000,
317
            mt_rand(0, 0xffff),
318
            mt_rand(0, 0xffff),
319
            mt_rand(0, 0xffff)
320
        );
321 1
    }
322 1
}
323 1
324 1
/**
325 1
 TODO : implements i18n
326 1
 **/
327 1
if (!function_exists('niceTime')) {
328 1
    function niceTime($time)
329 1
    {
330 1
        $delta = time() - $time;
331 1
        if ($delta < 60) {
332 1
            return 'il y a moins d\'une minute.';
333 1
        } elseif ($delta < 120) {
334 1
            return 'il y a environ une minute.';
335 1
        } elseif ($delta < 45 * 60) {
336 1
            return 'il y a ' . floor($delta / 60) . ' minutes.';
337 1
        } elseif ($delta < 120 * 60) {
338
            return 'il y a environ une heure.';
339 1
        } elseif ($delta < 24 * 60 * 60) {
340
            return 'il y a environ ' . floor($delta / 3600) . ' heures.';
341 1
        } elseif ($delta < 48 * 60 * 60) {
342 1
            return 'hier.';
343
        } elseif ($delta < 30 * 24 * 3600) {
344 1
            return 'il y a ' . floor($delta / 86400) . ' jours.';
345
        } elseif ($delta < 365 * 24 * 3600) {
346
            return 'il y a ' . floor($delta / (24 * 3600 * 30)) . ' mois.';
347
        } else {
348
            $diff = floor($delta / (24 * 3600 * 365));
349
350
            if ($diff == 1) {
351
                return 'il y a plus d\'un an.';
352
            } else {
353
                return 'il y a plus de ' . $diff . ' ans.';
354
            }
355
        }
356
    }
357
}
358
359
if (!function_exists('dispatchEvent')) {
360
    function dispatchEvent($event, $payload = null)
361
    {
362
        Suricate::EventDispatcher()->fire($event, $payload);
363
    }
364
}
365
366
367
/**
368
 * Return SVG file content for direct embed
369
 * SVG file must be located in `ressources/svg/` folder
370
 *
371
 * @param string $file filename without .svg extension
372
 * @return string
373
 */
374
if (!function_exists('insertSvg')) {
375
376
    function insertSvg($name, $class = 'w-4 h-4'): string
377
    {
378
        $filename = base_path('ressources/svg/' . $name . '.svg');
379
380
        if (is_file($filename) && is_readable($filename)) {
381
            return '<span class="' . $class . '">' . file_get_contents($filename)  . '</span>';
382
        }
383
        return '';
384
    }
385
}
386