UrlHelper::buildUrl()   F
last analyzed

Complexity

Conditions 12
Paths 2048

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 12

Importance

Changes 0
Metric Value
eloc 11
c 0
b 0
f 0
dl 0
loc 13
ccs 12
cts 12
cp 1
rs 2.8
cc 12
nc 2048
nop 1
crap 12

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 rhertogh\Yii2Oauth2Server\helpers;
4
5
use yii\helpers\ArrayHelper;
6
7
class UrlHelper
8
{
9
    /**
10
     * Add additional parameters to a URL
11
     * @param string $url
12
     * @param array $params
13
     * @return string
14
     * @since 1.0.0
15
     */
16 9
    public static function addQueryParams($url, $params)
17
    {
18 9
        $parts = parse_url($url);
19
20 9
        if (!empty($parts['query'])) {
21 4
            parse_str($parts['query'], $queryParams);
22 4
            $queryParams = ArrayHelper::merge($queryParams, $params);
23
        } else {
24 5
            $queryParams = $params;
25
        }
26
27 9
        $parts['query'] = http_build_query($queryParams);
28
29 9
        return static::buildUrl($parts);
30
    }
31
32 9
    public static function buildUrl(array $components)
33
    {
34 9
        return
35 9
            (isset($components['scheme']) ? "{$components['scheme']}:" : '') .
36 9
            ((isset($components['user']) || isset($components['host'])) ? '//' : '') .
37 9
            (isset($components['user']) ? "{$components['user']}" : '') .
38 9
            (isset($components['pass']) ? ":{$components['pass']}" : '') .
39 9
            (isset($components['user']) ? '@' : '') .
40 9
            (isset($components['host']) ? "{$components['host']}" : '') .
41 9
            (isset($components['port']) ? ":{$components['port']}" : '') .
42 9
            (isset($components['path']) ? "{$components['path']}" : '') .
43 9
            (isset($components['query']) ? "?{$components['query']}" : '') .
44 9
            (isset($components['fragment']) ? "#{$components['fragment']}" : '');
45
    }
46
47
    public static function stripQueryAndFragment($url)
48
    {
49
        $components = parse_url($url);
50
        unset($components['query']);
51
        unset($components['fragment']);
52
        return UrlHelper::buildUrl($components);
53
    }
54
}
55