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 |
||
9 | class MySQLi extends _Abstract { |
||
10 | /** |
||
11 | * @var \MySQLi Instance of DB connection |
||
12 | */ |
||
13 | protected $instance; |
||
14 | /** |
||
15 | * @inheritdoc |
||
16 | */ |
||
17 | function __construct ($database, $user = '', $password = '', $host = 'localhost', $charset = 'utf8mb4', $prefix = '') { |
||
39 | /** |
||
40 | * Parse host string into host and port separately |
||
41 | * |
||
42 | * Understands `p:` prefix for persistent connections |
||
43 | * |
||
44 | * @param string $host_string |
||
45 | * |
||
46 | * @return array |
||
47 | */ |
||
48 | protected function get_host_and_port ($host_string) { |
||
68 | /** |
||
69 | * @inheritdoc |
||
70 | * |
||
71 | * @return false|\mysqli_result |
||
72 | */ |
||
73 | protected function q_internal ($query) { |
||
89 | /** |
||
90 | * @inheritdoc |
||
91 | */ |
||
92 | protected function q_multi_internal ($query) { |
||
104 | /** |
||
105 | * @deprecated |
||
106 | * @todo remove after 4.x release |
||
107 | * |
||
108 | * @inheritdoc |
||
109 | */ |
||
110 | function n ($query_result) { |
||
117 | /** |
||
118 | * @inheritdoc |
||
119 | * |
||
120 | * @param false|\mysqli_result $query_result |
||
121 | */ |
||
122 | function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
||
141 | /** |
||
142 | * @inheritdoc |
||
143 | */ |
||
144 | function id () { |
||
147 | /** |
||
148 | * @inheritdoc |
||
149 | */ |
||
150 | function affected () { |
||
153 | /** |
||
154 | * @inheritdoc |
||
155 | * |
||
156 | * @param false|\mysqli_result $query_result |
||
157 | */ |
||
158 | function free ($query_result) { |
||
164 | /** |
||
165 | * @inheritdoc |
||
166 | */ |
||
167 | function columns ($table, $like = false) { |
||
179 | /** |
||
180 | * @inheritdoc |
||
181 | */ |
||
182 | function tables ($like = false) { |
||
190 | /** |
||
191 | * @inheritdoc |
||
192 | */ |
||
193 | protected function s_internal ($string, $single_quotes_around) { |
||
197 | /** |
||
198 | * @inheritdoc |
||
199 | */ |
||
200 | function server () { |
||
203 | /** |
||
204 | * @inheritdoc |
||
205 | */ |
||
206 | function __destruct () { |
||
212 | } |
||
213 |
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: