Url::clean()   C
last analyzed

Complexity

Conditions 11
Paths 193

Size

Total Lines 36
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 21
CRAP Score 11

Importance

Changes 0
Metric Value
cc 11
eloc 20
c 0
b 0
f 0
nc 193
nop 1
dl 0
loc 36
ccs 21
cts 21
cp 1
crap 11
rs 6.5416

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
declare(strict_types=1);
4
5
namespace Conia\Boiler;
6
7
use Conia\Boiler\Exception\RuntimeException;
8
9
class Url
10
{
11 5
    public static function clean(string $url): string
12
    {
13 5
        $parsed = parse_url($url);
14
15 5
        if (!$parsed) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $parsed of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
16 1
            throw new RuntimeException('Invalid Url');
17
        }
18
19
20 4
        $path = empty($parsed['scheme']) ? '' : $parsed['scheme'] . '://';
21 4
        $path .= rawurlencode($parsed['user'] ?? '');
22 4
        $path .= rawurlencode(empty($parsed['pass']) ? '' : ':' . $parsed['pass']);
23 4
        $path .= !empty($parsed['pass']) || !empty($parsed['pass']) ? '@' : '';
24 4
        $path .= $parsed['host'] ?? '';
25 4
        $path .= empty($parsed['port']) ? '' : ':' . $parsed['port'];
26
27 4
        $segments = [];
28
29 4
        foreach (explode('/', $parsed['path'] ?? '') as $segment) {
30 4
            $segments[] = urlencode($segment);
31
        }
32
33 4
        $path .= implode('/', $segments);
34 4
        $query = '';
35
36 4
        if (!empty($parsed['query'])) {
37 1
            parse_str($parsed['query'], $array);
38
39 1
            if (count($array) > 0) {
40 1
                $query .= '?' . http_build_query($array);
41
            }
42
        }
43
44 4
        $query .= empty($parsed['fragment']) ? '' : '#' . rawurlencode($parsed['fragment']);
45
46 4
        return $path . $query;
47
    }
48
}
49