Completed
Push — master ( da8fcc...d64df6 )
by Asmir
02:56
created

UrlUtils::resolveRelativeUrl()   C

Complexity

Conditions 12
Paths 35

Size

Total Lines 61
Code Lines 27

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 27
CRAP Score 12.0473
Metric Value
dl 0
loc 61
ccs 27
cts 29
cp 0.931
rs 6.2855
cc 12
eloc 27
nc 35
nop 2
crap 12.0473

How to fix   Long Method    Complexity   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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