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 |
||
| 14 | class Connection implements IConnection |
||
| 15 | { |
||
| 16 | use NotSerializable; // TODO: implement connection serialization instead of giving up |
||
| 17 | |||
| 18 | private $name; |
||
| 19 | private $connCtl; |
||
| 20 | private $typeCtl; |
||
| 21 | private $stmtExec; |
||
| 22 | private $copyCtl; |
||
| 23 | private $txCtl; |
||
| 24 | private $ipcCtl; |
||
| 25 | |||
| 26 | private $config; |
||
| 27 | |||
| 28 | |||
| 29 | /** |
||
| 30 | * @param string $name name for the connection |
||
| 31 | * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array |
||
| 32 | * of parameters for {@link ConnectionParameters::__construct()}, |
||
| 33 | * or a URL for {@link ConnectionParameters::fromUrl()}, |
||
| 34 | * or a PostgreSQL connection string (see {@link pg_connect()}) |
||
| 35 | */ |
||
| 36 | public function __construct(string $name, $params) |
||
| 47 | |||
| 48 | final public function getName(): string |
||
| 52 | |||
| 53 | final public function getConfig(): IConnConfig |
||
| 57 | |||
| 58 | |||
| 59 | //region Connection Control |
||
| 60 | |||
| 61 | public function getParameters(): ConnectionParameters |
||
| 65 | |||
| 66 | public function isConnected() |
||
| 70 | |||
| 71 | public function isConnectedWait() |
||
| 75 | |||
| 76 | public function connect(): bool |
||
| 80 | |||
| 81 | public function connectWait(): bool |
||
| 85 | |||
| 86 | public function disconnect(): bool |
||
| 90 | |||
| 91 | //endregion |
||
| 92 | |||
| 93 | //region Type Control |
||
| 94 | |||
| 95 | public function getTypeRegister(): TypeRegister |
||
| 99 | |||
| 100 | public function getTypeDictionary(): ITypeDictionary |
||
| 104 | |||
| 105 | public function flushTypeDictionary() |
||
| 109 | |||
| 110 | //endregion |
||
| 111 | |||
| 112 | //region Statement Execution |
||
| 113 | |||
| 114 | public function query($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): IQueryResult |
||
| 118 | |||
| 119 | public function querySingleTuple($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): ITuple |
||
| 123 | |||
| 124 | public function querySingleValue($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams) |
||
| 128 | |||
| 129 | public function command($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): ICommandResult |
||
| 133 | |||
| 134 | public function rawQuery(string $sqlQuery): IQueryResult |
||
| 138 | |||
| 139 | public function rawCommand(string $sqlCommand): ICommandResult |
||
| 143 | |||
| 144 | public function rawMultiStatement($sqlStatements) |
||
| 148 | |||
| 149 | public function runScript(string $sqlScript) |
||
| 153 | |||
| 154 | public function getStatementExceptionFactory(): StatementExceptionFactory |
||
| 158 | |||
| 159 | //endregion |
||
| 160 | |||
| 161 | //region Copy Control |
||
| 162 | |||
| 163 | public function copyFromFile(string $file, string $table, $columns = null, $options = []): ICommandResult |
||
| 167 | |||
| 168 | public function copyFromProgram(string $program, string $table, $columns = null, $options = []): ICommandResult |
||
| 172 | |||
| 173 | public function copyFromInput(string $table, $columns = null, $options = []): ICopyInResult |
||
| 177 | |||
| 178 | public function copyToFile(string $file, $tableOrRecipe, $columns = null, $options = []): ICommandResult |
||
| 182 | |||
| 183 | public function copyToProgram(string $program, $tableOrRecipe, $columns = null, $options = []): ICommandResult |
||
| 187 | |||
| 188 | public function copyToArray(string $table, $options = []) |
||
| 192 | |||
| 193 | //endregion |
||
| 194 | |||
| 195 | //region Transaction Control |
||
| 196 | |||
| 197 | public function inTransaction(): bool |
||
| 201 | |||
| 202 | public function startTransaction($transactionOptions = 0): bool |
||
| 206 | |||
| 207 | public function setupTransaction($transactionOptions) |
||
| 211 | |||
| 212 | public function setupSubsequentTransactions($transactionOptions) |
||
| 216 | |||
| 217 | public function commit(): bool |
||
| 221 | |||
| 222 | public function rollback(): bool |
||
| 226 | |||
| 227 | public function savepoint(string $name) |
||
| 231 | |||
| 232 | public function rollbackToSavepoint(string $name) |
||
| 236 | |||
| 237 | public function releaseSavepoint(string $name) |
||
| 241 | |||
| 242 | public function setTransactionSnapshot(string $snapshotId): bool |
||
| 246 | |||
| 247 | public function exportTransactionSnapshot() |
||
| 251 | |||
| 252 | public function prepareTransaction(string $name): bool |
||
| 256 | |||
| 257 | public function commitPreparedTransaction(string $name) |
||
| 261 | |||
| 262 | public function rollbackPreparedTransaction(string $name) |
||
| 266 | |||
| 267 | public function listPreparedTransactions(): IQueryResult |
||
| 271 | |||
| 272 | //endregion |
||
| 273 | |||
| 274 | //region IPC Control |
||
| 275 | |||
| 276 | public function getBackendPID(): int |
||
| 280 | |||
| 281 | public function notify(string $channel, string $payload = null) |
||
| 285 | |||
| 286 | public function listen(string $channel) |
||
| 290 | |||
| 291 | public function unlisten(string $channel) |
||
| 295 | |||
| 296 | public function unlistenAll() |
||
| 300 | |||
| 301 | public function pollNotification() |
||
| 305 | |||
| 306 | //endregion |
||
| 307 | } |
||
| 308 |