Complex classes like Connection 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 Connection, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Connection implements IConnection |
||
| 10 | { |
||
| 11 | use NotSerializable; // TODO: implement connection serialization instead of giving up |
||
| 12 | |||
| 13 | private $name; |
||
| 14 | private $connCtl; |
||
| 15 | private $typeCtl; |
||
| 16 | private $stmtExec; |
||
| 17 | private $copyCtl; |
||
| 18 | private $txCtl; |
||
| 19 | private $ipcCtl; |
||
| 20 | |||
| 21 | private $config; |
||
| 22 | |||
| 23 | |||
| 24 | /** |
||
| 25 | * @param string $name name for the connection |
||
| 26 | * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array |
||
| 27 | * of parameters for {@link ConnectionParameters::__construct()}, |
||
| 28 | * or a URL for {@link ConnectionParameters::fromUrl()}, |
||
| 29 | * or a PostgreSQL connection string (see {@link pg_connect()}) |
||
| 30 | */ |
||
| 31 | public function __construct($name, $params) |
||
| 42 | |||
| 43 | final public function getName() |
||
| 47 | |||
| 48 | final public function getConfig() |
||
| 52 | |||
| 53 | |||
| 54 | //region Connection Control |
||
| 55 | |||
| 56 | public function getParameters() |
||
| 60 | |||
| 61 | public function isConnected() |
||
| 65 | |||
| 66 | public function isConnectedWait() |
||
| 70 | |||
| 71 | public function connect() |
||
| 75 | |||
| 76 | public function connectWait() |
||
| 80 | |||
| 81 | public function disconnect() |
||
| 85 | |||
| 86 | //endregion |
||
| 87 | |||
| 88 | //region Type Control |
||
| 89 | |||
| 90 | public function getTypeRegister() |
||
| 94 | |||
| 95 | public function getTypeDictionary() |
||
| 99 | |||
| 100 | public function flushTypeDictionary() |
||
| 104 | |||
| 105 | //endregion |
||
| 106 | |||
| 107 | //region Statement Execution |
||
| 108 | |||
| 109 | public function query($sqlFragmentPatternOrRecipe, ...$fragmentsAndPositionalParamsAndNamedParamsMap) |
||
| 113 | |||
| 114 | public function command($sqlFragmentPatternOrRecipe, ...$fragmentsAndPositionalParamsAndNamedParamsMap) |
||
| 118 | |||
| 119 | public function rawQuery(string $sqlStatement) |
||
| 123 | |||
| 124 | public function rawMultiQuery($sqlStatements) |
||
| 128 | |||
| 129 | public function runScript($sqlScript) |
||
| 133 | |||
| 134 | public function getStatementExceptionFactory() |
||
| 138 | |||
| 139 | //endregion |
||
| 140 | |||
| 141 | //region Copy Control |
||
| 142 | |||
| 143 | public function copyFromFile($file, $table, $columns = null, $options = []) |
||
| 147 | |||
| 148 | public function copyFromProgram($program, $table, $columns = null, $options = []) |
||
| 152 | |||
| 153 | public function copyFromInput($table, $columns = null, $options = []) |
||
| 157 | |||
| 158 | public function copyToFile($file, $tableOrQuery, $columns = null, $options = []) |
||
| 162 | |||
| 163 | public function copyToProgram($program, $tableOrQuery, $columns = null, $options = []) |
||
| 167 | |||
| 168 | public function copyToArray($table, $options = []) |
||
| 172 | |||
| 173 | //endregion |
||
| 174 | |||
| 175 | //region Transaction Control |
||
| 176 | |||
| 177 | public function inTransaction() |
||
| 181 | |||
| 182 | public function startTransaction($transactionOptions = 0) |
||
| 186 | |||
| 187 | public function setupTransaction($transactionOptions) |
||
| 191 | |||
| 192 | public function setupSubsequentTransactions($transactionOptions) |
||
| 196 | |||
| 197 | public function commit() |
||
| 201 | |||
| 202 | public function rollback() |
||
| 206 | |||
| 207 | public function savepoint($name) |
||
| 211 | |||
| 212 | public function rollbackToSavepoint($name) |
||
| 216 | |||
| 217 | public function releaseSavepoint($name) |
||
| 221 | |||
| 222 | public function setTransactionSnapshot($snapshotId) |
||
| 226 | |||
| 227 | public function exportTransactionSnapshot() |
||
| 231 | |||
| 232 | public function prepareTransaction($name) |
||
| 236 | |||
| 237 | public function commitPreparedTransaction($name) |
||
| 241 | |||
| 242 | public function rollbackPreparedTransaction($name) |
||
| 246 | |||
| 247 | public function listPreparedTransactions() |
||
| 251 | |||
| 252 | //endregion |
||
| 253 | |||
| 254 | //region IPC Control |
||
| 255 | |||
| 256 | public function getBackendPID() |
||
| 260 | |||
| 261 | public function notify($channel, $payload = null) |
||
| 265 | |||
| 266 | public function listen($channel) |
||
| 270 | |||
| 271 | public function unlisten($channel) |
||
| 275 | |||
| 276 | public function unlistenAll() |
||
| 280 | |||
| 281 | public function pollNotification() |
||
| 285 | |||
| 286 | //endregion |
||
| 287 | } |
||
| 288 |