Passed
Push — static-analysis ( 01d316...e19ffc )
by SignpostMarv
03:18
created

UrlUtils::resolveRelativeUrlAfterEarlyChecks()   A

Complexity

Conditions 3
Paths 1

Size

Total Lines 23
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 3.0032

Importance

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