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

UrlUtils::resolveRelativeUrl()   C

Complexity

Conditions 12
Paths 35

Size

Total Lines 63
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 29
CRAP Score 12.0386

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 63
ccs 29
cts 31
cp 0.9355
rs 6.1308
c 1
b 0
f 0
cc 12
eloc 28
nc 35
nop 2
crap 12.0386

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
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