Completed
Push — master ( 55881e...c51bd4 )
by SignpostMarv
03:05
created

UrlUtils::resolveRelativeUrlToAbsoluteUrl()   B

Complexity

Conditions 4
Paths 8

Size

Total Lines 25
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 4.0119

Importance

Changes 0
Metric Value
dl 0
loc 25
ccs 10
cts 11
cp 0.9091
rs 8.5806
c 0
b 0
f 0
cc 4
eloc 15
nc 8
nop 3
crap 4.0119
1
<?php
2
3
declare(strict_types=1);
4
5
namespace GoetasWebservices\XML\XSDReader\Utils;
6
7
class UrlUtils
8
{
9 58
    public static function resolveRelativeUrl(string $base, string $rel): string
10
    {
11 58
        if ('' === trim($rel)) {
12 1
            return $base;
13
        } elseif (
14
        /* return if already absolute URL */
15 57
            parse_url($rel, PHP_URL_SCHEME) !== null ||
16 57
            substr($rel, 0, 2) === '//'
17
        ) {
18 49
            return $rel;
19
        } elseif (
20
        /* queries and anchors */
21 9
            in_array(
22 9
                $rel[0],
23
                [
24 9
                    '#',
25
                    '?',
26
                ],
27 9
                true
28
            )
29
        ) {
30 2
            return $base.$rel;
31
        }
32
33 9
        return static::resolveRelativeUrlAfterEarlyChecks($base, $rel);
34
    }
35
36
    /**
37
     * @param string $base
38
     * @param string $rel
39
     *
40
     * @return string
41
     */
42 9
    protected static function resolveRelativeUrlAfterEarlyChecks(string $base, string $rel): string
43
    {
44
        /* fix url file for Windows */
45 9
        $base = preg_replace('#^file:\/\/([^/])#', 'file:///\1', $base);
46
47
        /**
48
         * @var mixed[]
49
         *
50
         * parse base URL and convert to local variables:
51
         * $scheme, $host, $path
52
         */
53 9
        $parts = parse_url($base);
54
55 9
        return static::resolveRelativeUrlToAbsoluteUrl(
56 9
            $rel,
57
            (
58 9
                $rel[0] === '/'
59 4
                    ? ''  // destroy path if relative url points to root
60
                    : ( // remove non-directory element from path
61 7
                        isset($parts['path'])
62 7
                            ? preg_replace(
63 7
                                '#/[^/]*$#',
64 7
                                '',
65 7
                                (string) $parts['path']
66
                            )
67 9
                            : ''
68
                    )
69
            ),
70 9
            $parts
0 ignored issues
show
Security Bug introduced by
It seems like $parts defined by parse_url($base) on line 53 can also be of type false; however, GoetasWebservices\XML\XS...ativeUrlToAbsoluteUrl() does only seem to accept array, did you maybe forget to handle an error condition?

This check looks for type mismatches where the missing type is false. This is usually indicative of an error condtion.

Consider the follow example

<?php

function getDate($date)
{
    if ($date !== null) {
        return new DateTime($date);
    }

    return false;
}

This function either returns a new DateTime object or false, if there was an error. This is a typical pattern in PHP programming to show that an error has occurred without raising an exception. The calling code should check for this returned false before passing on the value to another function or method that may not be able to handle a false.

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