Completed
Push — master ( a5882e...93cf61 )
by Asmir
11s
created

UrlUtils   A

Complexity

Total Complexity 12

Size/Duplication

Total Lines 66
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 0

Test Coverage

Coverage 93.55%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 66
ccs 29
cts 31
cp 0.9355
rs 10
c 1
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
C resolveRelativeUrl() 0 63 12
1
<?php
2
3
namespace GoetasWebservices\XML\XSDReader\Utils;
4
5
class UrlUtils
6
{
7 54
    public static function resolveRelativeUrl($base, $rel)
8
    {
9
        $re = array(
10 54
            '#(/\.?/)#',
11 54
            '#/(?!\.\.)[^/]+/\.\./#',
12 54
        );
13
14 54
        if (!$rel) {
15 2
            return $base;
16
        }
17
18
        /* return if already absolute URL */
19 53
        if (parse_url($rel, PHP_URL_SCHEME) !== null || substr($rel, 0, 2) === '//') {
20 45
            return $rel;
21
        }
22
23
        /* queries and anchors */
24 9
        if ($rel[0] === '#' || $rel[0] === '?') {
25 2
            return $base.$rel;
26
        }
27
28
        /* fix url file for Windows */
29 9
        $base = preg_replace('#^file:\/\/([^/])#', 'file:///\1', $base);
30
31
        /*
32
         * parse base URL and convert to local variables:
33
         * $scheme, $host, $path
34
         */
35 9
        $parts = parse_url($base);
36
37
        /* remove non-directory element from path */
38 9
        $path = isset($parts['path']) ? preg_replace('#/[^/]*$#', '', $parts['path']) : '';
39
40
        /* destroy path if relative url points to root */
41 9
        if ($rel[0] === '/') {
42 4
            $path = '';
43 4
        }
44
45
        /* Build absolute URL */
46 9
        $abs = '';
47
48 9
        if (isset($parts['host'])) {
49 5
            $abs .= $parts['host'];
50 5
        }
51
52 9
        if (isset($parts['port'])) {
53
            $abs .= ':'.$parts['port'];
54
        }
55
56 9
        $abs .= $path.'/'.$rel;
57
58
        /* replace '//' or '/./' or '/foo/../' with '/' */
0 ignored issues
show
Unused Code Comprehensibility introduced by
38% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
59 9
        $n = 1;
60
        do {
61 9
            $abs = preg_replace($re, '/', $abs, -1, $n);
62 9
        } while ($n > 0);
63
64 9
        if (isset($parts['scheme'])) {
65 6
            $abs = $parts['scheme'].'://'.$abs;
66 6
        }
67
68 9
        return $abs;
69
    }
70
}
71