Complex classes like Droplet 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 Droplet, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 25 | class Droplet extends AbstractApi |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @param int $per_page |
||
| 29 | * @param int $page |
||
| 30 | * @param string|null $tag |
||
| 31 | * |
||
| 32 | * @return DropletEntity[] |
||
| 33 | */ |
||
| 34 | public function getAll($per_page = 200, $page = 1, $tag = null) |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @param int $id |
||
| 53 | * |
||
| 54 | * @return DropletEntity[] |
||
| 55 | */ |
||
| 56 | public function getNeighborsById($id) |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @return DropletEntity[] |
||
| 69 | */ |
||
| 70 | public function getAllNeighbors() |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @return UpgradeEntity[] |
||
| 83 | */ |
||
| 84 | public function getUpgrades() |
||
| 94 | |||
| 95 | /** |
||
| 96 | * @param int $id |
||
| 97 | * |
||
| 98 | * @throws HttpException |
||
| 99 | * |
||
| 100 | * @return DropletEntity |
||
| 101 | */ |
||
| 102 | public function getById($id) |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @param array|string $names |
||
| 113 | * @param string $region |
||
| 114 | * @param string $size |
||
| 115 | * @param string|int $image |
||
| 116 | * @param bool $backups |
||
| 117 | * @param bool $ipv6 |
||
| 118 | * @param bool $privateNetworking |
||
| 119 | * @param int[] $sshKeys |
||
| 120 | * @param string $userData |
||
| 121 | * @param bool $monitoring |
||
| 122 | * @param array $volumes |
||
| 123 | * @param array $tags |
||
| 124 | * @param bool $wait |
||
| 125 | * @param int $waitTimeout |
||
| 126 | * |
||
| 127 | * @throws HttpException |
||
| 128 | * |
||
| 129 | * @return DropletEntity|null |
||
| 130 | */ |
||
| 131 | public function create($names, $region, $size, $image, $backups = false, $ipv6 = false, $privateNetworking = false, array $sshKeys = [], $userData = '', $monitoring = true, array $volumes = [], array $tags = [], $wait = false, $waitTimeout = 300) |
||
| 183 | |||
| 184 | /** |
||
| 185 | * @param int $id |
||
| 186 | * |
||
| 187 | * @throws HttpException |
||
| 188 | */ |
||
| 189 | public function delete($id) |
||
| 193 | |||
| 194 | /** |
||
| 195 | * @param int $id |
||
| 196 | * |
||
| 197 | * @throws HttpException |
||
| 198 | * |
||
| 199 | * @return KernelEntity[] |
||
| 200 | */ |
||
| 201 | public function getAvailableKernels($id) |
||
| 213 | |||
| 214 | /** |
||
| 215 | * @param int $id |
||
| 216 | * |
||
| 217 | * @return ImageEntity[] |
||
| 218 | */ |
||
| 219 | public function getSnapshots($id) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * @param int $id |
||
| 236 | * |
||
| 237 | * @return ImageEntity[] |
||
| 238 | */ |
||
| 239 | public function getBackups($id) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @param int $id |
||
| 254 | * |
||
| 255 | * @return ActionEntity[] |
||
| 256 | */ |
||
| 257 | public function getActions($id) |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param int $id |
||
| 272 | * @param int $actionId |
||
| 273 | * |
||
| 274 | * @return ActionEntity |
||
| 275 | */ |
||
| 276 | public function getActionById($id, $actionId) |
||
| 284 | |||
| 285 | /** |
||
| 286 | * @param int $id |
||
| 287 | * |
||
| 288 | * @throws HttpException |
||
| 289 | * |
||
| 290 | * @return ActionEntity |
||
| 291 | */ |
||
| 292 | public function reboot($id) |
||
| 296 | |||
| 297 | /** |
||
| 298 | * @param int $id |
||
| 299 | * |
||
| 300 | * @throws HttpException |
||
| 301 | * |
||
| 302 | * @return ActionEntity |
||
| 303 | */ |
||
| 304 | public function powerCycle($id) |
||
| 308 | |||
| 309 | /** |
||
| 310 | * @param int $id |
||
| 311 | * |
||
| 312 | * @throws HttpException |
||
| 313 | * |
||
| 314 | * @return ActionEntity |
||
| 315 | */ |
||
| 316 | public function shutdown($id) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * @param int $id |
||
| 323 | * |
||
| 324 | * @throws HttpException |
||
| 325 | * |
||
| 326 | * @return ActionEntity |
||
| 327 | */ |
||
| 328 | public function powerOff($id) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param int $id |
||
| 335 | * |
||
| 336 | * @throws HttpException |
||
| 337 | * |
||
| 338 | * @return ActionEntity |
||
| 339 | */ |
||
| 340 | public function powerOn($id) |
||
| 344 | |||
| 345 | /** |
||
| 346 | * @param int $id |
||
| 347 | * |
||
| 348 | * @throws HttpException |
||
| 349 | * |
||
| 350 | * @return ActionEntity |
||
| 351 | */ |
||
| 352 | public function passwordReset($id) |
||
| 356 | |||
| 357 | /** |
||
| 358 | * @param int $id |
||
| 359 | * @param string $size |
||
| 360 | * @param bool $disk |
||
| 361 | * |
||
| 362 | * @throws HttpException |
||
| 363 | * |
||
| 364 | * @return ActionEntity |
||
| 365 | */ |
||
| 366 | public function resize($id, $size, $disk = true) |
||
| 370 | |||
| 371 | /** |
||
| 372 | * @param int $id |
||
| 373 | * @param int $image |
||
| 374 | * |
||
| 375 | * @throws HttpException |
||
| 376 | * |
||
| 377 | * @return ActionEntity |
||
| 378 | */ |
||
| 379 | public function restore($id, $image) |
||
| 383 | |||
| 384 | /** |
||
| 385 | * @param int $id |
||
| 386 | * @param int|string $image |
||
| 387 | * |
||
| 388 | * @throws HttpException |
||
| 389 | * |
||
| 390 | * @return ActionEntity |
||
| 391 | */ |
||
| 392 | public function rebuild($id, $image) |
||
| 396 | |||
| 397 | /** |
||
| 398 | * @param int $id |
||
| 399 | * @param string $name |
||
| 400 | * |
||
| 401 | * @throws HttpException |
||
| 402 | * |
||
| 403 | * @return ActionEntity |
||
| 404 | */ |
||
| 405 | public function rename($id, $name) |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @param int $id |
||
| 412 | * @param int $kernel |
||
| 413 | * |
||
| 414 | * @throws HttpException |
||
| 415 | * |
||
| 416 | * @return ActionEntity |
||
| 417 | */ |
||
| 418 | public function changeKernel($id, $kernel) |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @param int $id |
||
| 425 | * |
||
| 426 | * @throws HttpException |
||
| 427 | * |
||
| 428 | * @return ActionEntity |
||
| 429 | */ |
||
| 430 | public function enableIpv6($id) |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @param int $id |
||
| 437 | * |
||
| 438 | * @throws HttpException |
||
| 439 | * |
||
| 440 | * @return ActionEntity |
||
| 441 | */ |
||
| 442 | public function enableBackups($id) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @param int $id |
||
| 449 | * |
||
| 450 | * @throws HttpException |
||
| 451 | * |
||
| 452 | * @return ActionEntity |
||
| 453 | */ |
||
| 454 | public function disableBackups($id) |
||
| 458 | |||
| 459 | /** |
||
| 460 | * @param int $id |
||
| 461 | * |
||
| 462 | * @throws HttpException |
||
| 463 | * |
||
| 464 | * @return ActionEntity |
||
| 465 | */ |
||
| 466 | public function enablePrivateNetworking($id) |
||
| 470 | |||
| 471 | /** |
||
| 472 | * @param int $id |
||
| 473 | * @param string $name |
||
| 474 | * |
||
| 475 | * @throws HttpException |
||
| 476 | * |
||
| 477 | * @return ActionEntity |
||
| 478 | */ |
||
| 479 | public function snapshot($id, $name) |
||
| 483 | |||
| 484 | /** |
||
| 485 | * @param int $id |
||
| 486 | * @param array $options |
||
| 487 | * |
||
| 488 | * @throws HttpException |
||
| 489 | * |
||
| 490 | * @return ActionEntity |
||
| 491 | */ |
||
| 492 | private function executeAction($id, array $options) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @param DropletEntity $droplet |
||
| 503 | * @param int $waitTimeout |
||
| 504 | * |
||
| 505 | * @throws HttpException |
||
| 506 | * |
||
| 507 | * @return DropletEntity|null |
||
| 508 | */ |
||
| 509 | public function waitForActive($droplet, $waitTimeout) |
||
| 523 | } |
||
| 524 |