Complex classes like PostgreSQL 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 PostgreSQL, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class PostgreSQL extends _Abstract { |
||
10 | /** |
||
11 | * @var resource DB connection handler |
||
12 | */ |
||
13 | protected $handler; |
||
14 | /** |
||
15 | * @var resource |
||
16 | */ |
||
17 | protected $query_result; |
||
18 | /** |
||
19 | * @inheritdoc |
||
20 | */ |
||
21 | public function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
||
38 | /** |
||
39 | * Parse host string into host, port and persistent separately |
||
40 | * |
||
41 | * Understands `p:` prefix for persistent connections |
||
42 | * |
||
43 | * @param string $host_string |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | protected function get_host_port_and_persistent ($host_string) { |
||
69 | /** |
||
70 | * @inheritdoc |
||
71 | */ |
||
72 | public function q ($query, ...$params) { |
||
78 | /** |
||
79 | * Convert small subset of MySQL queries into PostgreSQL-compatible syntax |
||
80 | * |
||
81 | * @param string $query |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | protected function convert_sql ($query) { |
||
124 | /** |
||
125 | * @inheritdoc |
||
126 | * |
||
127 | * @return false|resource |
||
128 | */ |
||
129 | protected function q_internal ($query, $parameters = []) { |
||
141 | /** |
||
142 | * @param string|string[] $query |
||
143 | * |
||
144 | * @return string|string[] |
||
145 | */ |
||
146 | protected function convert_prepared_statements_syntax ($query) { |
||
157 | /** |
||
158 | * @inheritdoc |
||
159 | * |
||
160 | * @param false|resource $query_result |
||
161 | */ |
||
162 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
||
178 | /** |
||
179 | * @inheritdoc |
||
180 | */ |
||
181 | public function id () { |
||
184 | /** |
||
185 | * @inheritdoc |
||
186 | */ |
||
187 | public function affected () { |
||
190 | /** |
||
191 | * @inheritdoc |
||
192 | * |
||
193 | * @param false|resource $query_result |
||
194 | */ |
||
195 | public function free ($query_result) { |
||
201 | /** |
||
202 | * @inheritdoc |
||
203 | */ |
||
204 | public function columns ($table, $like = false) { |
||
226 | /** |
||
227 | * @inheritdoc |
||
228 | */ |
||
229 | public function tables ($like = false) { |
||
249 | /** |
||
250 | * @inheritdoc |
||
251 | */ |
||
252 | protected function s_internal ($string, $single_quotes_around) { |
||
255 | /** |
||
256 | * @inheritdoc |
||
257 | */ |
||
258 | public function server () { |
||
261 | /** |
||
262 | * @inheritdoc |
||
263 | */ |
||
264 | public function __destruct () { |
||
270 | } |
||
271 |