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 |
||
117 | |||
118 | /** |
||
119 | * {@inheritDoc} |
||
120 | 1 | */ |
|
121 | public function update() |
||
126 | |||
127 | /** |
||
128 | 1 | * {@inheritDoc} |
|
129 | */ |
||
130 | 1 | public function delete() |
|
134 | |||
135 | /** |
||
136 | * {@inheritDoc} |
||
137 | */ |
||
138 | public function retrieve() |
||
143 | 1 | ||
144 | /** |
||
145 | 1 | * Changes the root password for a server. |
|
146 | 1 | * |
|
147 | * @param string $newPassword The new root password |
||
148 | */ |
||
149 | public function changePassword(string $newPassword) |
||
156 | 1 | ||
157 | /** |
||
158 | * Reboots the server. |
||
159 | 1 | * |
|
160 | 1 | * @param string $type The type of reboot that will be performed. Either SOFT or HARD is supported. |
|
161 | 1 | */ |
|
162 | 1 | public function reboot(string $type = Enum::REBOOT_SOFT) |
|
173 | 1 | ||
174 | /** |
||
175 | 1 | * Starts server |
|
176 | 1 | */ |
|
177 | public function start() |
||
184 | 1 | ||
185 | /** |
||
186 | 1 | * Stops server |
|
187 | 1 | */ |
|
188 | 1 | public function stop() |
|
195 | |||
196 | /** |
||
197 | 1 | * Rebuilds the server. |
|
198 | * |
||
199 | 1 | * @param array $options {@see \OpenStack\Compute\v2\Api::rebuildServer} |
|
200 | 1 | */ |
|
201 | public function rebuild(array $options) |
||
208 | 1 | ||
209 | /** |
||
210 | * Resizes the server to a new flavor. Once this operation is complete and server has transitioned |
||
211 | * to an active state, you will either need to call {@see confirmResize()} or {@see revertResize()}. |
||
212 | * |
||
213 | * @param string $flavorId The UUID of the new flavor your server will be based on. |
||
214 | */ |
||
215 | 1 | public function resize(string $flavorId) |
|
224 | |||
225 | /** |
||
226 | * Confirms a previous resize operation. |
||
227 | */ |
||
228 | 2 | public function confirmResize() |
|
232 | 2 | ||
233 | 2 | /** |
|
234 | 2 | * Reverts a previous resize operation. |
|
235 | */ |
||
236 | public function revertResize() |
||
240 | |||
241 | /** |
||
242 | 1 | * Gets the console output of the server. |
|
243 | * |
||
244 | 1 | * @param int $length The number of lines, by default all lines will be returned. |
|
245 | 1 | * @return string |
|
246 | */ |
||
247 | public function getConsoleOutput(int $length = -1) |
||
256 | 1 | ||
257 | /** |
||
258 | 1 | * Gets a VNC console for a server. |
|
259 | 1 | * |
|
260 | * @param string $type The type of VNC console: novnc|xvpvnc. |
||
261 | * Defaults to novnc. |
||
262 | * |
||
263 | * @return array |
||
264 | */ |
||
265 | public function getVncConsole($type = Enum::CONSOLE_NOVNC): array |
||
270 | |||
271 | 1 | /** |
|
272 | * Gets a RDP console for a server. |
||
273 | 1 | * |
|
274 | 1 | * @param string $type The type of VNC console: rdp-html5 (default). |
|
275 | * |
||
276 | * @return array |
||
277 | */ |
||
278 | public function getRDPConsole($type = Enum::CONSOLE_RDP_HTML5): array |
||
283 | |||
284 | 1 | /** |
|
285 | * Gets a Spice console for a server. |
||
286 | 1 | * |
|
287 | 1 | * @param string $type The type of VNC console: spice-html5. |
|
288 | * |
||
289 | * @return array |
||
290 | */ |
||
291 | public function getSpiceConsole($type = Enum::CONSOLE_SPICE_HTML5): array |
||
296 | |||
297 | 1 | /** |
|
298 | 1 | * Gets a serial console for a server. |
|
299 | * |
||
300 | * @param string $type The type of VNC console: serial. |
||
301 | * |
||
302 | * @return array |
||
303 | */ |
||
304 | public function getSerialConsole($type = Enum::CONSOLE_SERIAL): array |
||
309 | |||
310 | /** |
||
311 | * Creates an image for the current server. |
||
312 | * |
||
313 | * @param array $options {@see \OpenStack\Compute\v2\Api::createServerImage} |
||
314 | */ |
||
315 | public function createImage(array $options) |
||
320 | |||
321 | /** |
||
322 | * Iterates over all the IP addresses for this server. |
||
323 | * |
||
324 | * @param array $options {@see \OpenStack\Compute\v2\Api::getAddressesByNetwork} |
||
325 | * |
||
326 | * @return array An array containing to two keys: "public" and "private" |
||
327 | */ |
||
328 | public function listAddresses(array $options = []): array |
||
336 | |||
337 | /** |
||
338 | * Returns Generator for InterfaceAttachment |
||
339 | * |
||
340 | * @return \Generator |
||
341 | */ |
||
342 | public function listInterfaceAttachments(array $options = []): \Generator |
||
346 | |||
347 | /** |
||
348 | * Retrieves metadata from the API. |
||
349 | * |
||
350 | * @return array |
||
351 | */ |
||
352 | public function getMetadata(): array |
||
357 | |||
358 | /** |
||
359 | * Resets all the metadata for this server with the values provided. All existing metadata keys |
||
360 | * will either be replaced or removed. |
||
361 | * |
||
362 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::putServerMetadata} |
||
363 | */ |
||
364 | public function resetMetadata(array $metadata) |
||
369 | |||
370 | /** |
||
371 | * Merges the existing metadata for the server with the values provided. Any existing keys |
||
372 | * referenced in the user options will be replaced with the user's new values. All other |
||
373 | * existing keys will remain unaffected. |
||
374 | * |
||
375 | * @param array $metadata {@see \OpenStack\Compute\v2\Api::postServerMetadata} |
||
376 | * |
||
377 | * @return array |
||
378 | */ |
||
379 | public function mergeMetadata(array $metadata) |
||
384 | |||
385 | /** |
||
386 | * Retrieve the value for a specific metadata key. |
||
387 | * |
||
388 | * @param string $key {@see \OpenStack\Compute\v2\Api::getServerMetadataKey} |
||
389 | * |
||
390 | * @return mixed |
||
391 | */ |
||
392 | View Code Duplication | public function getMetadataItem(string $key) |
|
399 | |||
400 | /** |
||
401 | * Remove a specific metadata key. |
||
402 | * |
||
403 | * @param string $key {@see \OpenStack\Compute\v2\Api::deleteServerMetadataKey} |
||
404 | */ |
||
405 | View Code Duplication | public function deleteMetadataItem(string $key) |
|
413 | |||
414 | |||
415 | /** |
||
416 | * Add security group to a server (addSecurityGroup action) |
||
417 | * |
||
418 | * @param array $options {@see \OpenStack\Compute\v2\Api::postSecurityGroup} |
||
419 | * |
||
420 | * @return SecurityGroup |
||
421 | */ |
||
422 | public function addSecurityGroup(array $options) : SecurityGroup |
||
430 | |||
431 | /** |
||
432 | * Add security group to a server (addSecurityGroup action) |
||
433 | * |
||
434 | * @param array $options {@see \OpenStack\Compute\v2\Api::deleteSecurityGroup} |
||
435 | */ |
||
436 | public function removeSecurityGroup(array $options) |
||
441 | |||
442 | public function parseMetadata(ResponseInterface $response): array |
||
446 | |||
447 | /** |
||
448 | * Returns Generator for SecurityGroups |
||
449 | * |
||
450 | * @return \Generator |
||
451 | */ |
||
452 | public function listSecurityGroups(): \Generator |
||
456 | |||
457 | |||
458 | /** |
||
459 | * Returns Generator for VolumeAttachment |
||
460 | * |
||
461 | * @return \Generator |
||
462 | */ |
||
463 | public function listVolumeAttachments(): \Generator |
||
467 | |||
468 | /** |
||
469 | * Attach a volume and returns volume that was attached |
||
470 | * |
||
471 | * @param $volumeId |
||
472 | * |
||
473 | * @return VolumeAttachment |
||
474 | */ |
||
475 | public function attachVolume(string $volumeId): VolumeAttachment |
||
481 | |||
482 | /** |
||
483 | * Detach a volume |
||
484 | * |
||
485 | * @param $attachmentId |
||
486 | * |
||
487 | * @return void |
||
488 | */ |
||
489 | public function detachVolume(string $attachmentId) |
||
493 | } |
||
494 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.