Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like Server 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 Server, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 22 | class Server extends OperatorResource implements |
||
| 23 | Creatable, |
||
| 24 | Updateable, |
||
| 25 | Deletable, |
||
| 26 | Retrievable, |
||
| 27 | Listable |
||
| 28 | { |
||
| 29 | use HasWaiterTrait; |
||
| 30 | |||
| 31 | /** @var string */ |
||
| 32 | public $id; |
||
| 33 | |||
| 34 | /** @var string */ |
||
| 35 | public $ipv4; |
||
| 36 | |||
| 37 | /** @var string */ |
||
| 38 | public $ipv6; |
||
| 39 | |||
| 40 | /** @var array */ |
||
| 41 | public $addresses; |
||
| 42 | |||
| 43 | /** @var \DateTimeImmutable */ |
||
| 44 | public $created; |
||
| 45 | |||
| 46 | /** @var \DateTimeImmutable */ |
||
| 47 | public $updated; |
||
| 48 | |||
| 49 | /** @var Flavor */ |
||
| 50 | public $flavor; |
||
| 51 | |||
| 52 | /** @var string */ |
||
| 53 | public $hostId; |
||
| 54 | |||
| 55 | /** @var string */ |
||
| 56 | public $hypervisorHostname; |
||
| 57 | |||
| 58 | /** @var Image */ |
||
| 59 | public $image; |
||
| 60 | |||
| 61 | /** @var array */ |
||
| 62 | public $links; |
||
| 63 | |||
| 64 | /** @var array */ |
||
| 65 | public $metadata; |
||
| 66 | |||
| 67 | /** @var string */ |
||
| 68 | public $name; |
||
| 69 | |||
| 70 | /** @var string */ |
||
| 71 | public $progress; |
||
| 72 | |||
| 73 | /** @var string */ |
||
| 74 | public $status; |
||
| 75 | |||
| 76 | /** @var string */ |
||
| 77 | public $tenantId; |
||
| 78 | |||
| 79 | /** @var string */ |
||
| 80 | public $userId; |
||
| 81 | |||
| 82 | /** @var string */ |
||
| 83 | public $adminPass; |
||
| 84 | |||
| 85 | /** @var string */ |
||
| 86 | public $taskState; |
||
| 87 | |||
| 88 | /** @var string */ |
||
| 89 | public $powerState; |
||
| 90 | |||
| 91 | /** @var string */ |
||
| 92 | public $vmState; |
||
| 93 | |||
| 94 | protected $resourceKey = 'server'; |
||
| 95 | protected $resourcesKey = 'servers'; |
||
| 96 | protected $markerKey = 'id'; |
||
| 97 | |||
| 98 | protected $aliases = [ |
||
| 99 | 'block_device_mapping_v2' => 'blockDeviceMapping', |
||
| 100 | 'accessIPv4' => 'ipv4', |
||
| 101 | 2 | 'accessIPv6' => 'ipv6', |
|
| 102 | 'tenant_id' => 'tenantId', |
||
| 103 | 2 | 'user_id' => 'userId', |
|
| 104 | 2 | 'security_groups' => 'securityGroups', |
|
| 105 | 'OS-EXT-STS:task_state' => 'taskState', |
||
| 106 | 'OS-EXT-STS:power_state' => 'powerState', |
||
| 107 | 'OS-EXT-STS:vm_state' => 'vmState', |
||
| 108 | 'OS-EXT-SRV-ATTR:hypervisor_hostname' => 'hypervisorHostname', |
||
| 109 | ]; |
||
| 110 | 1 | ||
| 111 | /** |
||
| 112 | 1 | * {@inheritDoc} |
|
| 113 | * |
||
| 114 | 1 | * @param array $userOptions {@see \OpenStack\Compute\v2\Api::postServer} |
|
| 115 | */ |
||
| 116 | public function create(array $userOptions): Creatable |
||
| 125 | |||
| 126 | /** |
||
| 127 | * {@inheritDoc} |
||
| 128 | 1 | */ |
|
| 129 | public function update() |
||
| 134 | |||
| 135 | /** |
||
| 136 | * {@inheritDoc} |
||
| 137 | */ |
||
| 138 | public function delete() |
||
| 142 | 1 | ||
| 143 | 1 | /** |
|
| 144 | * {@inheritDoc} |
||
| 145 | 1 | */ |
|
| 146 | 1 | public function retrieve() |
|
| 151 | |||
| 152 | /** |
||
| 153 | 2 | * Changes the root password for a server. |
|
| 154 | * |
||
| 155 | 2 | * @param string $newPassword The new root password |
|
| 156 | 1 | */ |
|
| 157 | public function changePassword(string $newPassword) |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Reboots the server. |
||
| 167 | * |
||
| 168 | * @param string $type The type of reboot that will be performed. Either SOFT or HARD is supported. |
||
| 169 | */ |
||
| 170 | 1 | public function reboot(string $type = Enum::REBOOT_SOFT) |
|
| 181 | |||
| 182 | /** |
||
| 183 | * Starts server |
||
| 184 | 1 | */ |
|
| 185 | public function start() |
||
| 192 | 1 | ||
| 193 | /** |
||
| 194 | * Stops server |
||
| 195 | */ |
||
| 196 | public function stop() |
||
| 203 | |||
| 204 | /** |
||
| 205 | 1 | * Rebuilds the server. |
|
| 206 | * |
||
| 207 | 1 | * @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer} |
|
| 208 | 1 | */ |
|
| 209 | public function rebuild(array $options) |
||
| 216 | |||
| 217 | 1 | /** |
|
| 218 | 1 | * Resizes the server to a new flavor. Once this operation is complete and server has transitioned |
|
| 219 | 1 | * to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}. |
|
| 220 | * |
||
| 221 | * @param string $flavorId The UUID of the new flavor your server will be based on. |
||
| 222 | */ |
||
| 223 | public function resize(string $flavorId) |
||
| 232 | 2 | ||
| 233 | 2 | /** |
|
| 234 | 2 | * Confirms a previous resize operation. |
|
| 235 | */ |
||
| 236 | public function confirmResize() |
||
| 240 | |||
| 241 | /** |
||
| 242 | 1 | * Reverts a previous resize operation. |
|
| 243 | */ |
||
| 244 | 1 | public function revertResize() |
|
| 248 | |||
| 249 | /** |
||
| 250 | * Gets the console output of the server. |
||
| 251 | * |
||
| 252 | * @param int $length The number of lines, by default all lines will be returned. |
||
| 253 | * @return string |
||
| 254 | */ |
||
| 255 | public function getConsoleOutput(int $length = -1): string |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Gets a VNC console for a server. |
||
| 270 | * |
||
| 271 | 1 | * @param string $type The type of VNC console: novnc|xvpvnc. |
|
| 272 | * Defaults to novnc. |
||
| 273 | 1 | * |
|
| 274 | 1 | * @return array |
|
| 275 | */ |
||
| 276 | public function getVncConsole($type = Enum::CONSOLE_NOVNC): array |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Gets a RDP console for a server. |
||
| 284 | 1 | * |
|
| 285 | * @param string $type The type of VNC console: rdp-html5 (default). |
||
| 286 | 1 | * |
|
| 287 | 1 | * @return array |
|
| 288 | */ |
||
| 289 | public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5): array |
||
| 294 | |||
| 295 | 1 | /** |
|
| 296 | * Gets a Spice console for a server. |
||
| 297 | 1 | * |
|
| 298 | 1 | * @param string $type The type of VNC console: spice-html5. |
|
| 299 | * |
||
| 300 | * @return array |
||
| 301 | */ |
||
| 302 | public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5): array |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Gets a serial console for a server. |
||
| 310 | * |
||
| 311 | * @param string $type The type of VNC console: serial. |
||
| 312 | * |
||
| 313 | * @return array |
||
| 314 | */ |
||
| 315 | public function getSerialConsole($type = Enum::CONSOLE_SERIAL): array |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Creates an image for the current server. |
||
| 323 | * |
||
| 324 | * @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage} |
||
| 325 | */ |
||
| 326 | public function createImage(array $options) |
||
| 331 | |||
| 332 | /** |
||
| 333 | * Iterates over all the IP addresses for this server. |
||
| 334 | * |
||
| 335 | * @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork} |
||
| 336 | * |
||
| 337 | * @return array An array containing to two keys: "public" and "private" |
||
| 338 | */ |
||
| 339 | public function listAddresses(array $options = []): array |
||
| 347 | |||
| 348 | /** |
||
| 349 | * Returns Generator for InterfaceAttachment |
||
| 350 | * |
||
| 351 | * @return \Generator |
||
| 352 | */ |
||
| 353 | public function listInterfaceAttachments(array $options = []): \Generator |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Gets an interface attachment. |
||
| 360 | * |
||
| 361 | * @param string $portId The unique ID of the port. |
||
| 362 | * @return InterfaceAttachment |
||
| 363 | */ |
||
| 364 | View Code Duplication | public function getInterfaceAttachment(string $portId): InterfaceAttachment |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Creates an interface attachment. |
||
| 376 | * |
||
| 377 | * @param array $userOptions {@see \OpenStack\Compute\v2\Api::postInterfaceAttachment} |
||
| 378 | * @return InterfaceAttachment |
||
| 379 | */ |
||
| 380 | public function createInterfaceAttachment(array $userOptions): InterfaceAttachment |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Detaches an interface attachment. |
||
| 392 | * |
||
| 393 | * @param string $portId |
||
| 394 | */ |
||
| 395 | public function detachInterface(string $portId) |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Retrieves metadata from the API. |
||
| 405 | * |
||
| 406 | * @return array |
||
| 407 | */ |
||
| 408 | public function getMetadata(): array |
||
| 413 | |||
| 414 | /** |
||
| 415 | * Resets all the metadata for this server with the values provided. All existing metadata keys |
||
| 416 | * will either be replaced or removed. |
||
| 417 | * |
||
| 418 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata} |
||
| 419 | */ |
||
| 420 | public function resetMetadata(array $metadata) |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Merges the existing metadata for the server with the values provided. Any existing keys |
||
| 428 | * referenced in the user options will be replaced with the user's new values. All other |
||
| 429 | * existing keys will remain unaffected. |
||
| 430 | * |
||
| 431 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata} |
||
| 432 | * |
||
| 433 | * @return array |
||
| 434 | */ |
||
| 435 | public function mergeMetadata(array $metadata) |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Retrieve the value for a specific metadata key. |
||
| 443 | * |
||
| 444 | * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} |
||
| 445 | * |
||
| 446 | * @return mixed |
||
| 447 | */ |
||
| 448 | View Code Duplication | public function getMetadataItem(string $key) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Remove a specific metadata key. |
||
| 458 | * |
||
| 459 | * @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey} |
||
| 460 | */ |
||
| 461 | View Code Duplication | public function deleteMetadataItem(string $key) |
|
| 469 | |||
| 470 | |||
| 471 | /** |
||
| 472 | * Add security group to a server (addSecurityGroup action) |
||
| 473 | * |
||
| 474 | * @param array $options {@see \OpenStack\Compute\v2\Api::postSecurityGroup} |
||
| 475 | * |
||
| 476 | * @return SecurityGroup |
||
| 477 | */ |
||
| 478 | View Code Duplication | public function addSecurityGroup(array $options) : SecurityGroup |
|
| 486 | |||
| 487 | /** |
||
| 488 | * Add security group to a server (addSecurityGroup action) |
||
| 489 | * |
||
| 490 | * @param array $options {@see \OpenStack\Compute\v2\Api::deleteSecurityGroup} |
||
| 491 | */ |
||
| 492 | public function removeSecurityGroup(array $options) |
||
| 497 | |||
| 498 | public function parseMetadata(ResponseInterface $response): array |
||
| 502 | |||
| 503 | /** |
||
| 504 | * Returns Generator for SecurityGroups |
||
| 505 | * |
||
| 506 | * @return \Generator |
||
| 507 | */ |
||
| 508 | public function listSecurityGroups(): \Generator |
||
| 512 | |||
| 513 | |||
| 514 | /** |
||
| 515 | * Returns Generator for VolumeAttachment |
||
| 516 | * |
||
| 517 | * @return \Generator |
||
| 518 | */ |
||
| 519 | public function listVolumeAttachments(): \Generator |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Attach a volume and returns volume that was attached |
||
| 526 | * |
||
| 527 | * @param $volumeId |
||
| 528 | * |
||
| 529 | * @return VolumeAttachment |
||
| 530 | */ |
||
| 531 | public function attachVolume(string $volumeId): VolumeAttachment |
||
| 537 | |||
| 538 | /** |
||
| 539 | * Detach a volume |
||
| 540 | * |
||
| 541 | * @param $attachmentId |
||
| 542 | * |
||
| 543 | * @return void |
||
| 544 | */ |
||
| 545 | public function detachVolume(string $attachmentId) |
||
| 549 | } |
||
| 550 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.