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 |
||
| 9 | class MySQLi extends _Abstract { |
||
| 10 | /** |
||
| 11 | * @var \MySQLi Instance of DB connection |
||
| 12 | */ |
||
| 13 | protected $instance; |
||
| 14 | /** |
||
| 15 | * @inheritdoc |
||
| 16 | */ |
||
| 17 | function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
||
| 18 | $start = microtime(true); |
||
| 19 | /** |
||
| 20 | * Parsing of $host variable, detecting port and persistent connection |
||
| 21 | */ |
||
| 22 | list($host, $port) = $this->get_host_and_port($host); |
||
| 23 | $this->instance = new \MySQLi($host, $user, $password, $database, $port); |
||
| 24 | if (!is_object($this->instance) || $this->instance->connect_errno) { |
||
| 25 | return; |
||
| 26 | } |
||
| 27 | $this->database = $database; |
||
| 28 | /** |
||
| 29 | * Changing DB charset if necessary |
||
| 30 | */ |
||
| 31 | if ($this->instance->character_set_name() != 'utf8mb4') { |
||
| 32 | $this->instance->set_charset('utf8mb4'); |
||
| 33 | } |
||
| 34 | $this->connected = true; |
||
| 35 | $this->connecting_time = microtime(true) - $start; |
||
| 36 | $this->db_type = 'mysql'; |
||
| 37 | $this->prefix = $prefix; |
||
| 38 | } |
||
| 39 | /** |
||
| 40 | * Parse host string into host and port separately |
||
| 41 | * |
||
| 42 | * Understands `p:` prefix for persistent connections |
||
| 43 | * |
||
| 44 | * @param string $host_string |
||
| 45 | * |
||
| 46 | * @return array |
||
| 47 | */ |
||
| 48 | protected function get_host_and_port ($host_string) { |
||
| 49 | $host = explode(':', $host_string); |
||
| 50 | $port = ini_get('mysqli.default_port') ?: 3306; |
||
| 51 | switch (count($host)) { |
||
| 52 | case 1: |
||
| 53 | $host = $host[0]; |
||
| 54 | break; |
||
| 55 | case 2: |
||
| 56 | if ($host[0] == 'p') { |
||
| 57 | $host = "$host[0]:$host[1]"; |
||
| 58 | } else { |
||
| 59 | list($host, $port) = $host; |
||
| 60 | } |
||
| 61 | break; |
||
| 62 | case 3: |
||
| 63 | $port = $host[2]; |
||
| 64 | $host = "$host[0]:$host[1]"; |
||
| 65 | } |
||
| 66 | return [$host, $port]; |
||
| 67 | } |
||
| 68 | /** |
||
| 69 | * @inheritdoc |
||
| 70 | * |
||
| 71 | * @return false|\mysqli_result |
||
| 72 | */ |
||
| 73 | protected function q_internal ($query) { |
||
| 88 | /** |
||
| 89 | * @inheritdoc |
||
| 90 | */ |
||
| 91 | protected function q_multi_internal ($query) { |
||
| 98 | /** |
||
| 99 | * @inheritdoc |
||
| 100 | * |
||
| 101 | * @param false|\mysqli_result $query_result |
||
| 102 | */ |
||
| 103 | function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
||
| 122 | /** |
||
| 123 | * @inheritdoc |
||
| 124 | */ |
||
| 125 | function id () { |
||
| 128 | /** |
||
| 129 | * @inheritdoc |
||
| 130 | */ |
||
| 131 | function affected () { |
||
| 134 | /** |
||
| 135 | * @inheritdoc |
||
| 136 | * |
||
| 137 | * @param false|\mysqli_result $query_result |
||
| 138 | */ |
||
| 139 | function free ($query_result) { |
||
| 145 | /** |
||
| 146 | * @inheritdoc |
||
| 147 | */ |
||
| 148 | function columns ($table, $like = false) { |
||
| 160 | /** |
||
| 161 | * @inheritdoc |
||
| 162 | */ |
||
| 163 | function tables ($like = false) { |
||
| 171 | /** |
||
| 172 | * @inheritdoc |
||
| 173 | */ |
||
| 174 | protected function s_internal ($string, $single_quotes_around) { |
||
| 178 | /** |
||
| 179 | * @inheritdoc |
||
| 180 | */ |
||
| 181 | function server () { |
||
| 184 | /** |
||
| 185 | * @inheritdoc |
||
| 186 | */ |
||
| 187 | function __destruct () { |
||
| 193 | } |
||
| 194 |