These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
1 | <?php |
||
2 | /* |
||
3 | * This class will run queries on master/slave servers depending on the query itself. |
||
4 | */ |
||
5 | class mysqlims extends mysqli |
||
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 |
||
18 | */ |
||
19 | public function __construct($main, $slave = false, $strict = false) |
||
20 | { |
||
21 | if ($strict) { |
||
22 | $this->mysqliW = new mysqli_strict($main['host'], |
||
23 | $main['user'], $main['pass'], |
||
24 | $main['name'], $main['port']); |
||
25 | View Code Duplication | if ($slave && is_array($slave) && isset($slave['enabled']) && $slave['enabled'] |
|
26 | === true) { |
||
27 | $this->mysqliR = new mysqli_strict($slave['host'], |
||
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'] |
|
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
|
|||
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
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 | } |
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.