Passed
Branch extract-store (f24e42)
by Konrad
04:37
created

splitURI()   B

Complexity

Conditions 7
Paths 7

Size

Total Lines 35
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 13.125

Importance

Changes 0
Metric Value
cc 7
eloc 15
nc 7
nop 1
dl 0
loc 35
ccs 6
cts 12
cp 0.5
crap 13.125
rs 8.8333
c 0
b 0
f 0
1
<?php
2
3
namespace sweetrdf\InMemoryStoreSqlite;
4
5
function calcURI(string $path, ?string $base = null): string
6
{
7
    /* quick check */
8 104
    if (preg_match("/^[a-z0-9\_]+\:/i", $path)) {/* abs path or bnode */
9 104
        return $path;
10
    }
11 27
    if (preg_match('/^\$\{.*\}/', $path)) {/* placeholder, assume abs URI */
12
        return $path;
13
    }
14 27
    if (preg_match("/^\/\//", $path)) {/* net path, assume http */
15
        return 'http:'.$path;
16
    }
17
    /* other URIs */
18 27
    $base = $base ?: NamespaceHelper::BASE_NAMESPACE;
19 27
    $base = preg_replace('/\#.*$/', '', $base);
20 27
    if (true === $path) {/* empty (but valid) URIref via turtle parser: <> */
0 ignored issues
show
introduced by
The condition true === $path is always false.
Loading history...
21
        return $base;
22
    }
23 27
    $path = preg_replace("/^\.\//", '', $path);
24 27
    $root = preg_match('/(^[a-z0-9]+\:[\/]{1,3}[^\/]+)[\/|$]/i', $base, $m) ? $m[1] : $base; /* w/o trailing slash */
25 27
    $base .= ($base == $root) ? '/' : '';
26 27
    if (preg_match('/^\//', $path)) {/* leading slash */
27 23
        return $root.$path;
28
    }
29 27
    if (!$path) {
30
        return $base;
31
    }
32 27
    if (preg_match('/^([\#\?])/', $path, $m)) {
33 1
        return preg_replace('/\\'.$m[1].'.*$/', '', $base).$path;
34
    }
35 26
    if (preg_match('/^(\&)(.*)$/', $path, $m)) {/* not perfect yet */
36
        return preg_match('/\?/', $base) ? $base.$m[1].$m[2] : $base.'?'.$m[2];
37
    }
38 26
    if (preg_match("/^[a-z0-9]+\:/i", $path)) {/* abs path */
39
        return $path;
40
    }
41
    /* rel path: remove stuff after last slash */
42 26
    $base = substr($base, 0, strrpos($base, '/') + 1);
43
44
    /* resolve ../ */
45 26
    while (preg_match('/^(\.\.\/)(.*)$/', $path, $m)) {
46
        $path = $m[2];
47
        $base = ($base == $root.'/') ? $base : preg_replace('/^(.*\/)[^\/]+\/$/', '\\1', $base);
48
    }
49
50 26
    return $base.$path;
51
}
52
53
function calcBase(string $path): string
54
{
55 24
    $r = $path;
56 24
    $r = preg_replace('/\#.*$/', '', $r); /* remove hash */
57 24
    $r = preg_replace('/^\/\//', 'http://', $r); /* net path (//), assume http */
58 24
    if (preg_match('/^[a-z0-9]+\:/', $r)) {/* scheme, abs path */
59 24
        while (preg_match('/^(.+\/)(\.\.\/.*)$/U', $r, $m)) {
60
            $r = calcURI($m[1], $m[2]);
61
        }
62
63 24
        return $r;
64
    }
65
66 23
    return 'file://'.realpath($r); /* real path */
67
}
68
69
/**
70
 * @return array<string,string>
71
 */
72
function splitURI($v): array
73
{
74
    /*
75
     * the following namespaces may lead to conflated URIs,
76
     * we have to set the split position manually
77
     */
78 23
    if (strpos($v, 'www.w3.org')) {
79
        /*
80
         * @todo port to NamespaceHelper
81
         */
82 23
        $specials = [
83
            'http://www.w3.org/XML/1998/namespace',
84
            'http://www.w3.org/2005/Atom',
85
            'http://www.w3.org/1999/xhtml',
86
        ];
87 23
        foreach ($specials as $ns) {
88 23
            if (str_contains($v, $ns)) {
89
                $local_part = substr($v, \strlen($ns));
90
                if (!preg_match('/^[\/\#]/', $local_part)) {
91
                    return [$ns, $local_part];
92
                }
93
            }
94
        }
95
    }
96
    /* auto-splitting on / or # */
97
    //$re = '^(.*?)([A-Z_a-z][-A-Z_a-z0-9.]*)$';
98 23
    if (preg_match('/^(.*[\/\#])([^\/\#]+)$/', $v, $m)) {
99 23
        return [$m[1], $m[2]];
100
    }
101
    /* auto-splitting on last special char, e.g. urn:foo:bar */
102
    if (preg_match('/^(.*[\:\/])([^\:\/]+)$/', $v, $m)) {
103
        return [$m[1], $m[2]];
104
    }
105
106
    return [$v, ''];
107
}
108