Passed
Push — static-analysis ( 44a2ef...01d316 )
by SignpostMarv
03:10
created

UrlUtils   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Test Coverage

Coverage 92.86%

Importance

Changes 4
Bugs 0 Features 0
Metric Value
dl 0
loc 108
ccs 39
cts 42
cp 0.9286
rs 10
c 4
b 0
f 0
wmc 14

3 Methods

Rating   Name   Duplication   Size   Complexity  
B resolveRelativeUrl() 0 17 6
B resolveRelativeUrlToAbsoluteUrl() 0 41 5
B resolveRelativeUrlAfterEarlyChecks() 0 24 3
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
            $base,
50 27
            $rel,
51
            (
52 27
                $rel[0] === '/'
53 17
                    ? ''  // destroy path if relative url points to root
54 4
                    : ( // remove non-directory element from path
55 21
                        isset($parts['path'])
56 21
                            ? preg_replace('#/[^/]*$#', '', $parts["path"])
57 25
                            : ''
58
                    )
59 9
            ),
60 18
            $parts
61 9
        );
62
    }
63
64
    /**
65
    * @param string $base
66
    * @param string $rel
67
    * @param string $path
68
    *
69
    * @return string
70
    */
71 27
    protected static function resolveRelativeUrlToAbsoluteUrl(
72
        $base,
0 ignored issues
show
Unused Code introduced by
The parameter $base is not used and could be removed. ( Ignorable by Annotation )

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

72
        /** @scrutinizer ignore-unused */ $base,

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
73
        $rel,
74
        $path,
75
        array $parts
76
    ) {
77
        $re = array(
78 27
            '#(/\.?/)#',
79
            '#/(?!\.\.)[^/]+/\.\./#'
80 9
        );
81
82
        /* Build absolute URL */
83 27
        $abs = '';
84
85 27
        if (isset($parts["host"])) {
86 15
            $abs .= $parts['host'];
87 5
        }
88
89 27
        if (isset($parts["port"])) {
90
            $abs .= ":".$parts["port"];
91
        }
92
93 27
        $abs .= $path."/".$rel;
94
95
        /*
96
        * replace superfluous slashes with a single slash.
97
        * covers:
98
        * //
99
        * /./
100
        * /foo/../
101
        */
102 27
        $n = 1;
103
        do {
104 27
            $abs = preg_replace($re, '/', $abs, -1, $n);
105 27
        } while ($n > 0);
106
107 27
        if (isset($parts["scheme"])) {
108 18
            $abs = $parts["scheme"].'://'.$abs;
109 6
        }
110
111 27
        return $abs;
112
    }
113
114
}
115