Completed
Push — development ( eedbab...777f16 )
by Sebastian
13s
created

mysqlims   A

Complexity

Total Complexity 20

Size/Duplication

Total Lines 79
Duplicated Lines 35.44 %

Coupling/Cohesion

Components 1
Dependencies 1

Importance

Changes 0
Metric Value
wmc 20
lcom 1
cbo 1
dl 28
loc 79
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
C __construct() 12 32 12
A prepare() 8 8 4
A query() 8 8 4

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
 * This class will run queries on master/slave servers depending on the query itself.
4
 */
5
class mysqlims extends mysqli
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    private $mysqliW;
8
    private $mysqliR = null;
9
10
    /*
11
     * Pass main and slave connection arrays to the constructor, and strict as true/false
12
     *
13
     * @param array $main
14
     * @param array $slave
15
     * @param boolean $strict
16
     *
17
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
18
     */
19
    public function __construct($main, $slave = false, $strict = false)
20
    {
21
        if ($strict) {
22
            $this->mysqliW = new mysqli_strict($main['host'],
0 ignored issues
show
Unused Code introduced by
The call to mysqli_strict::__construct() has too many arguments starting with $main['host'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
23
                $main['user'], $main['pass'],
24
                $main['name'], $main['port']);
25 View Code Duplication
            if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled']
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...
26
                === true) {
27
                $this->mysqliR = new mysqli_strict($slave['host'],
0 ignored issues
show
Unused Code introduced by
The call to mysqli_strict::__construct() has too many arguments starting with $slave['host'].

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
28
                    $slave['user'], $slave['pass'],
29
                    $slave['name'], $slave['port']);
30
            }
31
        } else {
32
            $this->mysqliW = new mysqli($main['host'],
33
                $main['user'], $main['pass'],
34
                $main['name'], $main['port']);
35 View Code Duplication
            if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled']
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...
36
                === true) {
37
                $this->mysqliR = new mysqli($slave['host'],
38
                    $slave['user'], $slave['pass'],
39
                    $slave['name'], $slave['port']);
40
            }
41
        }
42
43
        if ($this->mysqliW->connect_errno) {
44
            throw new Exception("Failed to connect to MySQL: (".$this->mysqliW->connect_errno.") ".$this->mysqliW->connect_error);
45
        }
46
47
        if ($this->mysqliR->connect_errno) {
48
            throw new Exception("Failed to connect to MySQL: (".$this->mysqliR->connect_errno.") ".$this->mysqliR->connect_error);
49
        }
50
    }
51
52
    /*
53
     * Override standard mysqli_prepare to select master/slave server
54
     * @param $string query
55
     *
56
     * @return mysqli_stmt
57
     */
58 View Code Duplication
    public function prepare($query)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
59
    {
60
        if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) {
61
            return $this->mysqliR->prepare($query);
62
        } else {
63
            return $this->mysqliW->prepare($query);
64
        }
65
    }
66
67
    /*
68
     * Override standard mysqli_query to select master/slave server
69
     * @param string $query
70
     * @param int $resultmode
71
     *
72
     * @return boolean
73
     * @return mixed
74
     */
75 View Code Duplication
    public function query($query, $resultmode = MYSQLI_STORE_RESULT)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in 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...
76
    {
77
        if (stripos($query, "SELECT") && stripos($query, "FOR UPDATE") === false && $this->mysqliR !== null) {/* Use readonly server */
78
            return $this->mysqliR->query($query, $resultmode);
79
        } else {
80
            return $this->mysqliW->query($query, $resultmode);
81
        }
82
    }
83
}