Passed
Pull Request — master (#16)
by
unknown
13:19 queued 10:34
created

Normalizer::normalizePath()   A

Complexity

Conditions 5
Paths 12

Size

Total Lines 21
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 10
CRAP Score 5.0187

Importance

Changes 0
Metric Value
cc 5
eloc 10
nc 12
nop 1
dl 0
loc 21
ccs 10
cts 11
cp 0.9091
crap 5.0187
rs 9.6111
c 0
b 0
f 0
1
<?php
2
3
namespace Aoe\Asdis\System\Uri;
4
5
class Normalizer
6
{
7
    /**
8
     * Makes a path relative to the webroot.
9
     */
10 3
    public function normalizePath(string $path): string
11
    {
12
        // Fix for wildcard protocol URLs, as parse_url (until PHP 5.4.7) requires the protocol to be set
13
        // @see http://www.php.net/manual/en/function.parse-url.php
14 3
        if (substr($path, 0, 2) === '//') {
15 1
            $path = 'http:' . $path;
16
        }
17
18 3
        $pathInfos = parse_url($path);
19
20 3
        if (isset($pathInfos['path'])) {
21 3
            $path = $pathInfos['path'];
22 3
            if (isset($pathInfos['query'])) {
23
                $path .= '?' . $pathInfos['query'];
24
            }
25
        }
26
27 3
        if (str_starts_with($path, '/')) {
28 1
            $path = substr($path, 1);
29
        }
30 3
        return $path;
31
    }
32
}
33