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 | 24 | public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
|
| 48 | /** |
||
| 49 | * Parse host string into host and port separately |
||
| 50 | * |
||
| 51 | * Understands `p:` prefix for persistent connections |
||
| 52 | * |
||
| 53 | * @param string $host_string |
||
| 54 | * |
||
| 55 | * @return array |
||
| 56 | */ |
||
| 57 | 24 | protected function get_host_and_port ($host_string) { |
|
| 77 | /** |
||
| 78 | * @inheritdoc |
||
| 79 | * |
||
| 80 | * @return bool|mysqli_result |
||
| 81 | */ |
||
| 82 | 24 | protected function q_internal ($query, $parameters = []) { |
|
| 99 | /** |
||
| 100 | * @param string $query |
||
| 101 | * @param array $parameters |
||
| 102 | * |
||
| 103 | * @return bool|mysqli_result |
||
| 104 | */ |
||
| 105 | 24 | protected function q_internal_internal ($query, $parameters) { |
|
| 125 | /** |
||
| 126 | * @inheritdoc |
||
| 127 | * |
||
| 128 | * @param false|mysqli_result $query_result |
||
| 129 | */ |
||
| 130 | 23 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
|
| 149 | /** |
||
| 150 | * @inheritdoc |
||
| 151 | */ |
||
| 152 | 17 | public function id () { |
|
| 155 | /** |
||
| 156 | * @inheritdoc |
||
| 157 | */ |
||
| 158 | 1 | public function affected () { |
|
| 161 | /** |
||
| 162 | * @inheritdoc |
||
| 163 | * |
||
| 164 | * @param false|mysqli_result $query_result |
||
| 165 | */ |
||
| 166 | 20 | public function free ($query_result) { |
|
| 172 | /** |
||
| 173 | * @inheritdoc |
||
| 174 | */ |
||
| 175 | 2 | public function columns ($table, $like = false) { |
|
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | */ |
||
| 190 | 1 | public function tables ($like = false) { |
|
| 198 | /** |
||
| 199 | * @inheritdoc |
||
| 200 | */ |
||
| 201 | 22 | protected function s_internal ($string, $single_quotes_around) { |
|
| 205 | /** |
||
| 206 | * @inheritdoc |
||
| 207 | */ |
||
| 208 | 1 | public function server () { |
|
| 211 | /** |
||
| 212 | * @inheritdoc |
||
| 213 | */ |
||
| 214 | 2 | public function __destruct () { |
|
| 220 | } |
||
| 221 |