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 |
||
5 | class Connection { |
||
6 | |||
7 | /** |
||
8 | * Connection options |
||
9 | * @var array |
||
10 | */ |
||
11 | protected $_options = [ |
||
12 | 'CQL_VERSION' => '3.0.0' |
||
13 | ]; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | protected $_keyspace; |
||
19 | |||
20 | /** |
||
21 | * @var array|\Traversable |
||
22 | */ |
||
23 | protected $_nodes; |
||
24 | |||
25 | /** |
||
26 | * @var Connection\Socket|Connection\Stream |
||
27 | */ |
||
28 | protected $_node; |
||
29 | |||
30 | /** |
||
31 | * @var int |
||
32 | */ |
||
33 | protected $_lastStreamId = 0; |
||
34 | |||
35 | /** |
||
36 | * |
||
37 | * @var array |
||
38 | */ |
||
39 | protected $_statements = []; |
||
40 | |||
41 | /** |
||
42 | * |
||
43 | * @var \SplQueue |
||
44 | */ |
||
45 | protected $_recycledStreams; |
||
46 | |||
47 | /** |
||
48 | * @var int |
||
49 | */ |
||
50 | protected $_consistency = Request\Request::CONSISTENCY_ONE; |
||
51 | |||
52 | /** |
||
53 | * @param array|\Traversable $nodes |
||
54 | * @param string $keyspace |
||
55 | * @param array $options |
||
56 | */ |
||
57 | public function __construct($nodes, $keyspace = '', array $options = []) { |
||
66 | |||
67 | /** |
||
68 | * @throws Exception |
||
69 | */ |
||
70 | protected function _connect() { |
||
101 | |||
102 | /** |
||
103 | * @return bool |
||
104 | */ |
||
105 | public function disconnect() { |
||
111 | |||
112 | /** |
||
113 | * @return bool |
||
114 | */ |
||
115 | public function isConnected() { |
||
118 | |||
119 | /** |
||
120 | * |
||
121 | * @param Response\Event $response |
||
122 | */ |
||
123 | public function trigger($response){ |
||
125 | |||
126 | /** |
||
127 | * |
||
128 | * @param int $streamId |
||
129 | * @throws Response\Exception |
||
130 | * @return Response\Response |
||
131 | */ |
||
132 | public function getResponse($streamId = 0){ |
||
140 | |||
141 | /** |
||
142 | * |
||
143 | * @throws Response\Exception |
||
144 | * @return Response\Response |
||
145 | */ |
||
146 | protected function _getResponse() { |
||
185 | |||
186 | /** |
||
187 | * Wait until all statements received response. |
||
188 | */ |
||
189 | public function flush(){ |
||
194 | |||
195 | /** |
||
196 | * @return Connection\Node |
||
197 | */ |
||
198 | public function getNode() { |
||
201 | |||
202 | /** |
||
203 | * Connect to database |
||
204 | * @throws Exception |
||
205 | * @return bool |
||
206 | */ |
||
207 | public function connect() { |
||
229 | |||
230 | /** |
||
231 | * @param Request\Request $request |
||
232 | * @throws Exception |
||
233 | * @return Response\Response |
||
234 | */ |
||
235 | public function syncRequest(Request\Request $request) { |
||
248 | |||
249 | /** |
||
250 | * |
||
251 | * @param Request\Request $request |
||
252 | * @return Statement |
||
253 | */ |
||
254 | public function asyncRequest(Request\Request $request) { |
||
265 | |||
266 | /** |
||
267 | * |
||
268 | * @throws Exception |
||
269 | * @return int |
||
270 | */ |
||
271 | protected function _getNewStreamId(){ |
||
281 | |||
282 | /***** Shorthand Methods ******/ |
||
283 | /** |
||
284 | * |
||
285 | * @param string $cql |
||
286 | * @throws Exception |
||
287 | * @return array |
||
288 | */ |
||
289 | public function prepare($cql) { |
||
294 | |||
295 | /** |
||
296 | * |
||
297 | * @param string $queryId |
||
298 | * @param array $values |
||
299 | * @param int $consistency |
||
300 | * @param array $options |
||
301 | * @throws Exception |
||
302 | * @return Response\Response |
||
303 | */ |
||
304 | public function executeSync($queryId, array $values = [], $consistency = null, array $options = []){ |
||
309 | |||
310 | /** |
||
311 | * |
||
312 | * @param string $queryId |
||
313 | * @param array $values |
||
314 | * @param int $consistency |
||
315 | * @param array $options |
||
316 | * @throws Exception |
||
317 | * @return Statement |
||
318 | */ |
||
319 | public function executeAsync($queryId, array $values = [], $consistency = null, array $options = []){ |
||
324 | |||
325 | /** |
||
326 | * |
||
327 | * @param string $cql |
||
328 | * @param array $values |
||
329 | * @param int $consistency |
||
330 | * @param array $options |
||
331 | * @throws Exception |
||
332 | * @return Response\Response |
||
333 | */ |
||
334 | public function querySync($cql, array $values = [], $consistency = null, array $options = []){ |
||
339 | |||
340 | /** |
||
341 | * |
||
342 | * @param string $cql |
||
343 | * @param array $values |
||
344 | * @param int $consistency |
||
345 | * @param array $options |
||
346 | * @throws Exception |
||
347 | * @return Statement |
||
348 | */ |
||
349 | public function queryAsync($cql, array $values = [], $consistency = null, array $options = []){ |
||
354 | |||
355 | /** |
||
356 | * @param string $keyspace |
||
357 | * @throws Exception |
||
358 | * @return Response\Result |
||
359 | */ |
||
360 | public function setKeyspace($keyspace) { |
||
368 | |||
369 | /** |
||
370 | * @param int $consistency |
||
371 | */ |
||
372 | public function setConsistency($consistency){ |
||
375 | } |
||
376 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.