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 | 61 | protected function normalizeServer(&$server) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Check if a resource exists. |
||
| 115 | * |
||
| 116 | * @param string $id |
||
| 117 | * |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | 61 | 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 | 61 | 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 | 61 | public function setResource($id, $resource) |
|
| 195 | |||
| 196 | /** |
||
| 197 | * Normalize libmemcached options. |
||
| 198 | * |
||
| 199 | * @param array|\Traversable $libOptions |
||
| 200 | * |
||
| 201 | * @throws Exception\InvalidArgumentException |
||
| 202 | */ |
||
| 203 | 61 | 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 | public function setLibOptions($id, array $libOptions) |
||
| 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 | 61 | 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 | 61 | 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 | 61 | protected function normalizeServers(&$servers) |
|
| 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.