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 | function __construct ($database, $user = '', $password = '', $host = 'localhost', $prefix = '') { |
||
| 22 | $start = microtime(true); |
||
| 23 | /** |
||
| 24 | * Parsing of $host variable, detecting port and persistent connection |
||
| 25 | */ |
||
| 26 | list($host, $port, $persistent) = $this->get_host_port_and_persistent($host); |
||
| 27 | $connection_string = "host=$host port=$port dbname=$database user=$user password=$password options='--client_encoding=UTF8'"; |
||
| 28 | $this->handler = $persistent ? pg_connect($connection_string) : pg_pconnect($connection_string); |
||
| 29 | if (!is_resource($this->handler)) { |
||
| 30 | return; |
||
| 31 | } |
||
| 32 | $this->database = $database; |
||
| 33 | $this->connected = true; |
||
| 34 | $this->connecting_time = microtime(true) - $start; |
||
| 35 | $this->db_type = 'postgresql'; |
||
| 36 | $this->prefix = $prefix; |
||
| 37 | } |
||
| 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 | function q ($query, $params = [], ...$param) { |
||
| 113 | /** |
||
| 114 | * @inheritdoc |
||
| 115 | * |
||
| 116 | * @return false|resource |
||
| 117 | */ |
||
| 118 | protected function q_internal ($query) { |
||
| 124 | /** |
||
| 125 | * @inheritdoc |
||
| 126 | */ |
||
| 127 | protected function q_multi_internal ($query) { |
||
| 130 | /** |
||
| 131 | * @inheritdoc |
||
| 132 | * |
||
| 133 | * @param false|resource $query_result |
||
| 134 | */ |
||
| 135 | function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
||
| 151 | /** |
||
| 152 | * @inheritdoc |
||
| 153 | */ |
||
| 154 | function id () { |
||
| 157 | /** |
||
| 158 | * @inheritdoc |
||
| 159 | */ |
||
| 160 | function affected () { |
||
| 163 | /** |
||
| 164 | * @inheritdoc |
||
| 165 | * |
||
| 166 | * @param false|resource $query_result |
||
| 167 | */ |
||
| 168 | function free ($query_result) { |
||
| 174 | /** |
||
| 175 | * @inheritdoc |
||
| 176 | */ |
||
| 177 | function columns ($table, $like = false) { |
||
| 199 | /** |
||
| 200 | * @inheritdoc |
||
| 201 | */ |
||
| 202 | function tables ($like = false) { |
||
| 222 | /** |
||
| 223 | * @inheritdoc |
||
| 224 | */ |
||
| 225 | protected function s_internal ($string, $single_quotes_around) { |
||
| 228 | /** |
||
| 229 | * @inheritdoc |
||
| 230 | */ |
||
| 231 | function server () { |
||
| 234 | /** |
||
| 235 | * @inheritdoc |
||
| 236 | */ |
||
| 237 | function __destruct () { |
||
| 243 | } |
||
| 244 |