Completed
Push — master ( d819e3...4fe283 )
by Christopher
03:01 queued 12s
created

MySQLConnector   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 128
Duplicated Lines 6.25 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 87.5%

Importance

Changes 3
Bugs 2 Features 3
Metric Value
wmc 14
c 3
b 2
f 3
lcom 1
cbo 1
dl 8
loc 128
ccs 49
cts 56
cp 0.875
rs 10

3 Methods

Rating   Name   Duplication   Size   Complexity  
C connect() 0 35 7
B query() 8 48 6
A escape() 0 4 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
<?php
2
3
namespace AsyncPHP\Icicle\Database\Connector;
4
5
use AsyncPHP\Icicle\Database\Connector;
6
use Icicle\Loop;
7
use Icicle\Promise\Deferred;
8
use Icicle\Promise\PromiseInterface;
9
use InvalidArgumentException;
10
use MySQLi;
11
12
final class MySQLConnector implements Connector
13
{
14
    /**
15
     * @var null|MySQLi
16
     */
17
    private $connection = null;
18
19
    /**
20
     * @var bool
21
     */
22
    private $ready = true;
23
24
    /**
25
     * @var float
26
     */
27
    private $poll = 0.000001;
28
29
    /**
30
     * @inheritdoc
31
     *
32
     * @param array $config
33
     *
34
     * @throws InvalidArgumentException
35
     */
36 1
    public function connect(array $config)
37
    {
38 1
        if (!isset($config["host"])) {
39 1
            $config["host"] = "127.0.0.1";
40 1
        }
41
42 1
        if (!isset($config["username"])) {
43
            throw new InvalidArgumentException("Undefined username");
44
        }
45
46 1
        if (!isset($config["password"])) {
47
            throw new InvalidArgumentException("Undefined password");
48
        }
49
50 1
        if (!isset($config["database"])) {
51
            throw new InvalidArgumentException("Undefined database");
52
        }
53
54 1
        if (!isset($config["port"])) {
55 1
            $config["port"] = 3306;
56 1
        }
57
58 1
        if (!isset($config["socket"])) {
59 1
            $config["socket"] = null;
60 1
        }
61
62 1
        $this->connection = mysqli_connect(
63 1
            $config["host"],
64 1
            $config["username"],
65 1
            $config["password"],
66 1
            $config["database"],
67 1
            $config["port"],
68 1
            $config["socket"]
69 1
        );
70 1
    }
71
72
    /**
73
     * @inheritdoc
74
     *
75
     * @param string $query
76
     *
77
     * @return PromiseInterface
78
     */
79 1
    public function query($query)
80
    {
81 1
        $deferred = new Deferred();
82
83
        $links = $errors = $rejects = [
84 1
            $this->connection,
85 1
        ];
86
87
        $outer = Loop\periodic($this->poll, function () use (&$outer, $links, $errors, $rejects, $query, $deferred) {
0 ignored issues
show
Unused Code introduced by
$outer is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
88 1
            if ($this->ready) {
89 1
                $this->ready = false;
90 1
                $outer->stop();
91
92 1
                $result = $this->connection->query($query, MYSQLI_ASYNC);
93
94 1 View Code Duplication
                if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
95
                    $this->ready = true;
96
                    return $deferred->reject($this->connection->error);
97
                }
98
99 1
                $inner = Loop\periodic($this->poll, function () use (&$inner, $links, $errors, $rejects, $deferred) {
0 ignored issues
show
Unused Code introduced by
$inner is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
100 1
                    if (mysqli_poll($links, $errors, $rejects, $this->poll)) {
101 1
                        $inner->stop();
102
103 1
                        $result = $this->connection->reap_async_query();
104
105 1 View Code Duplication
                        if ($result === false) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
106
                            $this->ready = true;
107
                            return $deferred->reject($this->connection->error);
108
                        }
109
110 1
                        if ($result === true) {
111 1
                            $this->ready = true;
112 1
                            return $deferred->resolve();
113
                        }
114
115 1
                        $rows = $result->fetch_all(MYSQLI_ASSOC);
116 1
                        $result->free();
117
118 1
                        $this->ready = true;
119 1
                        return $deferred->resolve($rows);
120
                    }
121 1
                });
122 1
            }
123 1
        });
124
125 1
        return $deferred->getPromise();
126
    }
127
128
    /**
129
     * @inheritdoc
130
     *
131
     * @param mixed $value
132
     *
133
     * @return mixed
134
     */
135 1
    public function escape($value)
136
    {
137 1
        return mysqli_real_escape_string($this->connection, $value);
138
    }
139
}
140