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 |
||
13 | class MySQLi extends _Abstract { |
||
14 | /** |
||
15 | * @var \MySQLi Instance of DB connection |
||
16 | */ |
||
17 | protected $instance; |
||
18 | /** |
||
19 | * @inheritdoc |
||
20 | */ |
||
21 | public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
||
49 | /** |
||
50 | * Parse host string into host and port separately |
||
51 | * |
||
52 | * Understands `p:` prefix for persistent connections |
||
53 | * |
||
54 | * @param string $host_string |
||
55 | * |
||
56 | * @return array |
||
57 | */ |
||
58 | protected function get_host_and_port ($host_string) { |
||
78 | /** |
||
79 | * @inheritdoc |
||
80 | * |
||
81 | * @return bool|mysqli_result|mysqli_stmt |
||
82 | */ |
||
83 | protected function q_internal ($query, $parameters = []) { |
||
100 | /** |
||
101 | * @param string $query |
||
102 | * @param array $parameters |
||
103 | * |
||
104 | * @return bool|mysqli_result|mysqli_stmt |
||
105 | */ |
||
106 | protected function q_internal_once ($query, $parameters) { |
||
125 | /** |
||
126 | * @param mysqli_stmt $stmt |
||
127 | * @param array $local_parameters |
||
128 | * |
||
129 | * @return bool |
||
130 | */ |
||
131 | protected function q_internal_once_bind_param ($stmt, $local_parameters) { |
||
146 | /** |
||
147 | * @inheritdoc |
||
148 | * |
||
149 | * @param false|mysqli_result|mysqli_stmt $query_result_stmt |
||
150 | */ |
||
151 | public function f ($query_result_stmt, $single_column = false, $array = false, $indexed = false) { |
||
160 | /** |
||
161 | * @param mysqli_result $query_result |
||
162 | * @param bool $single_column |
||
163 | * @param bool $array |
||
164 | * @param bool $indexed |
||
165 | * |
||
166 | * @return array|bool|mixed |
||
167 | */ |
||
168 | protected function f_result ($query_result, $single_column, $array, $indexed) { |
||
184 | /** |
||
185 | * @param mysqli_stmt $stmt |
||
186 | * @param bool $single_column |
||
187 | * @param bool $array |
||
188 | * @param bool $indexed |
||
189 | * |
||
190 | * @return array|bool|mixed |
||
191 | */ |
||
192 | protected function f_stmt ($stmt, $single_column, $array, $indexed) { |
||
213 | /** |
||
214 | * @param mysqli_stmt $stmt |
||
215 | * @param array $result |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | protected function f_stmt_bind_result ($stmt, &$result) { |
||
231 | /** |
||
232 | * @param mysqli_stmt $stmt |
||
233 | * @param array $result |
||
234 | * @param bool $single_column |
||
235 | * @param bool $indexed |
||
236 | * @param string[] $columns |
||
237 | * |
||
238 | * @return array|bool |
||
239 | */ |
||
240 | protected function f_stmt_internal ($stmt, $result, $single_column, $indexed, $columns) { |
||
258 | /** |
||
259 | * @inheritdoc |
||
260 | */ |
||
261 | public function id () { |
||
264 | /** |
||
265 | * @inheritdoc |
||
266 | */ |
||
267 | public function affected () { |
||
270 | /** |
||
271 | * @inheritdoc |
||
272 | * |
||
273 | * @param false|mysqli_result|mysqli_stmt $query_result_stmt |
||
274 | */ |
||
275 | public function free ($query_result_stmt) { |
||
284 | /** |
||
285 | * @inheritdoc |
||
286 | */ |
||
287 | public function columns ($table, $like = false) { |
||
299 | /** |
||
300 | * @inheritdoc |
||
301 | */ |
||
302 | public function tables ($like = false) { |
||
310 | /** |
||
311 | * @inheritdoc |
||
312 | */ |
||
313 | protected function s_internal ($string, $single_quotes_around) { |
||
317 | /** |
||
318 | * @inheritdoc |
||
319 | */ |
||
320 | public function server () { |
||
323 | /** |
||
324 | * @inheritdoc |
||
325 | */ |
||
326 | public function __destruct () { |
||
332 | } |
||
333 |