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 |
||
0 ignored issues
–
show
|
|||
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 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
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
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'] |
|
26 | === true) { |
||
27 | $this->mysqliR = new mysqli_strict($slave['host'], |
||
0 ignored issues
–
show
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
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'] |
|
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) |
|
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) |
|
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 | } |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.