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 |
||
21 | class Connection implements IConnection |
||
22 | { |
||
23 | use NotSerializable; // TODO: implement connection serialization instead of giving up |
||
24 | |||
25 | private $name; |
||
26 | private $connCtl; |
||
27 | private $typeCtl; |
||
28 | private $stmtExec; |
||
29 | private $copyCtl; |
||
30 | private $txCtl; |
||
31 | private $ipcCtl; |
||
32 | private $cacheCtl; |
||
33 | |||
34 | private $config; |
||
35 | |||
36 | |||
37 | /** |
||
38 | * @param string $name name for the connection |
||
39 | * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array |
||
40 | * of parameters for {@link ConnectionParameters::__construct()}, |
||
41 | * or a URL for {@link ConnectionParameters::fromUrl()}, |
||
42 | * or a PostgreSQL connection string (see {@link pg_connect()}) |
||
43 | */ |
||
44 | public function __construct(string $name, $params) |
||
56 | |||
57 | final public function getName(): string |
||
61 | |||
62 | final public function getConfig(): IConnConfig |
||
66 | |||
67 | |||
68 | //region Connection Control |
||
69 | |||
70 | public function getParameters(): ConnectionParameters |
||
74 | |||
75 | public function isConnected(): ?bool |
||
79 | |||
80 | public function isConnectedWait(): ?bool |
||
84 | |||
85 | public function connect(?\Closure $initProcedure = null): bool |
||
89 | |||
90 | public function connectWait(): bool |
||
94 | |||
95 | public function disconnect(): bool |
||
99 | |||
100 | public function registerConnectStartHook(\Closure $closure): void |
||
104 | |||
105 | public function registerPreDisconnectHook(\Closure $closure): void |
||
109 | |||
110 | public function registerPostDisconnectHook(\Closure $closure): void |
||
114 | |||
115 | //endregion |
||
116 | |||
117 | //region Type Control |
||
118 | |||
119 | public function getTypeRegister(): TypeRegister |
||
123 | |||
124 | public function getTypeDictionary(): ITypeDictionary |
||
128 | |||
129 | public function flushTypeDictionary(): void |
||
133 | |||
134 | public function setTypeControlOption(int $option): void |
||
138 | |||
139 | public function unsetTypeControlOption(int $option): void |
||
143 | |||
144 | //endregion |
||
145 | |||
146 | //region Statement Execution |
||
147 | |||
148 | public function query($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): IQueryResult |
||
152 | |||
153 | public function querySingleTuple($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): ITuple |
||
157 | |||
158 | public function querySingleColumn($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams): IColumn |
||
162 | |||
163 | public function querySingleValue($sqlFragmentPatternOrRelationDefinition, ...$fragmentsAndParams) |
||
167 | |||
168 | public function command($sqlFragmentPatternOrCommand, ...$fragmentsAndParams): ICommandResult |
||
172 | |||
173 | public function rawQuery(string $sqlQuery): IQueryResult |
||
177 | |||
178 | public function rawCommand(string $sqlCommand): ICommandResult |
||
182 | |||
183 | public function executeStatement($sqlStatement): IResult |
||
187 | |||
188 | public function runScript(string $sqlScript): array |
||
192 | |||
193 | public function getStatementExceptionFactory(): StatementExceptionFactory |
||
197 | |||
198 | //endregion |
||
199 | |||
200 | //region Copy Control |
||
201 | |||
202 | public function copyFromFile(string $file, string $table, ?array $columns = null, array $options = []): ICommandResult |
||
206 | |||
207 | public function copyFromProgram(string $program, string $table, ?array $columns = null, array $options = []): ICommandResult |
||
211 | |||
212 | public function copyFromInput(string $table, ?array $columns = null, array $options = []): ICopyInResult |
||
216 | |||
217 | public function copyToFile(string $file, $tableOrRelationDefinition, ?array $columns = null, array $options = []): ICommandResult |
||
221 | |||
222 | public function copyToProgram(string $program, $tableOrRelationDefinition, ?array $columns = null, array $options = []): ICommandResult |
||
226 | |||
227 | public function copyToArray(string $table, array $options = []): array |
||
231 | |||
232 | //endregion |
||
233 | |||
234 | //region Transaction Control |
||
235 | |||
236 | public function inTransaction(): bool |
||
240 | |||
241 | public function startTransaction($transactionOptions = 0): ITxHandle |
||
245 | |||
246 | public function setupSubsequentTransactions($transactionOptions): void |
||
250 | |||
251 | public function getDefaultTxConfig(): TxConfig |
||
255 | |||
256 | public function commitPreparedTransaction(string $name): void |
||
260 | |||
261 | public function rollbackPreparedTransaction(string $name): void |
||
265 | |||
266 | public function listPreparedTransactions(): IQueryResult |
||
270 | |||
271 | //endregion |
||
272 | |||
273 | //region Transaction observing |
||
274 | |||
275 | public function addTransactionControlObserver(ITransactionControlObserver $observer): void |
||
279 | |||
280 | public function removeTransactionControlObserver(ITransactionControlObserver $observer): void |
||
284 | |||
285 | public function removeAllTransactionControlObservers(): void |
||
289 | |||
290 | //endregion |
||
291 | |||
292 | //region IPC Control |
||
293 | |||
294 | public function getBackendPID(): int |
||
298 | |||
299 | public function notify(string $channel, ?string $payload = null): void |
||
303 | |||
304 | public function listen(string $channel): void |
||
308 | |||
309 | public function unlisten(string $channel): void |
||
313 | |||
314 | public function unlistenAll(): void |
||
318 | |||
319 | public function pollNotification(): ?Notification |
||
323 | |||
324 | //endregion |
||
325 | |||
326 | //region Cache Control |
||
327 | |||
328 | public function isCacheEnabled(): bool |
||
332 | |||
333 | public function cachePermanently(string $cacheKey, $object): bool |
||
337 | |||
338 | public function getCached(string $cacheKey) |
||
342 | |||
343 | public function flushCache(string $cacheKey): bool |
||
347 | |||
348 | //endregion |
||
349 | } |
||
350 |