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 | * Shelves server |
|
206 | */ |
||
207 | 1 | public function shelve() |
|
214 | |||
215 | 1 | /* |
|
216 | * Suspend server |
||
217 | 1 | */ |
|
218 | 1 | public function suspend() |
|
225 | |||
226 | /** |
||
227 | * Shelf-offloads server |
||
228 | 2 | */ |
|
229 | public function shelveOffload() |
||
236 | |||
237 | /** |
||
238 | * Unshelves server |
||
239 | */ |
||
240 | public function unshelve() |
||
247 | |||
248 | /* |
||
249 | * Resume server |
||
250 | */ |
||
251 | public function resume() |
||
258 | 1 | ||
259 | 1 | /** |
|
260 | * Locks server |
||
261 | */ |
||
262 | public function lock() |
||
269 | |||
270 | /** |
||
271 | 1 | * Unlocks server |
|
272 | */ |
||
273 | 1 | public function unlock() |
|
280 | |||
281 | /** |
||
282 | * Rebuilds the server. |
||
283 | * |
||
284 | 1 | * @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer} |
|
285 | */ |
||
286 | 1 | public function rebuild(array $options) |
|
293 | |||
294 | /** |
||
295 | 1 | * Resizes the server to a new flavor. Once this operation is complete and server has transitioned |
|
296 | * to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}. |
||
297 | 1 | * |
|
298 | 1 | * @param string $flavorId The UUID of the new flavor your server will be based on. |
|
299 | */ |
||
300 | public function resize(string $flavorId) |
||
309 | |||
310 | /** |
||
311 | * Confirms a previous resize operation. |
||
312 | */ |
||
313 | public function confirmResize() |
||
317 | |||
318 | /** |
||
319 | * Reverts a previous resize operation. |
||
320 | */ |
||
321 | public function revertResize() |
||
325 | |||
326 | /** |
||
327 | * Gets a VNC console for a server. |
||
328 | * |
||
329 | * @param string $type The type of VNC console: novnc|xvpvnc. |
||
330 | * Defaults to novnc. |
||
331 | * |
||
332 | * @return array |
||
333 | */ |
||
334 | public function getVncConsole($type = Enum::CONSOLE_NOVNC): array |
||
339 | |||
340 | /** |
||
341 | * Gets a RDP console for a server. |
||
342 | * |
||
343 | * @param string $type The type of VNC console: rdp-html5 (default). |
||
344 | * |
||
345 | * @return array |
||
346 | */ |
||
347 | public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5): array |
||
352 | |||
353 | /** |
||
354 | * Gets a Spice console for a server. |
||
355 | * |
||
356 | * @param string $type The type of VNC console: spice-html5. |
||
357 | * |
||
358 | * @return array |
||
359 | */ |
||
360 | public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5): array |
||
365 | |||
366 | /** |
||
367 | * Gets a serial console for a server. |
||
368 | * |
||
369 | * @param string $type The type of VNC console: serial. |
||
370 | * |
||
371 | * @return array |
||
372 | */ |
||
373 | public function getSerialConsole($type = Enum::CONSOLE_SERIAL): array |
||
378 | |||
379 | /** |
||
380 | * Get the console log. |
||
381 | * |
||
382 | * @param int $length Number of lines of console log to grab. |
||
383 | * |
||
384 | * @return string - the console log output |
||
385 | */ |
||
386 | public function getConsoleLog(int $length = 50): string { |
||
390 | |||
391 | /** |
||
392 | * Creates an image for the current server. |
||
393 | * |
||
394 | * @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage} |
||
395 | */ |
||
396 | public function createImage(array $options) |
||
401 | |||
402 | /** |
||
403 | * Iterates over all the IP addresses for this server. |
||
404 | * |
||
405 | * @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork} |
||
406 | * |
||
407 | * @return array An array containing to two keys: "public" and "private" |
||
408 | */ |
||
409 | public function listAddresses(array $options = []): array |
||
417 | |||
418 | /** |
||
419 | * Returns Generator for InterfaceAttachment |
||
420 | * |
||
421 | * @return \Generator |
||
422 | */ |
||
423 | public function listInterfaceAttachments(array $options = []): \Generator |
||
427 | |||
428 | /** |
||
429 | * Retrieves metadata from the API. |
||
430 | * |
||
431 | * @return array |
||
432 | */ |
||
433 | public function getMetadata(): array |
||
438 | |||
439 | /** |
||
440 | * Resets all the metadata for this server with the values provided. All existing metadata keys |
||
441 | * will either be replaced or removed. |
||
442 | * |
||
443 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata} |
||
444 | */ |
||
445 | public function resetMetadata(array $metadata) |
||
450 | |||
451 | /** |
||
452 | * Merges the existing metadata for the server with the values provided. Any existing keys |
||
453 | * referenced in the user options will be replaced with the user's new values. All other |
||
454 | * existing keys will remain unaffected. |
||
455 | * |
||
456 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata} |
||
457 | * |
||
458 | * @return array |
||
459 | */ |
||
460 | public function mergeMetadata(array $metadata) |
||
465 | |||
466 | /** |
||
467 | * Retrieve the value for a specific metadata key. |
||
468 | * |
||
469 | * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} |
||
470 | * |
||
471 | * @return mixed |
||
472 | */ |
||
473 | View Code Duplication | public function getMetadataItem(string $key) |
|
480 | |||
481 | /** |
||
482 | * Remove a specific metadata key. |
||
483 | * |
||
484 | * @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey} |
||
485 | */ |
||
486 | View Code Duplication | public function deleteMetadataItem(string $key) |
|
494 | |||
495 | |||
496 | /** |
||
497 | * Add security group to a server (addSecurityGroup action) |
||
498 | * |
||
499 | * @param array $options {@see \OpenStack\Compute\v2\Api::postSecurityGroup} |
||
500 | * |
||
501 | * @return SecurityGroup |
||
502 | */ |
||
503 | public function addSecurityGroup(array $options) : SecurityGroup |
||
511 | |||
512 | /** |
||
513 | * Add security group to a server (addSecurityGroup action) |
||
514 | * |
||
515 | * @param array $options {@see \OpenStack\Compute\v2\Api::deleteSecurityGroup} |
||
516 | */ |
||
517 | public function removeSecurityGroup(array $options) |
||
522 | |||
523 | public function parseMetadata(ResponseInterface $response): array |
||
527 | |||
528 | /** |
||
529 | * Returns Generator for SecurityGroups |
||
530 | * |
||
531 | * @return \Generator |
||
532 | */ |
||
533 | public function listSecurityGroups(): \Generator |
||
537 | |||
538 | |||
539 | /** |
||
540 | * Returns Generator for VolumeAttachment |
||
541 | * |
||
542 | * @return \Generator |
||
543 | */ |
||
544 | public function listVolumeAttachments(): \Generator |
||
548 | |||
549 | /** |
||
550 | * Attach a volume and returns volume that was attached |
||
551 | * |
||
552 | * @param $volumeId |
||
553 | * |
||
554 | * @return VolumeAttachment |
||
555 | */ |
||
556 | public function attachVolume(string $volumeId): VolumeAttachment |
||
562 | |||
563 | /** |
||
564 | * Detach a volume |
||
565 | * |
||
566 | * @param $attachmentId |
||
567 | * |
||
568 | * @return void |
||
569 | */ |
||
570 | public function detachVolume(string $attachmentId) |
||
574 | |||
575 | /** |
||
576 | * Get a Generator for the instance actions |
||
577 | * |
||
578 | * @return \Generator |
||
579 | */ |
||
580 | public function listInstanceActions(): \Generator |
||
584 | |||
585 | /** |
||
586 | * Get a specific instance action |
||
587 | * |
||
588 | * @string The request ID of the instance action |
||
589 | * @return InstanceAction |
||
590 | */ |
||
591 | public function getInstanceAction(string $requestId): InstanceAction |
||
597 | } |
||
598 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.