Completed
Push — master ( da8fcc...d64df6 )
by Asmir
02:56
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.1%
Metric Value
wmc 12
lcom 0
cbo 0
dl 0
loc 66
ccs 27
cts 29
cp 0.931
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
C resolveRelativeUrl() 0 61 12
1
<?php
2
namespace GoetasWebservices\XML\XSDReader\Utils;
3
4
class UrlUtils
5
{
6
7 48
    public static function resolveRelativeUrl($base, $rel)
8
    {
9
        $re = array(
10 48
            '#(/\.?/)#',
11
            '#/(?!\.\.)[^/]+/\.\./#'
12 48
        );
13
14
15 48
        if (!$rel) {
16 1
            return $base;
17
        }
18
19
        /* return if already absolute URL */
20 47
        if (parse_url($rel, PHP_URL_SCHEME) !== null || substr($rel, 0, 2) === '//') {
21 39
            return $rel;
22
        }
23
24
        /* queries and anchors */
25 9
        if ($rel[0] === '#' || $rel[0] === '?') {
26 2
            return $base.$rel;
27
        }
28
29
        /*
30
         * parse base URL and convert to local variables:
31
         * $scheme, $host, $path
32
         */
33 9
        $parts = parse_url($base);
34
35
        /* remove non-directory element from path */
36 9
        $path = isset($parts['path']) ? preg_replace('#/[^/]*$#', '', $parts["path"]) : '';
37
38
        /* destroy path if relative url points to root */
39 9
        if ($rel[0] === '/') {
40 4
            $path = '';
41 4
        }
42
43
        /* Build absolute URL */
44 9
        $abs = '';
45
46 9
        if (isset($parts["host"])) {
47 5
            $abs .= $parts['host'];
48 5
        }
49
50 9
        if (isset($parts["port"])) {
51
            $abs .= ":".$parts["port"];
52
        }
53
54 9
        $abs .= $path."/".$rel;
55
56
        /* 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...
57 9
        $n = 1;
58
        do {
59 9
            $abs = preg_replace($re, '/', $abs, -1, $n);
60 9
        } while ($n > 0);
61
62 9
        if (isset($parts["scheme"])) {
63 6
            $abs = $parts["scheme"].'://'.$abs;
64 6
        }
65
66 9
        return $abs;
67
    }
68
69
}
70