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) |
|
| 156 | |||
| 157 | /** |
||
| 158 | * Set a resource. |
||
| 159 | * |
||
| 160 | * @param string $id |
||
| 161 | * @param array|\Traversable|CouchbaseBucketResource $resource |
||
| 162 | * |
||
| 163 | * @return CouchbaseResourceManager Fluent interface |
||
| 164 | * |
||
| 165 | * @throws \Zend\Stdlib\Exception\InvalidArgumentException |
||
| 166 | * @throws Exception\InvalidArgumentException |
||
| 167 | */ |
||
| 168 | 61 | public function setResource($id, $resource) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Remove a resource. |
||
| 197 | * |
||
| 198 | * @param string $id |
||
| 199 | * |
||
| 200 | * @return CouchbaseResourceManager Fluent interface |
||
| 201 | */ |
||
| 202 | public function removeResource($id) |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Set servers. |
||
| 211 | * |
||
| 212 | * $servers can be an array list or a comma separated list of servers. |
||
| 213 | * One server in the list can be descripted as follows: |
||
| 214 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
| 215 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
| 216 | * - List: array(<host>[, <port>][, <weight>]) |
||
| 217 | * |
||
| 218 | * @param string $id |
||
| 219 | * @param string $server |
||
| 220 | * |
||
| 221 | * @return CouchbaseResourceManager |
||
| 222 | */ |
||
| 223 | 61 | public function setServer($id, $server) |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Add servers. |
||
| 239 | * |
||
| 240 | * @param string $id |
||
| 241 | * @param string|array $servers |
||
| 242 | * |
||
| 243 | * @return CouchbaseResourceManager |
||
| 244 | */ |
||
| 245 | public function addServers($id, $servers) |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Add one server. |
||
| 273 | * |
||
| 274 | * @param string $id |
||
| 275 | * @param string|array $server |
||
| 276 | * |
||
| 277 | * @return CouchbaseResourceManager |
||
| 278 | */ |
||
| 279 | public function addServer($id, $server) |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Set servers. |
||
| 286 | * |
||
| 287 | * $servers can be an array list or a comma separated list of servers. |
||
| 288 | * One server in the list can be descripted as follows: |
||
| 289 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
| 290 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
| 291 | * - List: array(<host>[, <port>][, <weight>]) |
||
| 292 | * |
||
| 293 | * @param string $id |
||
| 294 | * @param string $username |
||
| 295 | * |
||
| 296 | * @return CouchbaseResourceManager |
||
| 297 | */ |
||
| 298 | public function setUsername($id, $username) |
||
| 311 | |||
| 312 | public function getUsername($id) |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Set encoder. |
||
| 325 | * |
||
| 326 | * @param string $id |
||
| 327 | * @param string $encoder |
||
| 328 | * |
||
| 329 | * @return CouchbaseResourceManager |
||
| 330 | */ |
||
| 331 | public function setEncoder($id, $encoder) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Get encoder. |
||
| 347 | * |
||
| 348 | * @param $id |
||
| 349 | * |
||
| 350 | * @return CouchbaseBucketResource |
||
| 351 | */ |
||
| 352 | public function getEncoder($id) |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Set decoder. |
||
| 365 | * |
||
| 366 | * @param string $id |
||
| 367 | * @param string $encoder |
||
| 368 | * |
||
| 369 | * @return CouchbaseResourceManager |
||
| 370 | */ |
||
| 371 | public function setDecoder($id, $encoder) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * Get decoder. |
||
| 387 | * |
||
| 388 | * @param $id |
||
| 389 | * |
||
| 390 | * @return CouchbaseBucketResource |
||
| 391 | */ |
||
| 392 | public function getDecoder($id) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Set servers. |
||
| 405 | * |
||
| 406 | * $servers can be an array list or a comma separated list of servers. |
||
| 407 | * One server in the list can be descripted as follows: |
||
| 408 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
| 409 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
| 410 | * - List: array(<host>[, <port>][, <weight>]) |
||
| 411 | * |
||
| 412 | * @param string $id |
||
| 413 | * @param string $bucket |
||
| 414 | * |
||
| 415 | * @return CouchbaseResourceManager |
||
| 416 | */ |
||
| 417 | 61 | public function setBucket($id, $bucket) |
|
| 430 | |||
| 431 | public function getBucket($id) |
||
| 441 | |||
| 442 | /** |
||
| 443 | * Set servers. |
||
| 444 | * |
||
| 445 | * $servers can be an array list or a comma separated list of servers. |
||
| 446 | * One server in the list can be descripted as follows: |
||
| 447 | * - URI: [tcp://]<host>[:<port>][?weight=<weight>] |
||
| 448 | * - Assoc: array('host' => <host>[, 'port' => <port>][, 'weight' => <weight>]) |
||
| 449 | * - List: array(<host>[, <port>][, <weight>]) |
||
| 450 | * |
||
| 451 | * @param string $id |
||
| 452 | * @param string $password |
||
| 453 | * |
||
| 454 | * @return CouchbaseResourceManager |
||
| 455 | */ |
||
| 456 | public function setPassword($id, $password) |
||
| 469 | |||
| 470 | public function getPassword($id) |
||
| 480 | |||
| 481 | /** |
||
| 482 | * Normalize a list of servers into the following format: |
||
| 483 | * array(array('host' => <host>, 'port' => <port>, 'weight' => <weight>)[, ...]). |
||
| 484 | * |
||
| 485 | * @param string|array $servers |
||
| 486 | * |
||
| 487 | * @throws \Zend\Stdlib\Exception\InvalidArgumentException |
||
| 488 | */ |
||
| 489 | 61 | protected function normalizeServers(&$servers) |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Compare 2 normalized server arrays |
||
| 505 | * (Compares only the host and the port). |
||
| 506 | * |
||
| 507 | * @param array $serverA |
||
| 508 | * @param array $serverB |
||
| 509 | * |
||
| 510 | * @return int |
||
| 511 | */ |
||
| 512 | protected function compareServers(array $serverA, array $serverB) |
||
| 522 | } |
||
| 523 |
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.