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 | 32 | 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 | 32 | protected function get_host_and_port ($host_string) { |
|
| 78 | /** |
||
| 79 | * @inheritdoc |
||
| 80 | * |
||
| 81 | * @return bool|mysqli_result|mysqli_stmt |
||
| 82 | */ |
||
| 83 | 32 | 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 | 32 | protected function q_internal_once ($query, $parameters) { |
|
| 125 | /** |
||
| 126 | * @param mysqli_stmt $stmt |
||
| 127 | * @param array $local_parameters |
||
| 128 | * |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 27 | protected function q_internal_once_bind_param ($stmt, $local_parameters) { |
|
| 137 | /** |
||
| 138 | * @inheritdoc |
||
| 139 | * |
||
| 140 | * @param false|mysqli_result|mysqli_stmt $query_result_stmt |
||
| 141 | */ |
||
| 142 | 32 | public function f ($query_result_stmt, $single_column = false, $array = false, $indexed = false) { |
|
| 151 | /** |
||
| 152 | * @param mysqli_result $query_result |
||
| 153 | * @param bool $single_column |
||
| 154 | * @param bool $array |
||
| 155 | * @param bool $indexed |
||
| 156 | * |
||
| 157 | * @return array|bool|mixed |
||
| 158 | */ |
||
| 159 | 30 | protected function f_result ($query_result, $single_column, $array, $indexed) { |
|
| 175 | /** |
||
| 176 | * @param mysqli_stmt $stmt |
||
| 177 | * @param bool $single_column |
||
| 178 | * @param bool $array |
||
| 179 | * @param bool $indexed |
||
| 180 | * |
||
| 181 | * @return array|bool|mixed |
||
| 182 | */ |
||
| 183 | 25 | protected function f_stmt ($stmt, $single_column, $array, $indexed) { |
|
| 204 | /** |
||
| 205 | * @param mysqli_stmt $stmt |
||
| 206 | * @param array $result |
||
| 207 | * |
||
| 208 | * @return bool |
||
| 209 | */ |
||
| 210 | 25 | protected function f_stmt_bind_result ($stmt, &$result) { |
|
| 213 | /** |
||
| 214 | * @param mysqli_stmt $stmt |
||
| 215 | * @param array $result |
||
| 216 | * @param bool $single_column |
||
| 217 | * @param bool $indexed |
||
| 218 | * @param string[] $columns |
||
| 219 | * |
||
| 220 | * @return array|bool |
||
| 221 | */ |
||
| 222 | 25 | protected function f_stmt_internal ($stmt, $result, $single_column, $indexed, $columns) { |
|
| 240 | /** |
||
| 241 | * @inheritdoc |
||
| 242 | */ |
||
| 243 | 24 | public function id () { |
|
| 246 | /** |
||
| 247 | * @inheritdoc |
||
| 248 | */ |
||
| 249 | 1 | public function affected () { |
|
| 252 | /** |
||
| 253 | * @inheritdoc |
||
| 254 | * |
||
| 255 | * @param false|mysqli_result|mysqli_stmt $query_result_stmt |
||
| 256 | */ |
||
| 257 | 27 | public function free ($query_result_stmt) { |
|
| 266 | /** |
||
| 267 | * @inheritdoc |
||
| 268 | */ |
||
| 269 | 3 | public function columns ($table, $like = false) { |
|
| 281 | /** |
||
| 282 | * @inheritdoc |
||
| 283 | */ |
||
| 284 | 1 | public function tables ($like = false) { |
|
| 292 | /** |
||
| 293 | * @inheritdoc |
||
| 294 | */ |
||
| 295 | 29 | protected function s_internal ($string, $single_quotes_around) { |
|
| 299 | /** |
||
| 300 | * @inheritdoc |
||
| 301 | */ |
||
| 302 | 1 | public function server () { |
|
| 305 | /** |
||
| 306 | * @inheritdoc |
||
| 307 | */ |
||
| 308 | 1 | public function __destruct () { |
|
| 314 | } |
||
| 315 |