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 | 18 | function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
|
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 | 18 | protected function get_host_and_port ($host_string) { |
|
71 | /** |
||
72 | * @inheritdoc |
||
73 | * |
||
74 | * @return false|mysqli_result |
||
75 | */ |
||
76 | 18 | protected function q_internal ($query) { |
|
91 | /** |
||
92 | * @inheritdoc |
||
93 | */ |
||
94 | 5 | protected function q_multi_internal ($query) { |
|
101 | /** |
||
102 | * @inheritdoc |
||
103 | * |
||
104 | * @param false|mysqli_result $query_result |
||
105 | */ |
||
106 | 17 | function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
|
125 | /** |
||
126 | * @inheritdoc |
||
127 | */ |
||
128 | 13 | function id () { |
|
131 | /** |
||
132 | * @inheritdoc |
||
133 | */ |
||
134 | 1 | function affected () { |
|
137 | /** |
||
138 | * @inheritdoc |
||
139 | * |
||
140 | * @param false|mysqli_result $query_result |
||
141 | */ |
||
142 | 16 | function free ($query_result) { |
|
148 | /** |
||
149 | * @inheritdoc |
||
150 | */ |
||
151 | 2 | function columns ($table, $like = false) { |
|
163 | /** |
||
164 | * @inheritdoc |
||
165 | */ |
||
166 | 1 | function tables ($like = false) { |
|
174 | /** |
||
175 | * @inheritdoc |
||
176 | */ |
||
177 | 18 | protected function s_internal ($string, $single_quotes_around) { |
|
181 | /** |
||
182 | * @inheritdoc |
||
183 | */ |
||
184 | 1 | function server () { |
|
187 | /** |
||
188 | * @inheritdoc |
||
189 | */ |
||
190 | 1 | function __destruct () { |
|
196 | } |
||
197 |