Url::addOrUpdateArgs()   A
last analyzed

Complexity

Conditions 6
Paths 32

Size

Total Lines 27
Code Lines 17

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 16
CRAP Score 6.0493

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 6
eloc 17
c 2
b 0
f 0
nc 32
nop 3
dl 0
loc 27
ccs 16
cts 18
cp 0.8889
crap 6.0493
rs 9.0777
1
<?php
2
3
namespace ThinKit\Helpers;
4
5
class Url
6
{
7
    /**
8
     * Update url arguments.
9
     *
10
     * @param  string  $url
11
     * @param  string  $key
12
     * @param  mixed|null  $value
13
     * @return string
14
     */
15 2
    public static function addOrUpdateArgs(string $url, string $key, mixed $value = null): string
16
    {
17 2
        $a     = parse_url($url);
18 2
        $query = $a['query'] ?? '';
19
20 2
        parse_str($query, $params);
21 2
        $params[$key] = $value;
22 2
        $query        = http_build_query($params);
23
24 2
        $result = '';
25 2
        if (!empty($a['scheme'])) {
26 2
            $result .= $a['scheme'].':';
27
        }
28 2
        if (!empty($a['host'])) {
29 2
            $result .= '//'.$a['host'];
30
        }
31 2
        if (!empty($a['port'])) {
32
            $result .= ':'.$a['port'];
33
        }
34 2
        if (!empty($a['path'])) {
35
            $result .= $a['path'];
36
        }
37 2
        if ($query) {
38 2
            $result .= '?'.$query;
39
        }
40
41 2
        return $result;
42
    }
43
}
44