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 = '') { |
|
| 21 | 24 | $start = microtime(true); |
|
| 22 | /** |
||
| 23 | * Parsing of $host variable, detecting port and persistent connection |
||
| 24 | */ |
||
| 25 | 24 | list($host, $port) = $this->get_host_and_port($host); |
|
| 26 | 24 | $this->instance = new \MySQLi($host, $user, $password, $database, $port); |
|
| 27 | 24 | if (!is_object($this->instance) || $this->instance->connect_errno) { |
|
| 28 | 1 | return; |
|
| 29 | } |
||
| 30 | 24 | $this->database = $database; |
|
| 31 | /** |
||
| 32 | * Changing DB charset if necessary |
||
| 33 | */ |
||
| 34 | 24 | if ($this->instance->character_set_name() != 'utf8mb4') { |
|
| 35 | 24 | $this->instance->set_charset('utf8mb4'); |
|
| 36 | } |
||
| 37 | 24 | $this->connected = true; |
|
| 38 | 24 | $this->connecting_time = microtime(true) - $start; |
|
| 39 | 24 | $this->db_type = 'mysql'; |
|
| 40 | 24 | $this->prefix = $prefix; |
|
| 41 | 24 | } |
|
| 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 | 24 | protected function get_host_and_port ($host_string) { |
|
| 71 | /** |
||
| 72 | * @inheritdoc |
||
| 73 | * |
||
| 74 | * @return false|mysqli_result |
||
| 75 | */ |
||
| 76 | 24 | protected function q_internal ($query, $parameters = []) { |
|
| 106 | /** |
||
| 107 | * @inheritdoc |
||
| 108 | * |
||
| 109 | * @param false|mysqli_result $query_result |
||
| 110 | */ |
||
| 111 | 23 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
|
| 130 | /** |
||
| 131 | * @inheritdoc |
||
| 132 | */ |
||
| 133 | 17 | public function id () { |
|
| 136 | /** |
||
| 137 | * @inheritdoc |
||
| 138 | */ |
||
| 139 | 1 | public function affected () { |
|
| 142 | /** |
||
| 143 | * @inheritdoc |
||
| 144 | * |
||
| 145 | * @param false|mysqli_result $query_result |
||
| 146 | */ |
||
| 147 | 20 | public function free ($query_result) { |
|
| 153 | /** |
||
| 154 | * @inheritdoc |
||
| 155 | */ |
||
| 156 | 2 | public function columns ($table, $like = false) { |
|
| 168 | /** |
||
| 169 | * @inheritdoc |
||
| 170 | */ |
||
| 171 | 1 | public function tables ($like = false) { |
|
| 179 | /** |
||
| 180 | * @inheritdoc |
||
| 181 | */ |
||
| 182 | 24 | protected function s_internal ($string, $single_quotes_around) { |
|
| 186 | /** |
||
| 187 | * @inheritdoc |
||
| 188 | */ |
||
| 189 | 1 | public function server () { |
|
| 192 | /** |
||
| 193 | * @inheritdoc |
||
| 194 | */ |
||
| 195 | 2 | public function __destruct () { |
|
| 201 | } |
||
| 202 |