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 | 32 | 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 | 32 | protected function get_host_port_and_persistent ($host_string) { |
|
| 69 | /** |
||
| 70 | * @inheritdoc |
||
| 71 | */ |
||
| 72 | 32 | 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 | 32 | protected function convert_sql ($query) { |
|
| 124 | /** |
||
| 125 | * @inheritdoc |
||
| 126 | * |
||
| 127 | * @return false|resource |
||
| 128 | */ |
||
| 129 | 32 | protected function q_internal ($query, $parameters = []) { |
|
| 141 | /** |
||
| 142 | * @param string $query |
||
| 143 | * |
||
| 144 | * @return string |
||
| 145 | */ |
||
| 146 | 27 | protected function convert_prepared_statements_syntax ($query) { |
|
| 154 | /** |
||
| 155 | * @inheritdoc |
||
| 156 | * |
||
| 157 | * @param false|resource $query_result |
||
| 158 | */ |
||
| 159 | 32 | public function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
|
| 175 | /** |
||
| 176 | * @inheritdoc |
||
| 177 | */ |
||
| 178 | 24 | public function id () { |
|
| 181 | /** |
||
| 182 | * @inheritdoc |
||
| 183 | */ |
||
| 184 | 1 | public function affected () { |
|
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | * |
||
| 190 | * @param false|resource $query_result |
||
| 191 | */ |
||
| 192 | 27 | public function free ($query_result) { |
|
| 198 | /** |
||
| 199 | * @inheritdoc |
||
| 200 | */ |
||
| 201 | 3 | public function columns ($table, $like = false) { |
|
| 223 | /** |
||
| 224 | * @inheritdoc |
||
| 225 | */ |
||
| 226 | 1 | public function tables ($like = false) { |
|
| 246 | /** |
||
| 247 | * @inheritdoc |
||
| 248 | */ |
||
| 249 | 29 | protected function s_internal ($string, $single_quotes_around) { |
|
| 252 | /** |
||
| 253 | * @inheritdoc |
||
| 254 | */ |
||
| 255 | 1 | public function server () { |
|
| 258 | /** |
||
| 259 | * @inheritdoc |
||
| 260 | */ |
||
| 261 | 1 | public function __destruct () { |
|
| 267 | } |
||
| 268 |