Completed
Pull Request — master (#2)
by
unknown
02:18
created

Config::__construct()   D

Complexity

Conditions 13
Paths 260

Size

Total Lines 50
Code Lines 29

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 50
rs 4
c 0
b 0
f 0
cc 13
eloc 29
nc 260
nop 1

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 Kraken\Redis;
4
5
use InvalidArgumentException;
6
7
class Config
8
{
9
    public function __construct($target)
10
    {
11
        if ($target === null) {
12
            $target = 'tcp://127.0.0.1';
13
        }
14
15
        if (strpos($target, '://') === false) {
16
            $target = 'tcp://' . $target;
17
        }
18
19
        $parts = parse_url($target);
20
        if ($parts === false || !isset($parts['host']) || $parts['scheme'] !== 'tcp') {
21
            throw new InvalidArgumentException('Given URL can not be parsed');
22
        }
23
24
        if (!isset($parts['port'])) {
25
            $parts['port'] = 6379;
26
            $this->port = 6379;
0 ignored issues
show
Bug introduced by
The property port does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
27
        }
28
29
        if ($parts['host'] === 'localhost') {
30
            $parts['host'] = '127.0.0.1';
31
            $this->host = '127.0.0.1';
0 ignored issues
show
Bug introduced by
The property host does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
32
        }
33
34
        $auth = null;
35
        if (isset($parts['user'])) {
36
            $auth = $parts['user'];
37
            $this->auth = $auth;
0 ignored issues
show
Bug introduced by
The property auth does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
38
        }
39
40
        if (isset($parts['pass'])) {
41
            $auth .= ':' . $parts['pass'];
42
        }
43
44
        if ($auth !== null) {
45
            $parts['auth'] = $auth;
46
        }
47
48
        if (isset($parts['path']) && $parts['path'] !== '') {
49
            $parts['db'] = substr($parts['path'], 1);
50
        }
51
52
        unset($parts['scheme'], $parts['user'], $parts['pass'], $parts['path']);
53
54
        $this->host = $parts['host'];
55
        $this->port = $parts['port'];
56
        $this->auth = $auth;
57
        $this->db =  $parts['db'];
0 ignored issues
show
Bug introduced by
The property db does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
58
    }
59
60
    public function getHost()
61
    {
62
63
    }
64
65
    public function getPort()
66
    {
67
68
    }
69
70
    public function getDb()
71
    {
72
73
    }
74
}