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); |
||
| 12 | class Resource extends OperatorResource implements Retrievable |
||
| 13 | { |
||
| 14 | const RESOURCE_TYPE_GENERIC = 'generic'; |
||
| 15 | const RESOURCE_TYPE_CEPH_ACCOUNT = 'ceph_account'; |
||
| 16 | const RESOURCE_TYPE_HOST = 'host'; |
||
| 17 | const RESOURCE_TYPE_HOST_DISK = 'host_disk'; |
||
| 18 | const RESOURCE_TYPE_HOST_NETWORK_INTERFACE = 'host_network_interface'; |
||
| 19 | const RESOURCE_TYPE_IDENTITY = 'identity'; |
||
| 20 | const RESOURCE_TYPE_IMAGE = 'image'; |
||
| 21 | const RESOURCE_TYPE_INSTANCE = 'instance'; |
||
| 22 | const RESOURCE_TYPE_INSTANCE_DISK = 'instance_disk'; |
||
| 23 | const RESOURCE_TYPE_INSTANCE_NETWORK_INTERFACE = 'instance_network_interface'; |
||
| 24 | const RESOURCE_TYPE_IPMI = 'ipmi'; |
||
| 25 | const RESOURCE_TYPE_NETWORK = 'network'; |
||
| 26 | const RESOURCE_TYPE_STACK = 'stack'; |
||
| 27 | const RESOURCE_TYPE_SWIFT_ACCOUNT = 'swift_account'; |
||
| 28 | const RESOURCE_TYPE_VOLUME = 'volume'; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | public $createdByUserId; |
||
| 32 | |||
| 33 | /** @var string */ |
||
| 34 | public $startedAt; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | public $displayName; |
||
| 38 | |||
| 39 | /** @var string */ |
||
| 40 | public $revisionEnd; |
||
| 41 | |||
| 42 | /** @var string */ |
||
| 43 | public $userId; |
||
| 44 | |||
| 45 | /** @var string */ |
||
| 46 | public $createdByProjectId; |
||
| 47 | |||
| 48 | /** @var string */ |
||
| 49 | public $id; |
||
| 50 | |||
| 51 | /** @var array */ |
||
| 52 | public $metrics; |
||
| 53 | |||
| 54 | /** @var string */ |
||
| 55 | public $host; |
||
| 56 | |||
| 57 | /** @var string */ |
||
| 58 | public $imageRef; |
||
| 59 | |||
| 60 | /** @var string */ |
||
| 61 | public $flavorId; |
||
| 62 | |||
| 63 | /** @var string */ |
||
| 64 | public $serverGroup; |
||
| 65 | |||
| 66 | /** @var string */ |
||
| 67 | public $originalResourceId; |
||
| 68 | |||
| 69 | /** @var string */ |
||
| 70 | public $revisionStart; |
||
| 71 | |||
| 72 | /** @var string */ |
||
| 73 | public $projectId; |
||
| 74 | |||
| 75 | /** @var string */ |
||
| 76 | public $type; |
||
| 77 | |||
| 78 | /** @var string */ |
||
| 79 | public $endedAt; |
||
| 80 | |||
| 81 | protected $aliases = [ |
||
| 82 | 'created_by_user_id' => 'createdByUserId', |
||
| 83 | 'started_at' => 'startedAt', |
||
| 84 | 'display_name' => 'displayName', |
||
| 85 | 'revision_end' => 'revisionEnd', |
||
| 86 | 'user_id' => 'userId', |
||
| 87 | 'created_by_project_id' => 'createdByProjectId', |
||
| 88 | 'image_ref' => 'imageRef', |
||
| 89 | 'flavor_id' => 'flavorId', |
||
| 90 | 'server_group' => 'serverGroup', |
||
| 91 | 'original_resource_id' => 'originalResourceId', |
||
| 92 | 'revision_start' => 'revisionStart', |
||
| 93 | 'project_id' => 'projectId', |
||
| 94 | 'ended_at' => 'endedAt', |
||
| 95 | ]; |
||
| 96 | |||
| 97 | public function retrieve() |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @param string $metric |
||
| 105 | * |
||
| 106 | * @return Metric |
||
| 107 | */ |
||
| 108 | View Code Duplication | public function getMetric(string $metric): Metric |
|
|
|
|||
| 109 | { |
||
| 110 | $response = $this->execute( |
||
| 111 | $this->api->getResourceMetric(), |
||
| 112 | [ |
||
| 113 | 'resourceId' => $this->id, |
||
| 114 | 'metric' => $metric, |
||
| 115 | 'type' => $this->type, |
||
| 116 | ] |
||
| 117 | ); |
||
| 118 | $metric = $this->model(Metric::class)->populateFromResponse($response); |
||
| 119 | |||
| 120 | return $metric; |
||
| 121 | } |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetricMeasures} |
||
| 125 | * |
||
| 126 | * @return array |
||
| 127 | */ |
||
| 128 | public function getMetricMeasures(array $options = []): array |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @param array $options {@see \OpenStack\Metric\v1\Gnocchi\Api::getResourceMetrics} |
||
| 145 | * |
||
| 146 | * @return \Generator |
||
| 147 | */ |
||
| 148 | public function listResourceMetrics(array $options = []): \Generator |
||
| 154 | } |
||
| 155 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.