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 bool|mysqli_result |
||
| 75 | */ |
||
| 76 | 24 | protected function q_internal ($query, $parameters = []) { |
|
| 93 | /** |
||
| 94 | * @param string $query |
||
| 95 | * @param array $parameters |
||
| 96 | * |
||
| 97 | * @return bool|mysqli_result |
||
| 98 | */ |
||
| 99 | 24 | protected function q_internal_internal ($query, $parameters) { |
|
| 116 | /** |
||
| 117 | * @inheritdoc |
||
| 118 | * |
||
| 119 | * @param false|mysqli_result $query_result |
||
| 120 | */ |
||
| 121 | 23 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
|
| 140 | /** |
||
| 141 | * @inheritdoc |
||
| 142 | */ |
||
| 143 | 17 | public function id () { |
|
| 146 | /** |
||
| 147 | * @inheritdoc |
||
| 148 | */ |
||
| 149 | 1 | public function affected () { |
|
| 152 | /** |
||
| 153 | * @inheritdoc |
||
| 154 | * |
||
| 155 | * @param false|mysqli_result $query_result |
||
| 156 | */ |
||
| 157 | 20 | public function free ($query_result) { |
|
| 163 | /** |
||
| 164 | * @inheritdoc |
||
| 165 | */ |
||
| 166 | 2 | public function columns ($table, $like = false) { |
|
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | 1 | public function tables ($like = false) { |
|
| 189 | /** |
||
| 190 | * @inheritdoc |
||
| 191 | */ |
||
| 192 | 22 | protected function s_internal ($string, $single_quotes_around) { |
|
| 196 | /** |
||
| 197 | * @inheritdoc |
||
| 198 | */ |
||
| 199 | 1 | public function server () { |
|
| 202 | /** |
||
| 203 | * @inheritdoc |
||
| 204 | */ |
||
| 205 | 2 | public function __destruct () { |
|
| 211 | } |
||
| 212 |