Passed
Push — master ( bb924a...090072 )
by Vasyl
01:56
created

prepare_url_path()   A

Complexity

Conditions 5
Paths 3

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 5
eloc 6
c 1
b 0
f 0
nc 3
nop 1
dl 0
loc 12
rs 9.6111
1
<?php
2
3
if (!function_exists('route_alias')) {
4
    /**
5
     * Get URL-path (alias/system path) for entity.
6
     *
7
     * @param string $systemName
8
     * @param array $parameters
9
     * @param bool $absolute
10
     * @param bool $forceWithLocalePreffix
11
     * @return string
12
     */
13
    function route_alias(string $systemName, $parameters = [], $absolute = true, $forceWithLocalePreffix = false): string
14
    {
15
        return app()->make(\Fomvasss\UrlAliases\UrlAlias::class)->route($systemName, $parameters, $absolute, $forceWithLocalePreffix);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

15
        return /** @scrutinizer ignore-call */ app()->make(\Fomvasss\UrlAliases\UrlAlias::class)->route($systemName, $parameters, $absolute, $forceWithLocalePreffix);
Loading history...
16
    }
17
}
18
19
20
if (!function_exists('url_alias_current')) {
21
    /**
22
     * Get current url alias path or system path.
23
     *
24
     * @param string $str
25
     * @return array
26
     */
27
    function url_alias_current($absolute = true): string
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
28
    {
29
        return app()->make(\Fomvasss\UrlAliases\UrlAlias::class)->current($absolute);
0 ignored issues
show
Bug introduced by
The function app was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

29
        return /** @scrutinizer ignore-call */ app()->make(\Fomvasss\UrlAliases\UrlAlias::class)->current($absolute);
Loading history...
30
    }
31
}
32
33
if (!function_exists('array_wrap')) {
34
    /**
35
     * @param $value
36
     * @return array
37
     */
38
    function array_wrap($value)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
39
    {
40
        if (is_null($value)) {
41
            return [];
42
        }
43
44
        return ! is_array($value) ? [$value] : $value;
45
    }
46
}
47
48
if (!function_exists('is_url')) {
49
    /**
50
     * Check the string is url.
51
     *
52
     * @param string $str
53
     * @return mixed
54
     */
55
    function is_url(string $str)
56
    {
57
        return filter_var($str, FILTER_VALIDATE_URL, FILTER_FLAG_HOST_REQUIRED);
58
    }
59
}
60
61
if (!function_exists('prepare_url_path')) {
62
    /**
63
     * Remove sheme & current domain from url.
64
     *
65
     * @param string $str
66
     * @return mixed
67
     */
68
    function prepare_url_path(string $url = null)
69
    {
70
        $path = parse_url($url)['path'] ?? '/';
71
72
        // TODO: to replace request()->root()
73
        if ($path === '/' || $url === '/' || $url === request()->root()) {
0 ignored issues
show
Bug introduced by
The function request was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

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

73
        if ($path === '/' || $url === '/' || $url === /** @scrutinizer ignore-call */ request()->root()) {
Loading history...
74
            return '/';
75
        } elseif ($path) {
76
            return trim($path, '/');
77
        }
78
79
        return null;
80
    }
81
}
82
83
if (!function_exists('url_path_segments')) {
84
    /**
85
     * @param string $path
86
     * @param null $index
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $index is correct as it would always require null to be passed?
Loading history...
87
     * @return mixed
88
     */
89
    function url_path_segments(string $path, int $index = null)
90
    {
91
        $segments = explode('/', $path);
92
93
        $array = array_values(array_filter($segments, function ($value) {
94
            return $value !== '';
95
        }));
96
97
        if ($index) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $index of type integer|null is loosely compared to true; this is ambiguous if the integer can be 0. You might want to explicitly use !== null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
98
            return $array[$index - 1] ?? '';
99
        }
100
101
        return $array;
102
    }
103
}
104
105