Complex classes like MySQLi often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use MySQLi, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
12 | class MySQLi extends _Abstract { |
||
13 | /** |
||
14 | * @var \MySQLi Instance of DB connection |
||
15 | */ |
||
16 | protected $instance; |
||
17 | /** |
||
18 | * @inheritdoc |
||
19 | */ |
||
20 | 22 | public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
|
21 | 22 | $start = microtime(true); |
|
22 | /** |
||
23 | * Parsing of $host variable, detecting port and persistent connection |
||
24 | */ |
||
25 | 22 | list($host, $port) = $this->get_host_and_port($host); |
|
26 | 22 | $this->instance = new \MySQLi($host, $user, $password, $database, $port); |
|
27 | 22 | if (!is_object($this->instance) || $this->instance->connect_errno) { |
|
28 | 1 | return; |
|
29 | } |
||
30 | 22 | $this->database = $database; |
|
31 | /** |
||
32 | * Changing DB charset if necessary |
||
33 | */ |
||
34 | 22 | if ($this->instance->character_set_name() != 'utf8mb4') { |
|
35 | 22 | $this->instance->set_charset('utf8mb4'); |
|
36 | } |
||
37 | 22 | $this->connected = true; |
|
38 | 22 | $this->connecting_time = microtime(true) - $start; |
|
39 | 22 | $this->db_type = 'mysql'; |
|
40 | 22 | $this->prefix = $prefix; |
|
41 | 22 | } |
|
42 | /** |
||
43 | * Parse host string into host and port separately |
||
44 | * |
||
45 | * Understands `p:` prefix for persistent connections |
||
46 | * |
||
47 | * @param string $host_string |
||
48 | * |
||
49 | * @return array |
||
50 | */ |
||
51 | 22 | protected function get_host_and_port ($host_string) { |
|
71 | /** |
||
72 | * @inheritdoc |
||
73 | * |
||
74 | * @return false|mysqli_result |
||
75 | */ |
||
76 | 22 | protected function q_internal ($query, $parameters = []) { |
|
104 | /** |
||
105 | * @inheritdoc |
||
106 | * |
||
107 | * @param false|mysqli_result $query_result |
||
108 | */ |
||
109 | 21 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
|
128 | /** |
||
129 | * @inheritdoc |
||
130 | */ |
||
131 | 15 | public function id () { |
|
134 | /** |
||
135 | * @inheritdoc |
||
136 | */ |
||
137 | 1 | public function affected () { |
|
140 | /** |
||
141 | * @inheritdoc |
||
142 | * |
||
143 | * @param false|mysqli_result $query_result |
||
144 | */ |
||
145 | 18 | public function free ($query_result) { |
|
151 | /** |
||
152 | * @inheritdoc |
||
153 | */ |
||
154 | 2 | public function columns ($table, $like = false) { |
|
166 | /** |
||
167 | * @inheritdoc |
||
168 | */ |
||
169 | 1 | public function tables ($like = false) { |
|
177 | /** |
||
178 | * @inheritdoc |
||
179 | */ |
||
180 | 22 | protected function s_internal ($string, $single_quotes_around) { |
|
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | 1 | public function server () { |
|
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | 2 | public function __destruct () { |
|
199 | } |
||
200 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: