Complex classes like CouchbaseResourceManager 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 CouchbaseResourceManager, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class CouchbaseResourceManager |
||
|
|||
29 | { |
||
30 | /** |
||
31 | * Registered resources. |
||
32 | * |
||
33 | * @var array |
||
34 | */ |
||
35 | protected $resources = []; |
||
36 | |||
37 | /** |
||
38 | * Get servers. |
||
39 | * |
||
40 | * @param string $id |
||
41 | * |
||
42 | * @throws Exception\RuntimeException |
||
43 | * |
||
44 | * @return array array('host' => <host>, 'port' => <port>) |
||
45 | */ |
||
46 | public function getServer($id) |
||
58 | |||
59 | /** |
||
60 | * Normalize one server into the following format: |
||
61 | * array('host' => <host>, 'port' => <port>, 'weight' => <weight>). |
||
62 | * |
||
63 | * @param string|array &$server |
||
64 | * |
||
65 | * @throws Exception\InvalidArgumentException |
||
66 | * @throws \Zend\Stdlib\Exception\InvalidArgumentException |
||
67 | */ |
||
68 | 62 | protected function normalizeServer(&$server) |
|
69 | { |
||
70 | 62 | $host = null; |
|
71 | 62 | $port = 8091; |
|
72 | // convert a single server into an array |
||
73 | 62 | if ($server instanceof \Traversable) { |
|
74 | $server = ArrayUtils::iteratorToArray($server); |
||
75 | } |
||
76 | 62 | if (is_array($server)) { |
|
77 | // array(<host>[, <port>]) |
||
78 | 62 | if (isset($server[0])) { |
|
79 | 62 | $host = (string) $server[0]; |
|
80 | 62 | $port = isset($server[1]) ? (int) $server[1] : $port; |
|
81 | 62 | } |
|
82 | // array('host' => <host>[, 'port' => <port>]) |
||
83 | 62 | if (!isset($server[0]) && isset($server['host'])) { |
|
84 | $host = (string) $server['host']; |
||
85 | $port = isset($server['port']) ? (int) $server['port'] : $port; |
||
86 | } |
||
87 | 62 | } else { |
|
88 | // parse server from URI host{:?port} |
||
89 | 1 | $server = trim($server); |
|
90 | 1 | if (strpos($server, '://') === false) { |
|
91 | 1 | $server = 'http://'.$server; |
|
92 | 1 | } |
|
93 | 1 | $server = parse_url($server); |
|
94 | 1 | if (!$server) { |
|
95 | 1 | throw new Exception\InvalidArgumentException('Invalid server given'); |
|
96 | } |
||
97 | $host = $server['host']; |
||
98 | $port = isset($server['port']) ? (int) $server['port'] : $port; |
||
99 | if (isset($server['query'])) { |
||
100 | $query = null; |
||
101 | parse_str($server['query'], $query); |
||
102 | } |
||
103 | } |
||
104 | 62 | if (!$host) { |
|
105 | throw new Exception\InvalidArgumentException('Missing required server host'); |
||
106 | } |
||
107 | $server = [ |
||
108 | 62 | 'host' => $host, |
|
109 | 62 | 'port' => $port, |
|
110 | 62 | ]; |
|
111 | 62 | } |
|
112 | |||
113 | /** |
||
114 | * Check if a resource exists. |
||
115 | * |
||
116 | * @param string $id |
||
117 | * |
||
118 | * @return bool |
||
119 | */ |
||
120 | 62 | public function hasResource($id) |
|
124 | |||
125 | /** |
||
126 | * Gets a couchbase cluster resource. |
||
127 | * |
||
128 | * @param string $id |
||
129 | * |
||
130 | * @return \CouchbaseBucket |
||
131 | * |
||
132 | * @throws Exception\RuntimeException |
||
133 | */ |
||
134 | 62 | public function getResource($id) |
|
157 | |||
158 | /** |
||
159 | * Set a resource. |
||
160 | * |
||
161 | * @param string $id |
||
162 | * @param array|\Traversable|CouchbaseBucketResource $resource |
||
163 | * |
||
164 | * @return CouchbaseResourceManager Fluent interface |
||
165 | * |
||
166 | * @throws \Zend\Stdlib\Exception\InvalidArgumentException |
||
167 | * @throws Exception\InvalidArgumentException |
||
168 | */ |
||
169 | 62 | public function setResource($id, $resource) |
|
195 | |||
196 | /** |
||
197 | * Normalize libmemcached options. |
||
198 | * |
||
199 | * @param array|\Traversable $libOptions |
||
200 | * |
||
201 | * @throws Exception\InvalidArgumentException |
||
202 | */ |
||
203 | 62 | protected function normalizeLibOptions(&$libOptions) |
|
219 | |||
220 | /** |
||
221 | * Convert option name into it's constant value. |
||
222 | * |
||
223 | * @param string|int $key |
||
224 | * |
||
225 | * @throws Exception\InvalidArgumentException |
||
226 | */ |
||
227 | protected function normalizeLibOptionKey(&$key) |
||
240 | |||
241 | /** |
||
242 | * Set Libmemcached options. |
||
243 | * |
||
244 | * @param string $id |
||
245 | * @param array $libOptions |
||
246 | * |
||
247 | * @return CouchbaseResourceManager Fluent interface |
||
248 | */ |
||
249 | 1 | public function setLibOptions($id, array $libOptions) |
|
250 | { |
||
251 | 1 | if (!$this->hasResource($id)) { |
|
252 | 1 | return $this->setResource($id, [ |
|
253 | 1 | 'lib_options' => $libOptions, |
|
254 | 1 | ]); |
|
255 | } |
||
256 | |||
257 | $this->normalizeLibOptions($libOptions); |
||
258 | |||
259 | $resource = &$this->resources[$id]; |
||
260 | $resource['lib_options'] = $libOptions; |
||
261 | |||
262 | return $this; |
||
263 | } |
||
264 | |||
265 | /** |
||
266 | * Get Libmemcached options. |
||
267 | * |
||
268 | * @param string $id |
||
269 | * |
||
270 | * @return array |
||
271 | * |
||
272 | * @throws Exception\RuntimeException |
||
273 | */ |
||
274 | public function getLibOptions($id) |
||
297 | |||
298 | /** |
||
299 | * Remove a resource. |
||
300 | * |
||
301 | * @param string $id |
||
302 | * |
||
303 | * @return CouchbaseResourceManager Fluent interface |
||
304 | */ |
||
305 | public function removeResource($id) |
||
311 | |||
312 | /** |
||
313 | * Set servers. |
||
314 | * |
||
315 | * $servers can be an array list or a comma separated list of servers. |
||
316 | * One server in the list can be descripted as follows: |
||
317 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
318 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
319 | * - List: array(<host>[, <port>][, <weight>]) |
||
320 | * |
||
321 | * @param string $id |
||
322 | * @param string $server |
||
323 | * |
||
324 | * @return CouchbaseResourceManager |
||
325 | */ |
||
326 | 62 | public function setServer($id, $server) |
|
339 | |||
340 | /** |
||
341 | * Add servers. |
||
342 | * |
||
343 | * @param string $id |
||
344 | * @param string|array $servers |
||
345 | * |
||
346 | * @return CouchbaseResourceManager |
||
347 | */ |
||
348 | public function addServers($id, $servers) |
||
373 | |||
374 | /** |
||
375 | * Add one server. |
||
376 | * |
||
377 | * @param string $id |
||
378 | * @param string|array $server |
||
379 | * |
||
380 | * @return CouchbaseResourceManager |
||
381 | */ |
||
382 | public function addServer($id, $server) |
||
386 | |||
387 | /** |
||
388 | * Set servers. |
||
389 | * |
||
390 | * $servers can be an array list or a comma separated list of servers. |
||
391 | * One server in the list can be descripted as follows: |
||
392 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
393 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
394 | * - List: array(<host>[, <port>][, <weight>]) |
||
395 | * |
||
396 | * @param string $id |
||
397 | * @param string $username |
||
398 | * |
||
399 | * @return CouchbaseResourceManager |
||
400 | */ |
||
401 | public function setUsername($id, $username) |
||
414 | |||
415 | public function getUsername($id) |
||
425 | |||
426 | /** |
||
427 | * Set servers. |
||
428 | * |
||
429 | * $servers can be an array list or a comma separated list of servers. |
||
430 | * One server in the list can be descripted as follows: |
||
431 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
432 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
433 | * - List: array(<host>[, <port>][, <weight>]) |
||
434 | * |
||
435 | * @param string $id |
||
436 | * @param string $bucket |
||
437 | * |
||
438 | * @return CouchbaseResourceManager |
||
439 | */ |
||
440 | 62 | public function setBucket($id, $bucket) |
|
453 | |||
454 | public function getBucket($id) |
||
464 | |||
465 | /** |
||
466 | * Set servers. |
||
467 | * |
||
468 | * $servers can be an array list or a comma separated list of servers. |
||
469 | * One server in the list can be descripted as follows: |
||
470 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
471 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
472 | * - List: array(<host>[, <port>][, <weight>]) |
||
473 | * |
||
474 | * @param string $id |
||
475 | * @param string $password |
||
476 | * |
||
477 | * @return CouchbaseResourceManager |
||
478 | */ |
||
479 | public function setPassword($id, $password) |
||
492 | |||
493 | public function getPassword($id) |
||
503 | |||
504 | /** |
||
505 | * Normalize a list of servers into the following format: |
||
506 | * array(array('host' => <host>, 'port' => <port>, 'weight' => <weight>)[, ...]). |
||
507 | * |
||
508 | * @param string|array $servers |
||
509 | * |
||
510 | * @throws \Zend\Stdlib\Exception\InvalidArgumentException |
||
511 | */ |
||
512 | 62 | protected function normalizeServers(&$servers) |
|
513 | { |
||
514 | 62 | if (!is_array($servers) && !$servers instanceof \Traversable) { |
|
515 | // Convert string into a list of servers |
||
516 | 1 | $servers = explode(',', $servers); |
|
517 | 1 | } |
|
518 | 62 | $result = []; |
|
519 | 62 | foreach ($servers as $server) { |
|
520 | 62 | $this->normalizeServer($server); |
|
521 | 62 | $result[$server['host'].':'.$server['port']] = $server; |
|
522 | 62 | } |
|
523 | 62 | $servers = array_values($result); |
|
524 | 62 | } |
|
525 | |||
526 | /** |
||
527 | * Compare 2 normalized server arrays |
||
528 | * (Compares only the host and the port). |
||
529 | * |
||
530 | * @param array $serverA |
||
531 | * @param array $serverB |
||
532 | * |
||
533 | * @return int |
||
534 | */ |
||
535 | protected function compareServers(array $serverA, array $serverB) |
||
545 | } |
||
546 |
The class complexity is the sum of the complexity of all methods. A very high value is usually an indication that your class does not follow the single reponsibility principle and does more than one job.
Some resources for further reading:
You can also find more detailed suggestions for refactoring in the “Code” section of your repository.