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', $charset = 'UTF8', $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 | 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 | * @deprecated |
||
| 132 | * @todo remove after 4.x release |
||
| 133 | * |
||
| 134 | * @inheritdoc |
||
| 135 | */ |
||
| 136 | function n ($query_result) { |
||
| 143 | /** |
||
| 144 | * @inheritdoc |
||
| 145 | * |
||
| 146 | * @param false|resource $query_result |
||
| 147 | */ |
||
| 148 | function f ($query_result, $single_column = false, $array = false, $indexed = false) { |
||
| 164 | /** |
||
| 165 | * @inheritdoc |
||
| 166 | */ |
||
| 167 | function id () { |
||
| 170 | /** |
||
| 171 | * @inheritdoc |
||
| 172 | */ |
||
| 173 | function affected () { |
||
| 176 | /** |
||
| 177 | * @inheritdoc |
||
| 178 | * |
||
| 179 | * @param false|resource $query_result |
||
| 180 | */ |
||
| 181 | function free ($query_result) { |
||
| 187 | /** |
||
| 188 | * @inheritdoc |
||
| 189 | */ |
||
| 190 | function columns ($table, $like = false) { |
||
| 212 | /** |
||
| 213 | * @inheritdoc |
||
| 214 | */ |
||
| 215 | function tables ($like = false) { |
||
| 235 | /** |
||
| 236 | * @inheritdoc |
||
| 237 | */ |
||
| 238 | protected function s_internal ($string, $single_quotes_around) { |
||
| 241 | /** |
||
| 242 | * @inheritdoc |
||
| 243 | */ |
||
| 244 | function server () { |
||
| 247 | /** |
||
| 248 | * @inheritdoc |
||
| 249 | */ |
||
| 250 | function __destruct () { |
||
| 256 | } |
||
| 257 |
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: