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:
| 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 | protected $resourceKey = 'server'; |
||
| 89 | protected $resourcesKey = 'servers'; |
||
| 90 | protected $markerKey = 'id'; |
||
| 91 | |||
| 92 | protected $aliases = [ |
||
| 93 | 'block_device_mapping_v2' => 'blockDeviceMapping', |
||
| 94 | 'accessIPv4' => 'ipv4', |
||
| 95 | 'accessIPv6' => 'ipv6', |
||
| 96 | 'tenant_id' => 'tenantId', |
||
| 97 | 'user_id' => 'userId', |
||
| 98 | 'security_groups' => 'securityGroups', |
||
| 99 | 'OS-EXT-STS:task_state' => 'taskState', |
||
| 100 | 'OS-EXT-SRV-ATTR:hypervisor_hostname' => 'hypervisorHostname', |
||
| 101 | 2 | ]; |
|
| 102 | |||
| 103 | 2 | /** |
|
| 104 | 2 | * {@inheritDoc} |
|
| 105 | * |
||
| 106 | * @param array $userOptions {@see \OpenStack\Compute\v2\Api::postServer} |
||
| 107 | */ |
||
| 108 | public function create(array $userOptions): Creatable |
||
| 113 | |||
| 114 | 1 | /** |
|
| 115 | * {@inheritDoc} |
||
| 116 | */ |
||
| 117 | public function update() |
||
| 122 | 1 | ||
| 123 | 1 | /** |
|
| 124 | * {@inheritDoc} |
||
| 125 | */ |
||
| 126 | public function delete() |
||
| 130 | 1 | ||
| 131 | /** |
||
| 132 | 1 | * {@inheritDoc} |
|
| 133 | */ |
||
| 134 | public function retrieve() |
||
| 139 | |||
| 140 | 1 | /** |
|
| 141 | * Changes the root password for a server. |
||
| 142 | 1 | * |
|
| 143 | 1 | * @param string $newPassword The new root password |
|
| 144 | */ |
||
| 145 | 1 | public function changePassword(string $newPassword) |
|
| 152 | |||
| 153 | 2 | /** |
|
| 154 | * Reboots the server. |
||
| 155 | 2 | * |
|
| 156 | 1 | * @param string $type The type of reboot that will be performed. Either SOFT or HARD is supported. |
|
| 157 | */ |
||
| 158 | public function reboot(string $type = Enum::REBOOT_SOFT) |
||
| 169 | |||
| 170 | 1 | /** |
|
| 171 | * Starts server |
||
| 172 | 1 | */ |
|
| 173 | 1 | public function start() |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Stops server |
||
| 183 | */ |
||
| 184 | 1 | public function stop() |
|
| 191 | 1 | ||
| 192 | 1 | /** |
|
| 193 | * Rebuilds the server. |
||
| 194 | * |
||
| 195 | * @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer} |
||
| 196 | */ |
||
| 197 | 1 | public function rebuild(array $options) |
|
| 204 | |||
| 205 | 1 | /** |
|
| 206 | * Resizes the server to a new flavor. Once this operation is complete and server has transitioned |
||
| 207 | 1 | * to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}. |
|
| 208 | 1 | * |
|
| 209 | * @param string $flavorId The UUID of the new flavor your server will be based on. |
||
| 210 | */ |
||
| 211 | public function resize(string $flavorId) |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Confirms a previous resize operation. |
||
| 223 | */ |
||
| 224 | public function confirmResize() |
||
| 228 | 2 | ||
| 229 | /** |
||
| 230 | 2 | * Reverts a previous resize operation. |
|
| 231 | */ |
||
| 232 | 2 | public function revertResize() |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Gets a VNC console for a server. |
||
| 239 | * |
||
| 240 | * @param string $type The type of VNC console: novnc|xvpvnc. |
||
| 241 | * Defaults to novnc. |
||
| 242 | 1 | * |
|
| 243 | * @return array |
||
| 244 | 1 | */ |
|
| 245 | 1 | public function getVncConsole($type = Enum::CONSOLE_NOVNC): array |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Gets a RDP console for a server. |
||
| 253 | * |
||
| 254 | * @param string $type The type of VNC console: rdp-html5 (default). |
||
| 255 | * |
||
| 256 | 1 | * @return array |
|
| 257 | */ |
||
| 258 | 1 | public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5): array |
|
| 263 | |||
| 264 | /** |
||
| 265 | * Gets a Spice console for a server. |
||
| 266 | * |
||
| 267 | * @param string $type The type of VNC console: spice-html5. |
||
| 268 | * |
||
| 269 | * @return array |
||
| 270 | */ |
||
| 271 | 1 | public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5): array |
|
| 276 | |||
| 277 | /** |
||
| 278 | * Gets a serial console for a server. |
||
| 279 | * |
||
| 280 | * @param string $type The type of VNC console: serial. |
||
| 281 | * |
||
| 282 | * @return array |
||
| 283 | */ |
||
| 284 | 1 | public function getSerialConsole($type = Enum::CONSOLE_SERIAL): array |
|
| 289 | |||
| 290 | /** |
||
| 291 | * Creates an image for the current server. |
||
| 292 | * |
||
| 293 | * @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage} |
||
| 294 | */ |
||
| 295 | 1 | public function createImage(array $options) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Iterates over all the IP addresses for this server. |
||
| 303 | * |
||
| 304 | * @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork} |
||
| 305 | * |
||
| 306 | * @return array An array containing to two keys: "public" and "private" |
||
| 307 | */ |
||
| 308 | public function listAddresses(array $options = []): array |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Returns Generator for InterfaceAttachment |
||
| 319 | * |
||
| 320 | * @return \Generator |
||
| 321 | */ |
||
| 322 | public function listInterfaceAttachments(array $options = []): \Generator |
||
| 326 | |||
| 327 | /** |
||
| 328 | * Retrieves metadata from the API. |
||
| 329 | * |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function getMetadata(): array |
||
| 337 | |||
| 338 | /** |
||
| 339 | * Resets all the metadata for this server with the values provided. All existing metadata keys |
||
| 340 | * will either be replaced or removed. |
||
| 341 | * |
||
| 342 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata} |
||
| 343 | */ |
||
| 344 | public function resetMetadata(array $metadata) |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Merges the existing metadata for the server with the values provided. Any existing keys |
||
| 352 | * referenced in the user options will be replaced with the user's new values. All other |
||
| 353 | * existing keys will remain unaffected. |
||
| 354 | * |
||
| 355 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata} |
||
| 356 | * |
||
| 357 | * @return array |
||
| 358 | */ |
||
| 359 | public function mergeMetadata(array $metadata) |
||
| 364 | |||
| 365 | /** |
||
| 366 | * Retrieve the value for a specific metadata key. |
||
| 367 | * |
||
| 368 | * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} |
||
| 369 | * |
||
| 370 | * @return mixed |
||
| 371 | */ |
||
| 372 | View Code Duplication | public function getMetadataItem(string $key) |
|
| 379 | |||
| 380 | /** |
||
| 381 | * Remove a specific metadata key. |
||
| 382 | * |
||
| 383 | * @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey} |
||
| 384 | */ |
||
| 385 | View Code Duplication | public function deleteMetadataItem(string $key) |
|
| 393 | |||
| 394 | |||
| 395 | /** |
||
| 396 | * Add security group to a server (addSecurityGroup action) |
||
| 397 | * |
||
| 398 | * @param array $options {@see \OpenStack\Compute\v2\Api::postSecurityGroup} |
||
| 399 | * |
||
| 400 | * @return SecurityGroup |
||
| 401 | */ |
||
| 402 | public function addSecurityGroup(array $options) : SecurityGroup |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Add security group to a server (addSecurityGroup action) |
||
| 413 | * |
||
| 414 | * @param array $options {@see \OpenStack\Compute\v2\Api::deleteSecurityGroup} |
||
| 415 | */ |
||
| 416 | public function removeSecurityGroup(array $options) |
||
| 421 | |||
| 422 | public function parseMetadata(ResponseInterface $response): array |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Returns Generator for SecurityGroups |
||
| 429 | * |
||
| 430 | * @return \Generator |
||
| 431 | */ |
||
| 432 | public function listSecurityGroups(): \Generator |
||
| 436 | |||
| 437 | |||
| 438 | /** |
||
| 439 | * Returns Generator for VolumeAttachment |
||
| 440 | * |
||
| 441 | * @return \Generator |
||
| 442 | */ |
||
| 443 | public function listVolumeAttachments(): \Generator |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Attach a volume and returns volume that was attached |
||
| 450 | * |
||
| 451 | * @param $volumeId |
||
| 452 | * |
||
| 453 | * @return VolumeAttachment |
||
| 454 | */ |
||
| 455 | public function attachVolume(string $volumeId): VolumeAttachment |
||
| 461 | |||
| 462 | /** |
||
| 463 | * Detach a volume |
||
| 464 | * |
||
| 465 | * @param $attachmentId |
||
| 466 | * |
||
| 467 | * @return void |
||
| 468 | */ |
||
| 469 | public function detachVolume(string $attachmentId) |
||
| 473 | } |
||
| 474 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.