Passed
Push — main ( 8b73d6...c69a6f )
by Dimitri
03:18
created

mix()   C

Complexity

Conditions 12
Paths 40

Size

Total Lines 53
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 12
eloc 27
c 1
b 0
f 0
nc 40
nop 2
dl 0
loc 53
rs 6.9666

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
 * This file is part of Blitz PHP framework.
5
 *
6
 * (c) 2022 Dimitri Sitchet Tomkeu <[email protected]>
7
 *
8
 * For the full copyright and license information, please view
9
 * the LICENSE file that was distributed with this source code.
10
 */
11
12
if (! function_exists('css_url')) {
13
    /**
14
     * CSS URL
15
     *
16
     * Renvoie l'url d'un fichier css.
17
     *
18
     * @param string $name nom du fichier dont on veut avoir l'url
19
     */
20
    function css_url(string $name): string
21
    {
22
        $name = explode('?', $name)[0];
23
        $name = str_replace(site_url() . 'css/', '', htmlspecialchars($name));
24
25
        if (is_localfile($name)) {
26
            $name .= (! preg_match('#\.css$#i', $name) ? '.css' : '');
27
            $filename = WEBROOT . 'css' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
28
29
            return site_url() . 'css/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
30
        }
31
32
        return $name . (! preg_match('#\.css$#i', $name) ? '.css' : '');
33
    }
34
}
35
36
if (! function_exists('js_url')) {
37
    /**
38
     * JS URL
39
     *
40
     * Renvoie l'url d'un fichier js.
41
     *
42
     * @param string $name nom du fichier dont on veut avoir l'url
43
     */
44
    function js_url(string $name): string
45
    {
46
        $name = explode('?', $name)[0];
47
        $name = str_replace(site_url() . 'js/', '', htmlspecialchars($name));
48
49
        if (is_localfile($name)) {
50
            $name .= (! preg_match('#\.js$#i', $name) ? '.js' : '');
51
            $filename = WEBROOT . 'js' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
52
53
            return site_url() . 'js/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
54
        }
55
56
        return $name . (! preg_match('#\.js$#i', $name) ? '.js' : '');
57
    }
58
}
59
60
if (! function_exists('lib_css_url')) {
61
    /**
62
     * LIB CSS URL
63
     *
64
     * Renvoie l'url d'un fichier css d'une librairie
65
     *
66
     * @param string $name nom du fichier dont on veut avoir l'url
67
     */
68
    function lib_css_url(string $name): string
69
    {
70
        $name = explode('?', $name)[0];
71
        $name = str_replace(site_url() . 'lib/', '', htmlspecialchars($name));
72
73
        if (is_localfile($name)) {
74
            $name .= (! preg_match('#\.css$#i', $name) ? '.css' : '');
75
            $filename = WEBROOT . 'lib' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
76
77
            return site_url() . 'lib/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
78
        }
79
80
        return $name . (! preg_match('#\.css$#i', $name) ? '.css' : '');
81
    }
82
}
83
84
if (! function_exists('lib_js_url')) {
85
    /**
86
     * LIB JS URL
87
     *
88
     * Renvoie l'url d'un fichier js d'une librairy.
89
     *
90
     * @param string $name nom du fichier dont on veut avoir l'url
91
     */
92
    function lib_js_url(string $name): string
93
    {
94
        $name = explode('?', $name)[0];
95
        $name = str_replace(site_url() . 'lib/', '', htmlspecialchars($name));
96
97
        if (is_localfile($name)) {
98
            $name .= (! preg_match('#\.js$#i', $name) ? '.js' : '');
99
            $filename = WEBROOT . 'lib' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
100
101
            return site_url() . 'lib/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
102
        }
103
104
        return $name . (! preg_match('#\.js$#i', $name) ? '.js' : '');
105
    }
106
}
107
108
if (! function_exists('lib_styles')) {
109
    /**
110
     * LIB_STYLES
111
     *
112
     * inclu une ou plusieurs feuilles de style css
113
     *
114
     * @param string|string[] $name  nom du fichier dont on veut inserer
115
     * @param bool            $print Specifie si on affiche directement la sortie ou si on la retourne
116
     *
117
     * @return string|void
118
     */
119
    function lib_styles($name, bool $print = true)
120
    {
121
        $name   = (array) $name;
122
        $return = [];
123
124
        foreach ($name as $style) {
125
            if (is_string($style)) {
126
                $style = (! preg_match('#\.css$#i', $style) ? $style . '.css' : $style);
127
                if (is_file(WEBROOT . 'lib' . DS . str_replace('/', DS, $style))) {
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
128
                    $return[] = '<link rel="preload" type="text/css" href="' . lib_css_url($style) . '" as="style">
129
						<link rel="stylesheet" type="text/css" href="' . lib_css_url($style) . '" />';
130
                } elseif (is_localfile($style)) {
131
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
132
                    $return[] = '<link rel="stylesheet" type="text/css" href="' . lib_css_url($style) . '" /> -->';
133
                } else {
134
                    $return[] = '<link rel="preload" type="text/css" href="' . lib_css_url($style) . '" as="style">
135
						<link rel="stylesheet" type="text/css" href="' . lib_css_url($style) . '" />';
136
                }
137
            }
138
        }
139
140
        $output = implode("\n", $return);
141
142
        if (false === $print) {
143
            return $output;
144
        }
145
146
        echo $output;
147
    }
148
}
149
150
if (! function_exists('lib_scripts')) {
151
    /**
152
     * LIB_SCRIPTS
153
     *
154
     * inclu un ou plusieurs scripts js
155
     *
156
     * @param string|string[] $name  nom du fichier dont on veut inserer
157
     * @param bool            $print Specifie si on affiche directement la sortie ou si on la retourne
158
     *
159
     * @return string|void
160
     */
161
    function lib_scripts($name, bool $print = true)
162
    {
163
        $name   = (array) $name;
164
        $return = [];
165
166
        foreach ($name as $script) {
167
            if (is_string($script)) {
168
                $script = (! preg_match('#\.js$#i', $script) ? $script . '.js' : $script);
169
                if (is_file(WEBROOT . 'lib' . DS . str_replace('/', DS, $script))) {
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
170
                    $return[] = '<script type="text/javascript" src="' . lib_js_url($script) . '"></script>';
171
                } elseif (is_localfile($script)) {
172
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
173
                    $return[] = '<script type="text/javascript" src="' . lib_js_url($script) . '"></script> -->';
174
                } else {
175
                    $return[] = '<script type="text/javascript" src="' . lib_js_url($script) . '"></script>';
176
                }
177
            }
178
        }
179
180
        $output = implode("\n", $return);
181
182
        if (false === $print) {
183
            return $output;
184
        }
185
186
        echo $output;
187
    }
188
}
189
190
if (! function_exists('styles')) {
191
    /**
192
     * STYLES
193
     *
194
     * inclu une ou plusieurs feuilles de style css
195
     *
196
     * @param string|string[] $name  nom du fichier dont on veut inserer
197
     * @param bool            $print Specifie si on affiche directement la sortie ou si on la retourne
198
     *
199
     * @return string|void
200
     */
201
    function styles($name, bool $print = true)
202
    {
203
        $name   = (array) $name;
204
        $return = [];
205
206
        foreach ($name as $style) {
207
            if (is_string($style)) {
208
                $style = (! preg_match('#\.css$#i', $style) ? $style . '.css' : $style);
209
                if (is_file(WEBROOT . 'css' . DS . str_replace('/', DS, $style))) {
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
210
                    $return[] = '<link rel="preload" type="text/css" href="' . css_url($style) . '" as="style">
211
						<link rel="stylesheet" type="text/css" href="' . css_url($style) . '" />';
212
                } elseif (is_localfile($style)) {
213
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
214
                    $return[] = '<link rel="stylesheet" type="text/css" href="' . css_url($style) . '" /> -->';
215
                } else {
216
                    $return[] = '<link rel="preload" type="text/css" href="' . css_url($style) . '" as="style">
217
						<link rel="stylesheet" type="text/css" href="' . css_url($style) . '" />';
218
                }
219
            }
220
        }
221
222
        $output = implode("\n", $return);
223
224
        if (false === $print) {
225
            return $output;
226
        }
227
228
        echo $output;
229
    }
230
}
231
232
if (! function_exists('scripts')) {
233
    /**
234
     * SCRIPTS
235
     *
236
     * inclu un ou plusieurs scripts js
237
     *
238
     * @param string|string[] $name  nom du fichier dont on veut inserer
239
     * @param bool            $print Specifie si on affiche directement la sortie ou si on la retourne
240
     *
241
     * @return string|void
242
     */
243
    function scripts($name, bool $print = true)
244
    {
245
        $name   = (array) $name;
246
        $return = [];
247
248
        foreach ($name as $script) {
249
            if (is_string($script)) {
250
                $script = (! preg_match('#\.js$#i', $script) ? $script . '.js' : $script);
251
                if (is_file(WEBROOT . 'js' . DS . str_replace('/', DS, $script))) {
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
252
                    $return[] = '<script type="text/javascript" src="' . js_url($script) . '"></script>';
253
                } elseif (is_localfile($script)) {
254
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
255
                    $return[] = '<script type="text/javascript" src="' . js_url($script) . '"></script> -->';
256
                } else {
257
                    $return[] = '<script type="text/javascript" src="' . js_url($script) . '"></script>';
258
                }
259
            }
260
        }
261
262
        $output = implode("\n", $return);
263
264
        if (false === $print) {
265
            return $output;
266
        }
267
268
        echo $output;
269
    }
270
}
271
272
if (! function_exists('less_url')) {
273
    /**
274
     * LESS URL
275
     *
276
     * Renvoie l'url d'un fichier less.
277
     *
278
     * @param string $name nom du fichier dont on veut avoir l'url
279
     */
280
    function less_url(string $name): string
281
    {
282
        $name = explode('?', $name)[0];
283
        $name = str_replace(site_url() . 'less/', '', htmlspecialchars($name));
284
285
        if (is_localfile($name)) {
286
            $name .= (! preg_match('#\.less$#i', $name) ? '.less' : '');
287
            $filename = WEBROOT . 'less' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
288
289
            return site_url() . 'less/' . $name . ((file_exists($filename)) ? '?v=' . filemtime($filename) : '');
290
        }
291
292
        return $name . (! preg_match('#\.less$#i', $name) ? '.less' : '');
293
    }
294
}
295
296
if (! function_exists('less_styles')) {
297
    /**
298
     * LESS_STYLES
299
     *
300
     * inclu une ou plusieurs feuilles de style less
301
     *
302
     * @param string|string[] $name  nom du fichier dont on veut inserer
303
     * @param bool            $print Specifie si on affiche directement la sortie ou si on la retourne
304
     *
305
     * @return string|void
306
     */
307
    function less_styles($name, bool $print = true)
308
    {
309
        $name   = (array) $name;
310
        $return = [];
311
312
        foreach ($name as $style) {
313
            if (is_string($style)) {
314
                $style = (! preg_match('#\.less$#i', $style) ? $style . '.less' : $style);
315
                if (is_file(WEBROOT . 'less' . DS . str_replace('/', DS, $style))) {
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
316
                    $return[] = '<link rel="stylesheet" type="text/less" href="' . less_url($style) . '" />';
317
                } elseif (is_localfile($style)) {
318
                    $return[] = "<!-- The specified file do not exist. we can not load it. \n\t";
319
                    $return[] = '<link rel="stylesheet" type="text/less" href="' . less_url($style) . '" /> -->';
320
                } else {
321
                    $return[] = '<link rel="stylesheet" type="text/less" href="' . less_url($style) . '" />';
322
                }
323
            }
324
        }
325
326
        $output = implode("\n", $return);
327
328
        if (false === $print) {
329
            return $output;
330
        }
331
332
        echo $output;
333
    }
334
}
335
336
if (! function_exists('img_url')) {
337
    /**
338
     * IMG URL
339
     *
340
     * Renvoie l'url d'une image
341
     *
342
     * @param string $name nom du fichier dont on veut avoir l'url
343
     */
344
    function img_url(?string $name, bool $add_version = true): string
345
    {
346
        if (empty($name)) {
347
            return '';
348
        }
349
350
        $name = explode('?', $name)[0];
351
        $name = str_replace(site_url() . 'img/', '', htmlspecialchars($name));
352
353
        if (is_localfile($name)) {
354
            $filename = WEBROOT . 'img' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
355
356
            return site_url() . 'img/' . $name . ((file_exists($filename) && $add_version) ? '?v=' . filemtime($filename) : '');
357
        }
358
359
        return $name;
360
    }
361
}
362
363
if (! function_exists('img')) {
364
    /**
365
     * IMG
366
     *
367
     * Cree une image
368
     *
369
     * @param string $name nom du fichier dont on veut inserer
370
     * @param string $alt  texte alternatif
371
     *
372
     * @return string|void
373
     */
374
    function img(string $name, string $alt = '', array $options = [])
375
    {
376
        $return = '<img src="' . img_url($name) . '" alt="' . $alt . '"';
377
378
        $noprint = isset($options['print']) && $options['print'] === false;
379
        unset($options['print']);
380
381
        foreach ($options as $key => $value) {
382
            $return .= ' ' . $key . '="' . $value . '"';
383
        }
384
        $return .= ' />';
385
386
        if ($noprint === true) {
387
            return $return;
388
        }
389
390
        echo $return;
391
    }
392
}
393
394
if (! function_exists('docs_url')) {
395
    /**
396
     * DOCS URL
397
     *
398
     * Renvoie l'url d'un document
399
     *
400
     * @param string $name nom du fichier dont on veut avoir l'url
401
     */
402
    function docs_url(?string $name, bool $add_version = true): string
403
    {
404
        if (empty($name)) {
405
            return '';
406
        }
407
408
        $name = explode('?', $name)[0];
409
        $name = str_replace(site_url() . 'docs/', '', htmlspecialchars($name));
410
411
        if (is_localfile($name)) {
412
            $filename = WEBROOT . 'docs' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
413
414
            return site_url() . 'docs/' . $name . ((file_exists($filename && $add_version)) ? '?v=' . filemtime($filename) : '');
0 ignored issues
show
Bug introduced by
$filename && $add_version of type boolean is incompatible with the type string expected by parameter $filename of file_exists(). ( Ignorable by Annotation )

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

414
            return site_url() . 'docs/' . $name . ((file_exists(/** @scrutinizer ignore-type */ $filename && $add_version)) ? '?v=' . filemtime($filename) : '');
Loading history...
415
        }
416
417
        return $name;
418
    }
419
}
420
421
if (! function_exists('videos_url')) {
422
    /**
423
     * VIDEOS URL
424
     *
425
     * Renvoie l'url d'une vidéo
426
     *
427
     * @param string $name nom du fichier dont on veut avoir l'url
428
     */
429
    function videos_url(?string $name, bool $add_version = true): string
430
    {
431
        if (empty($name)) {
432
            return '';
433
        }
434
435
        $name = explode('?', $name)[0];
436
        $name = str_replace(site_url() . 'videos/', '', htmlspecialchars($name));
437
438
        if (is_localfile($name)) {
439
            $filename = WEBROOT . 'videos' . DS . $name;
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
440
441
            return site_url() . 'videos/' . $name . ((file_exists($filename) && $add_version) ? '?v=' . filemtime($filename) : '');
442
        }
443
444
        return $name;
445
    }
446
}
447
448
if (! function_exists('mix')) {
449
    /**
450
     * Obtenez le chemin d'accès à un fichier Mix versionné.
451
     *
452
     * @throws \Exception
453
     */
454
    function mix(string $path, string $manifestDirectory = ''): string
455
    {
456
        static $manifests = [];
457
458
        $publicPath = trim(WEBROOT, '/\\');
0 ignored issues
show
Bug introduced by
The constant WEBROOT was not found. Maybe you did not declare it correctly or list all dependencies?
Loading history...
459
460
        if ($path[0] !== '/') {
461
            $path = "/{$path}";
462
        }
463
464
        if ($manifestDirectory && $manifestDirectory[0] !== '/') {
465
            $manifestDirectory = "/{$manifestDirectory}";
466
        }
467
468
        $config = (object) config('mix');
469
470
        if (is_file($publicPath . $manifestDirectory . '/hot')) {
471
            $url = rtrim(file_get_contents($publicPath . $manifestDirectory . '/hot'));
472
473
            if (!empty($customUrl = $customUrl = $config->hot_proxy_url)) {
0 ignored issues
show
Bug introduced by
The property hot_proxy_url does not seem to exist on BlitzPHP\Config\Config.
Loading history...
Unused Code introduced by
The assignment to $customUrl is dead and can be removed.
Loading history...
474
                return $customUrl . $path;
475
            }
476
477
            if (strpos($url, 'http://') === 0 || strpos($url, 'https://') === 0) {
478
                return explode(':', $url, 2)[1] . $path;
479
            }
480
481
            return "//localhost:3300{$path}";
482
        }
483
484
        $manifestPath = $publicPath . $manifestDirectory . '/mix-manifest.json';
485
486
        if (!isset($manifests[$manifestPath])) {
487
            if (!is_file($manifestPath)) {
488
                throw new Exception('Le manifeste Mix n\'existe pas.');
489
            }
490
491
            $manifests[$manifestPath] = json_decode(file_get_contents($manifestPath), true);
492
        }
493
494
        $manifest = $manifests[$manifestPath];
495
496
        if (!isset($manifest[$path])) {
497
            $exception = new Exception("Impossible de localiser le fichier Mix: {$path}.");
498
499
            if (! BLITZ_DEBUG) {
500
                return $path;
501
            } else {
502
                throw $exception;
503
            }
504
        }
505
506
        return $config->url . $manifestDirectory . $manifest[$path];
0 ignored issues
show
Bug introduced by
The property url does not seem to exist on BlitzPHP\Config\Config.
Loading history...
507
    }
508
}
509