Passed
Push — static-analysis ( b6f893...04e304 )
by SignpostMarv
02:57
created

UrlUtils::replaceSuperfluousSlashes()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 13
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 9
nc 1
nop 1
dl 0
loc 13
ccs 11
cts 11
cp 1
crap 2
rs 9.4285
c 0
b 0
f 0
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Utils;
3
4
class UrlUtils
5
{
6
    /**
7
    * @param string $base
8
    * @param string $rel
9
    *
10
    * @return string
11
    */
12 162
    public static function resolveRelativeUrl($base, $rel)
13
    {
14 162
        if (!$rel) {
15 6
            return $base;
16
        } elseif (
17
        /* return if already absolute URL */
18 159
            parse_url($rel, PHP_URL_SCHEME) !== null ||
19 71
            substr($rel, 0, 2) === '//'
20 53
        ) {
21 135
            return $rel;
22
        } elseif (
23
        /* queries and anchors */
24 27
            in_array(
25 27
                $rel[0],
26
                [
27 27
                    '#',
28
                    '?'
29 9
                ]
30 9
            )
31 9
        ) {
32 6
            return $base.$rel;
33
        }
34
35 27
        return static::resolveRelativeUrlAfterEarlyChecks($base, $rel);
36
    }
37
38
    /**
39
    * @param string $base
40
    * @param string $rel
41
    *
42
    * @return string
43
    */
44 27
    protected static function resolveRelativeUrlAfterEarlyChecks($base, $rel)
45
    {
46
        /* fix url file for Windows */
47 27
        $base = preg_replace('#^file:\/\/([^/])#', 'file:///\1', $base);
48
49
        /*
50
         * parse base URL and convert to local variables:
51
         * $scheme, $host, $path
52
         */
53 27
        $parts = parse_url($base);
54
55 27
        return static::resolveRelativeUrlToAbsoluteUrl(
56 27
            $rel,
57
            (
58 27
                $rel[0] === '/'
59 17
                    ? ''  // destroy path if relative url points to root
60 4
                    : ( // remove non-directory element from path
61 21
                        isset($parts['path'])
62 21
                            ? preg_replace('#/[^/]*$#', '', $parts["path"])
63 25
                            : ''
64
                    )
65 9
            ),
66 18
            $parts
67 9
        );
68
    }
69
70
    /**
71
    * @param string $rel
72
    * @param string $path
73
    *
74
    * @return string
75
    */
76 27
    protected static function resolveRelativeUrlToAbsoluteUrl(
77
        $rel,
78
        $path,
79
        array $parts
80
    ) {
81
        /* Build absolute URL */
82 27
        $abs = '';
83
84 27
        if (isset($parts["host"])) {
85 15
            $abs .= $parts['host'];
86 5
        }
87
88 27
        if (isset($parts["port"])) {
89
            $abs .= ":".$parts["port"];
90
        }
91
92 27
        $abs .= $path."/".$rel;
93 27
        $abs = static::replaceSuperfluousSlashes($abs);
94
95 27
        if (isset($parts["scheme"])) {
96 18
            $abs = $parts["scheme"].'://'.$abs;
97 6
        }
98
99 27
        return $abs;
100
    }
101
102
    /**
103
        * replace superfluous slashes with a single slash.
104
        * covers:
105
        * //
106
        * /./
107
        * /foo/../
108
        *
109
        * @param string $abs
110
        *
111
        * @return string
112
        */
113 27
    protected static function replaceSuperfluousSlashes($abs) {
114 27
        $n = 1;
115
        do {
116 27
            $abs = preg_replace(
117 27
                '#(?:(?:/\.?/)|(?!\.\.)[^/]+/\.\./)#',
118 27
                '/',
119 27
                $abs,
120 27
                -1,
121 18
                $n
122 9
            );
123 27
        } while ($n > 0);
124
125 27
        return $abs;
126
    }
127
}
128