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 |
||
17 | class Connection implements IConnection |
||
18 | { |
||
19 | use NotSerializable; // TODO: implement connection serialization instead of giving up |
||
20 | |||
21 | private $name; |
||
22 | private $connCtl; |
||
23 | private $typeCtl; |
||
24 | private $stmtExec; |
||
25 | private $copyCtl; |
||
26 | private $txCtl; |
||
27 | private $ipcCtl; |
||
28 | private $cacheCtl; |
||
29 | |||
30 | private $config; |
||
31 | |||
32 | |||
33 | /** |
||
34 | * @param string $name name for the connection |
||
35 | * @param ConnectionParameters|array|string $params either a connection parameters object, or an associative array |
||
36 | * of parameters for {@link ConnectionParameters::__construct()}, |
||
37 | * or a URL for {@link ConnectionParameters::fromUrl()}, |
||
38 | * or a PostgreSQL connection string (see {@link pg_connect()}) |
||
39 | */ |
||
40 | public function __construct(string $name, $params) |
||
52 | |||
53 | final public function getName(): string |
||
57 | |||
58 | final public function getConfig(): IConnConfig |
||
62 | |||
63 | |||
64 | //region Connection Control |
||
65 | |||
66 | public function getParameters(): ConnectionParameters |
||
70 | |||
71 | public function isConnected() |
||
75 | |||
76 | public function isConnectedWait() |
||
80 | |||
81 | public function connect(\Closure $initProcedure = null): bool |
||
85 | |||
86 | public function connectWait(): bool |
||
90 | |||
91 | public function disconnect(): bool |
||
95 | |||
96 | public function registerConnectStartHook(\Closure $closure) |
||
97 | { |
||
98 | $this->connCtl->registerConnectStartHook($closure); |
||
99 | } |
||
100 | |||
101 | public function registerPreDisconnectHook(\Closure $closure) |
||
102 | { |
||
103 | $this->connCtl->registerPreDisconnectHook($closure); |
||
104 | } |
||
105 | |||
106 | public function registerPostDisconnectHook(\Closure $closure) |
||
107 | { |
||
108 | $this->connCtl->registerPostDisconnectHook($closure); |
||
109 | } |
||
110 | |||
111 | //endregion |
||
112 | |||
113 | //region Type Control |
||
114 | |||
115 | public function getTypeRegister(): TypeRegister |
||
116 | { |
||
117 | return $this->typeCtl->getTypeRegister(); |
||
118 | } |
||
119 | |||
120 | public function getTypeDictionary(): ITypeDictionary |
||
121 | { |
||
122 | return $this->typeCtl->getTypeDictionary(); |
||
123 | } |
||
124 | |||
125 | public function flushTypeDictionary() |
||
126 | { |
||
127 | $this->typeCtl->flushTypeDictionary(); |
||
128 | } |
||
129 | |||
130 | //endregion |
||
131 | |||
132 | //region Statement Execution |
||
133 | |||
134 | public function query($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): IQueryResult |
||
135 | { |
||
136 | return $this->stmtExec->query($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams); |
||
137 | } |
||
138 | |||
139 | public function querySingleTuple($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): ITuple |
||
140 | { |
||
141 | return $this->stmtExec->querySingleTuple($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams); |
||
142 | } |
||
143 | |||
144 | public function querySingleValue($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams) |
||
145 | { |
||
146 | return $this->stmtExec->querySingleValue($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams); |
||
147 | } |
||
148 | |||
149 | public function command($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams): ICommandResult |
||
150 | { |
||
151 | return $this->stmtExec->command($sqlFragmentPatternOrRecipe, ...$fragmentsAndParams); |
||
152 | } |
||
153 | |||
154 | public function rawQuery(string $sqlQuery): IQueryResult |
||
155 | { |
||
156 | return $this->stmtExec->rawQuery($sqlQuery); |
||
157 | } |
||
158 | |||
159 | public function rawCommand(string $sqlCommand): ICommandResult |
||
160 | { |
||
161 | return $this->stmtExec->rawCommand($sqlCommand); |
||
162 | } |
||
163 | |||
164 | public function rawMultiStatement($sqlStatements) |
||
165 | { |
||
166 | return $this->stmtExec->rawMultiStatement($sqlStatements); |
||
167 | } |
||
168 | |||
169 | public function runScript(string $sqlScript) |
||
170 | { |
||
171 | return $this->stmtExec->runScript($sqlScript); |
||
172 | } |
||
173 | |||
174 | public function getStatementExceptionFactory(): StatementExceptionFactory |
||
175 | { |
||
176 | return $this->stmtExec->getStatementExceptionFactory(); |
||
177 | } |
||
178 | |||
179 | //endregion |
||
180 | |||
181 | //region Copy Control |
||
182 | |||
183 | public function copyFromFile(string $file, string $table, $columns = null, $options = []): ICommandResult |
||
184 | { |
||
185 | return $this->copyCtl->copyFromFile($file, $table, $columns, $options); |
||
186 | } |
||
187 | |||
188 | public function copyFromProgram(string $program, string $table, $columns = null, $options = []): ICommandResult |
||
189 | { |
||
190 | return $this->copyCtl->copyFromProgram($program, $table, $columns, $options); |
||
191 | } |
||
192 | |||
193 | public function copyFromInput(string $table, $columns = null, $options = []): ICopyInResult |
||
194 | { |
||
195 | return $this->copyCtl->copyFromInput($table, $columns, $options); |
||
196 | } |
||
197 | |||
198 | public function copyToFile(string $file, $tableOrRecipe, $columns = null, $options = []): ICommandResult |
||
199 | { |
||
200 | return $this->copyCtl->copyToFile($file, $tableOrRecipe, $columns, $options); |
||
201 | } |
||
202 | |||
203 | public function copyToProgram(string $program, $tableOrRecipe, $columns = null, $options = []): ICommandResult |
||
204 | { |
||
205 | return $this->copyCtl->copyToProgram($program, $tableOrRecipe, $columns, $options); |
||
206 | } |
||
207 | |||
208 | public function copyToArray(string $table, $options = []) |
||
209 | { |
||
210 | return $this->copyCtl->copyToArray($table, $options); |
||
211 | } |
||
212 | |||
213 | //endregion |
||
214 | |||
215 | //region Transaction Control |
||
216 | |||
217 | public function inTransaction(): bool |
||
218 | { |
||
219 | return $this->txCtl->inTransaction(); |
||
220 | } |
||
221 | |||
222 | public function startTransaction($transactionOptions = 0): ITxHandle |
||
223 | { |
||
224 | return $this->txCtl->startTransaction($transactionOptions); |
||
225 | } |
||
226 | |||
227 | public function setupSubsequentTransactions($transactionOptions) |
||
231 | |||
232 | public function commitPreparedTransaction(string $name) |
||
233 | { |
||
234 | $this->txCtl->commitPreparedTransaction($name); |
||
235 | } |
||
236 | |||
237 | public function rollbackPreparedTransaction(string $name) |
||
238 | { |
||
239 | $this->txCtl->rollbackPreparedTransaction($name); |
||
240 | } |
||
241 | |||
242 | public function listPreparedTransactions(): IQueryResult |
||
243 | { |
||
244 | return $this->txCtl->listPreparedTransactions(); |
||
245 | } |
||
246 | |||
247 | //endregion |
||
248 | |||
249 | //region Transaction observing |
||
250 | |||
251 | public function addTransactionControlObserver(ITransactionControlObserver $observer) |
||
252 | { |
||
253 | $this->txCtl->addObserver($observer); |
||
254 | } |
||
255 | |||
256 | public function removeTransactionControlObserver(ITransactionControlObserver $observer) |
||
257 | { |
||
258 | $this->txCtl->removeObserver($observer); |
||
260 | |||
261 | public function removeAllTransactionControlObservers() |
||
265 | |||
266 | //endregion |
||
267 | |||
268 | //region IPC Control |
||
269 | |||
270 | public function getBackendPID(): int |
||
274 | |||
275 | public function notify(string $channel, string $payload = null) |
||
279 | |||
280 | public function listen(string $channel) |
||
284 | |||
285 | public function unlisten(string $channel) |
||
289 | |||
290 | public function unlistenAll() |
||
294 | |||
295 | public function pollNotification() |
||
299 | |||
300 | //endregion |
||
301 | |||
302 | //region Cache Control |
||
303 | |||
304 | public function isCacheEnabled(): bool |
||
308 | |||
309 | public function cachePermanently(string $cacheKey, $object): bool |
||
313 | |||
314 | public function getCached(string $cacheKey) |
||
318 | |||
319 | public function flushCache(string $cacheKey): bool |
||
323 | |||
324 | //endregion |
||
325 | } |
||
326 |