Completed
Pull Request — master (#9)
by Davidson
03:09
created

UrlUtils::resolveRelativeUrl()   C

Complexity

Conditions 12
Paths 35

Size

Total Lines 64
Code Lines 28

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 28
CRAP Score 12.0427

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 64
ccs 28
cts 30
cp 0.9333
rs 6.0561
c 1
b 0
f 0
cc 12
eloc 28
nc 35
nop 2
crap 12.0427

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
        /* fix url file for Windows */
30 9
        $base = preg_replace('#^file:\/\/([^/])#', 'file:///\1', $base);
31
32
        /*
33
         * parse base URL and convert to local variables:
34
         * $scheme, $host, $path
35
         */
36 9
        $parts = parse_url($base);
37
38
        /* remove non-directory element from path */
39 9
        $path = isset($parts['path']) ? preg_replace('#/[^/]*$#', '', $parts["path"]) : '';
40
41
        /* destroy path if relative url points to root */
42 9
        if ($rel[0] === '/') {
43 4
            $path = '';
44 4
        }
45
46
        /* Build absolute URL */
47 9
        $abs = '';
48
49 9
        if (isset($parts["host"])) {
50 5
            $abs .= $parts['host'];
51 5
        }
52
53 9
        if (isset($parts["port"])) {
54
            $abs .= ":".$parts["port"];
55
        }
56
57 9
        $abs .= $path."/".$rel;
58
59
        /* 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...
60 9
        $n = 1;
61
        do {
62 9
            $abs = preg_replace($re, '/', $abs, -1, $n);
63 9
        } while ($n > 0);
64
65 9
        if (isset($parts["scheme"])) {
66 6
            $abs = $parts["scheme"].'://'.$abs;
67 6
        }
68
69 9
        return $abs;
70
    }
71
72
}
73