Passed
Push — master ( 67a964...2aff1a )
by Konrad
04:08
created

getNormalizedValue()   B

Complexity

Conditions 10
Paths 8

Size

Total Lines 39
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 19
CRAP Score 10.0125

Importance

Changes 0
Metric Value
cc 10
eloc 21
nc 8
nop 1
dl 0
loc 39
rs 7.6666
c 0
b 0
f 0
ccs 19
cts 20
cp 0.95
crap 10.0125

How to fix   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 sweetrdf\InMemoryStoreSqlite;
4
5
function calcURI(string $path, ?string $base = null): string
6
{
7
    /* quick check */
8 87
    if (preg_match("/^[a-z0-9\_]+\:/i", $path)) {/* abs path or bnode */
9 87
        return $path;
10
    }
11 11
    if (preg_match('/^\$\{.*\}/', $path)) {/* placeholder, assume abs URI */
12
        return $path;
13
    }
14 11
    if (preg_match("/^\/\//", $path)) {/* net path, assume http */
15
        return 'http:'.$path;
16
    }
17
    /* other URIs */
18 11
    $base = $base ?: NamespaceHelper::BASE_NAMESPACE;
19 11
    $base = preg_replace('/\#.*$/', '', $base);
20 11
    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 11
    $path = preg_replace("/^\.\//", '', $path);
24 11
    $root = preg_match('/(^[a-z0-9]+\:[\/]{1,3}[^\/]+)[\/|$]/i', $base, $m) ? $m[1] : $base; /* w/o trailing slash */
25 11
    $base .= ($base == $root) ? '/' : '';
26 11
    if (preg_match('/^\//', $path)) {/* leading slash */
27 8
        return $root.$path;
28
    }
29 11
    if (!$path) {
30
        return $base;
31
    }
32 11
    if (preg_match('/^([\#\?])/', $path, $m)) {
33 1
        return preg_replace('/\\'.$m[1].'.*$/', '', $base).$path;
34
    }
35 10
    if (preg_match('/^(\&)(.*)$/', $path, $m)) {/* not perfect yet */
36
        return preg_match('/\?/', $base) ? $base.$m[1].$m[2] : $base.'?'.$m[2];
37
    }
38 10
    if (preg_match("/^[a-z0-9]+\:/i", $path)) {/* abs path */
39
        return $path;
40
    }
41
    /* rel path: remove stuff after last slash */
42 10
    $base = substr($base, 0, strrpos($base, '/') + 1);
43
44
    /* resolve ../ */
45 10
    while (preg_match('/^(\.\.\/)(.*)$/', $path, $m)) {
46
        $path = $m[2];
47
        $base = ($base == $root.'/') ? $base : preg_replace('/^(.*\/)[^\/]+\/$/', '\\1', $base);
48
    }
49
50 10
    return $base.$path;
51
}
52
53
function calcBase(string $path): string
54
{
55 9
    $r = $path;
56 9
    $r = preg_replace('/\#.*$/', '', $r); /* remove hash */
57 9
    $r = preg_replace('/^\/\//', 'http://', $r); /* net path (//), assume http */
58 9
    if (preg_match('/^[a-z0-9]+\:/', $r)) {/* scheme, abs path */
59 9
        while (preg_match('/^(.+\/)(\.\.\/.*)$/U', $r, $m)) {
60
            $r = calcURI($m[1], $m[2]);
61
        }
62
63 9
        return $r;
64
    }
65
66 8
    return 'file://'.realpath($r); /* real path */
67
}
68
69
/**
70
 * Normalize value for ORDER BY operations.
71
 */
72
function getNormalizedValue(string $val): string
73
{
74
    /* try date (e.g. 21 August 2007) */
75
    if (
76 86
        preg_match('/^[0-9]{1,2}\s+[a-z]+\s+[0-9]{4}/i', $val)
77 86
        && ($uts = strtotime($val))
78 86
        && (-1 !== $uts)
79
    ) {
80 1
        return (string) date("Y-m-d\TH:i:s", $uts);
81
    }
82
83
    /* xsd date (e.g. 2009-05-28T18:03:38+09:00 2009-05-28T18:03:38GMT) */
84 86
    if (true === (bool) strtotime($val)) {
85 5
        return (string) date('Y-m-d\TH:i:s\Z', strtotime($val));
86
    }
87
88 84
    if (is_numeric($val)) {
89 24
        $val = sprintf('%f', $val);
90 24
        if (preg_match("/([\-\+])([0-9]*)\.([0-9]*)/", $val, $m)) {
91 1
            return $m[1].sprintf('%018s', $m[2]).'.'.sprintf('%-015s', $m[3]);
92
        }
93 24
        if (preg_match("/([0-9]*)\.([0-9]*)/", $val, $m)) {
94 24
            return '+'.sprintf('%018s', $m[1]).'.'.sprintf('%-015s', $m[2]);
95
        }
96
97
        return $val;
98
    }
99
100
    /* any other string: remove tags, linebreaks etc., but keep MB-chars */
101
    // [\PL\s]+ ( = non-Letters) kills digits
102 66
    $re = '/[\PL\s]+/isu';
0 ignored issues
show
Unused Code introduced by
The assignment to $re is dead and can be removed.
Loading history...
103 66
    $re = '/[\s\'\"\´\`]+/is';
104 66
    $val = trim(preg_replace($re, '-', strip_tags($val)));
105 66
    if (\strlen($val) > 35) {
106 12
        $fnc = \function_exists('mb_substr') ? 'mb_substr' : 'substr';
107 12
        $val = $fnc($val, 0, 17).'-'.$fnc($val, -17);
108
    }
109
110 66
    return $val;
111
}
112
113
/**
114
 * @return array<string,string>
115
 */
116
function splitURI($v): array
117
{
118
    /*
119
     * the following namespaces may lead to conflated URIs,
120
     * we have to set the split position manually
121
     */
122 8
    if (strpos($v, 'www.w3.org')) {
123
        /*
124
         * @todo port to NamespaceHelper
125
         */
126 8
        $specials = [
127
            'http://www.w3.org/XML/1998/namespace',
128
            'http://www.w3.org/2005/Atom',
129
            'http://www.w3.org/1999/xhtml',
130
        ];
131 8
        foreach ($specials as $ns) {
132 8
            if (str_contains($v, $ns)) {
133
                $local_part = substr($v, \strlen($ns));
134
                if (!preg_match('/^[\/\#]/', $local_part)) {
135
                    return [$ns, $local_part];
136
                }
137
            }
138
        }
139
    }
140
    /* auto-splitting on / or # */
141
    //$re = '^(.*?)([A-Z_a-z][-A-Z_a-z0-9.]*)$';
142 8
    if (preg_match('/^(.*[\/\#])([^\/\#]+)$/', $v, $m)) {
143 8
        return [$m[1], $m[2]];
144
    }
145
    /* auto-splitting on last special char, e.g. urn:foo:bar */
146
    if (preg_match('/^(.*[\:\/])([^\:\/]+)$/', $v, $m)) {
147
        return [$m[1], $m[2]];
148
    }
149
150
    return [$v, ''];
151
}
152