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); |
||
| 23 | class Server extends OperatorResource implements |
||
| 24 | Creatable, |
||
| 25 | Updateable, |
||
| 26 | Deletable, |
||
| 27 | Retrievable, |
||
| 28 | Listable |
||
| 29 | { |
||
| 30 | use HasWaiterTrait; |
||
| 31 | |||
| 32 | /** @var string */ |
||
| 33 | public $id; |
||
| 34 | |||
| 35 | /** @var string */ |
||
| 36 | public $ipv4; |
||
| 37 | |||
| 38 | /** @var string */ |
||
| 39 | public $ipv6; |
||
| 40 | |||
| 41 | /** @var array */ |
||
| 42 | public $addresses; |
||
| 43 | |||
| 44 | /** @var \DateTimeImmutable */ |
||
| 45 | public $created; |
||
| 46 | |||
| 47 | /** @var \DateTimeImmutable */ |
||
| 48 | public $updated; |
||
| 49 | |||
| 50 | /** @var Flavor */ |
||
| 51 | public $flavor; |
||
| 52 | |||
| 53 | /** @var string */ |
||
| 54 | public $hostId; |
||
| 55 | |||
| 56 | /** @var string */ |
||
| 57 | public $hypervisorHostname; |
||
| 58 | |||
| 59 | /** @var Image */ |
||
| 60 | public $image; |
||
| 61 | |||
| 62 | /** @var array */ |
||
| 63 | public $links; |
||
| 64 | |||
| 65 | /** @var array */ |
||
| 66 | public $metadata; |
||
| 67 | |||
| 68 | /** @var string */ |
||
| 69 | public $name; |
||
| 70 | |||
| 71 | /** @var string */ |
||
| 72 | public $progress; |
||
| 73 | |||
| 74 | /** @var string */ |
||
| 75 | public $status; |
||
| 76 | |||
| 77 | /** @var string */ |
||
| 78 | public $tenantId; |
||
| 79 | |||
| 80 | /** @var string */ |
||
| 81 | public $userId; |
||
| 82 | |||
| 83 | /** @var string */ |
||
| 84 | public $adminPass; |
||
| 85 | |||
| 86 | /** @var string */ |
||
| 87 | public $taskState; |
||
| 88 | |||
| 89 | /** @var string */ |
||
| 90 | public $powerState; |
||
| 91 | |||
| 92 | /** @var string */ |
||
| 93 | public $vmState; |
||
| 94 | |||
| 95 | protected $resourceKey = 'server'; |
||
| 96 | protected $resourcesKey = 'servers'; |
||
| 97 | protected $markerKey = 'id'; |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @inheritdoc |
||
| 101 | 2 | */ |
|
| 102 | protected static function getAlias(): Alias |
||
| 126 | |||
| 127 | /** |
||
| 128 | 1 | * {@inheritDoc} |
|
| 129 | * |
||
| 130 | 1 | * @param array $userOptions {@see \OpenStack\Compute\v2\Api::postServer} |
|
| 131 | */ |
||
| 132 | 1 | public function create(array $userOptions): Creatable |
|
| 141 | |||
| 142 | 1 | /** |
|
| 143 | 1 | * {@inheritDoc} |
|
| 144 | */ |
||
| 145 | 1 | public function update() |
|
| 150 | |||
| 151 | /** |
||
| 152 | * {@inheritDoc} |
||
| 153 | 2 | */ |
|
| 154 | public function delete() |
||
| 158 | |||
| 159 | 1 | /** |
|
| 160 | 1 | * {@inheritDoc} |
|
| 161 | 1 | */ |
|
| 162 | 1 | public function retrieve() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * Changes the root password for a server. |
||
| 170 | 1 | * |
|
| 171 | * @param string $newPassword The new root password |
||
| 172 | 1 | */ |
|
| 173 | 1 | public function changePassword(string $newPassword) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Reboots the server. |
||
| 183 | * |
||
| 184 | 1 | * @param string $type The type of reboot that will be performed. Either SOFT or HARD is supported. |
|
| 185 | */ |
||
| 186 | 1 | public function reboot(string $type = Enum::REBOOT_SOFT) |
|
| 197 | 1 | ||
| 198 | /** |
||
| 199 | 1 | * Starts server |
|
| 200 | 1 | */ |
|
| 201 | public function start() |
||
| 208 | 1 | ||
| 209 | /** |
||
| 210 | * Stops server |
||
| 211 | */ |
||
| 212 | public function stop() |
||
| 219 | 1 | ||
| 220 | /** |
||
| 221 | * Rebuilds the server. |
||
| 222 | * |
||
| 223 | * @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer} |
||
| 224 | */ |
||
| 225 | public function rebuild(array $options) |
||
| 232 | 2 | ||
| 233 | 2 | /** |
|
| 234 | 2 | * Resizes the server to a new flavor. Once this operation is complete and server has transitioned |
|
| 235 | * to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}. |
||
| 236 | * |
||
| 237 | * @param string $flavorId The UUID of the new flavor your server will be based on. |
||
| 238 | */ |
||
| 239 | public function resize(string $flavorId) |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Confirms a previous resize operation. |
||
| 251 | */ |
||
| 252 | public function confirmResize() |
||
| 256 | 1 | ||
| 257 | /** |
||
| 258 | 1 | * Reverts a previous resize operation. |
|
| 259 | 1 | */ |
|
| 260 | public function revertResize() |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Gets a VNC console for a server. |
||
| 267 | * |
||
| 268 | * @param string $type The type of VNC console: novnc|xvpvnc. |
||
| 269 | * Defaults to novnc. |
||
| 270 | * |
||
| 271 | 1 | * @return array |
|
| 272 | */ |
||
| 273 | 1 | public function getVncConsole($type = Enum::CONSOLE_NOVNC): array |
|
| 278 | |||
| 279 | /** |
||
| 280 | * Gets a RDP console for a server. |
||
| 281 | * |
||
| 282 | * @param string $type The type of VNC console: rdp-html5 (default). |
||
| 283 | * |
||
| 284 | 1 | * @return array |
|
| 285 | */ |
||
| 286 | 1 | public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5): array |
|
| 291 | |||
| 292 | /** |
||
| 293 | * Gets a Spice console for a server. |
||
| 294 | * |
||
| 295 | 1 | * @param string $type The type of VNC console: spice-html5. |
|
| 296 | * |
||
| 297 | 1 | * @return array |
|
| 298 | 1 | */ |
|
| 299 | public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5): array |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Gets a serial console for a server. |
||
| 307 | * |
||
| 308 | * @param string $type The type of VNC console: serial. |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function getSerialConsole($type = Enum::CONSOLE_SERIAL): array |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Creates an image for the current server. |
||
| 320 | * |
||
| 321 | * @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage} |
||
| 322 | */ |
||
| 323 | public function createImage(array $options) |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Iterates over all the IP addresses for this server. |
||
| 331 | * |
||
| 332 | * @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork} |
||
| 333 | * |
||
| 334 | * @return array An array containing to two keys: "public" and "private" |
||
| 335 | */ |
||
| 336 | public function listAddresses(array $options = []): array |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Returns Generator for InterfaceAttachment |
||
| 347 | * |
||
| 348 | * @return \Generator |
||
| 349 | */ |
||
| 350 | public function listInterfaceAttachments(array $options = []): \Generator |
||
| 354 | |||
| 355 | /** |
||
| 356 | * Retrieves metadata from the API. |
||
| 357 | * |
||
| 358 | * @return array |
||
| 359 | */ |
||
| 360 | public function getMetadata(): array |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Resets all the metadata for this server with the values provided. All existing metadata keys |
||
| 368 | * will either be replaced or removed. |
||
| 369 | * |
||
| 370 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata} |
||
| 371 | */ |
||
| 372 | public function resetMetadata(array $metadata) |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Merges the existing metadata for the server with the values provided. Any existing keys |
||
| 380 | * referenced in the user options will be replaced with the user's new values. All other |
||
| 381 | * existing keys will remain unaffected. |
||
| 382 | * |
||
| 383 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata} |
||
| 384 | * |
||
| 385 | * @return array |
||
| 386 | */ |
||
| 387 | public function mergeMetadata(array $metadata) |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Retrieve the value for a specific metadata key. |
||
| 395 | * |
||
| 396 | * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} |
||
| 397 | * |
||
| 398 | * @return mixed |
||
| 399 | */ |
||
| 400 | View Code Duplication | public function getMetadataItem(string $key) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * Remove a specific metadata key. |
||
| 410 | * |
||
| 411 | * @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey} |
||
| 412 | */ |
||
| 413 | View Code Duplication | public function deleteMetadataItem(string $key) |
|
| 421 | |||
| 422 | |||
| 423 | /** |
||
| 424 | * Add security group to a server (addSecurityGroup action) |
||
| 425 | * |
||
| 426 | * @param array $options {@see \OpenStack\Compute\v2\Api::postSecurityGroup} |
||
| 427 | * |
||
| 428 | * @return SecurityGroup |
||
| 429 | */ |
||
| 430 | public function addSecurityGroup(array $options) : SecurityGroup |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Add security group to a server (addSecurityGroup action) |
||
| 441 | * |
||
| 442 | * @param array $options {@see \OpenStack\Compute\v2\Api::deleteSecurityGroup} |
||
| 443 | */ |
||
| 444 | public function removeSecurityGroup(array $options) |
||
| 449 | |||
| 450 | public function parseMetadata(ResponseInterface $response): array |
||
| 454 | |||
| 455 | /** |
||
| 456 | * Returns Generator for SecurityGroups |
||
| 457 | * |
||
| 458 | * @return \Generator |
||
| 459 | */ |
||
| 460 | public function listSecurityGroups(): \Generator |
||
| 464 | |||
| 465 | |||
| 466 | /** |
||
| 467 | * Returns Generator for VolumeAttachment |
||
| 468 | * |
||
| 469 | * @return \Generator |
||
| 470 | */ |
||
| 471 | public function listVolumeAttachments(): \Generator |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Attach a volume and returns volume that was attached |
||
| 478 | * |
||
| 479 | * @param $volumeId |
||
| 480 | * |
||
| 481 | * @return VolumeAttachment |
||
| 482 | */ |
||
| 483 | public function attachVolume(string $volumeId): VolumeAttachment |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Detach a volume |
||
| 492 | * |
||
| 493 | * @param $attachmentId |
||
| 494 | * |
||
| 495 | * @return void |
||
| 496 | */ |
||
| 497 | public function detachVolume(string $attachmentId) |
||
| 501 | } |
||
| 502 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.