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 Service extends AbstractService |
||
| 23 | { |
||
| 24 | 1 | /** |
|
| 25 | * Create a new network resource. |
||
| 26 | 1 | * |
|
| 27 | * @param array $options {@see \OpenStack\Networking\v2\Api::postNetwork} |
||
| 28 | * |
||
| 29 | * @return Network |
||
| 30 | */ |
||
| 31 | public function createNetwork(array $options): Network |
||
| 35 | |||
| 36 | 1 | /** |
|
| 37 | * Create a new network resources. |
||
| 38 | 1 | * |
|
| 39 | * @param array $options {@see \OpenStack\Networking\v2\Api::postNetworks} |
||
| 40 | * |
||
| 41 | * @return array |
||
| 42 | */ |
||
| 43 | public function createNetworks(array $options): array |
||
| 47 | |||
| 48 | /** |
||
| 49 | * Retrieve a network object without calling the remote API. Any values provided in the array will populate the |
||
| 50 | 1 | * empty object, allowing you greater control without the expense of network transactions. To call the remote API |
|
| 51 | * and have the response populate the object, call {@see Network::retrieve}. |
||
| 52 | 1 | * |
|
| 53 | * @param string $id |
||
| 54 | * |
||
| 55 | * @return Network |
||
| 56 | */ |
||
| 57 | public function getNetwork(string $id): Network |
||
| 61 | |||
| 62 | 1 | /** |
|
| 63 | * List networks. |
||
| 64 | 1 | * |
|
| 65 | * @param array $options {@see \OpenStack\Networking\v2\Api::getNetworks} |
||
| 66 | * |
||
| 67 | * @return \Generator |
||
| 68 | */ |
||
| 69 | public function listNetworks(array $options = []): \Generator |
||
| 73 | |||
| 74 | 1 | /** |
|
| 75 | * Create a new subnet resource. |
||
| 76 | 1 | * |
|
| 77 | * @param array $options {@see \OpenStack\Networking\v2\Api::postSubnet} |
||
| 78 | * |
||
| 79 | * @return Subnet |
||
| 80 | */ |
||
| 81 | public function createSubnet(array $options): Subnet |
||
| 85 | |||
| 86 | 1 | /** |
|
| 87 | * Create a new subnet resources. |
||
| 88 | 1 | * |
|
| 89 | * @param array $options {@see \OpenStack\Networking\v2\Api::postSubnets} |
||
| 90 | * |
||
| 91 | * @return []Subnet |
||
| 92 | */ |
||
| 93 | public function createSubnets(array $options): array |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Retrieve a subnet object without calling the remote API. Any values provided in the array will populate the |
||
| 100 | 1 | * empty object, allowing you greater control without the expense of network transactions. To call the remote API |
|
| 101 | * and have the response populate the object, call {@see Subnet::retrieve}. |
||
| 102 | 1 | * |
|
| 103 | * @param string $id |
||
| 104 | * |
||
| 105 | * @return Subnet |
||
| 106 | */ |
||
| 107 | public function getSubnet(string $id): Subnet |
||
| 111 | |||
| 112 | 1 | /** |
|
| 113 | * List subnets. |
||
| 114 | 1 | * |
|
| 115 | * @param array $options {@see \OpenStack\Networking\v2\Api::getSubnets} |
||
| 116 | * |
||
| 117 | * @return \Generator |
||
| 118 | */ |
||
| 119 | public function listSubnets(array $options = []): \Generator |
||
| 123 | |||
| 124 | 1 | /** |
|
| 125 | * Create a new port resource. |
||
| 126 | 1 | * |
|
| 127 | * @param array $options {@see \OpenStack\Networking\v2\Api::postSinglePort} |
||
| 128 | * |
||
| 129 | * @return Port |
||
| 130 | */ |
||
| 131 | public function createPort(array $options): Port |
||
| 135 | |||
| 136 | 1 | /** |
|
| 137 | * Create new port resources. |
||
| 138 | 1 | * |
|
| 139 | * @param array $options {@see \OpenStack\Networking\v2\Api::postMultiplePorts} |
||
| 140 | * |
||
| 141 | * @return []Port |
||
| 142 | */ |
||
| 143 | public function createPorts(array $options): array |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Retrieve a subnet object without calling the remote API. Any values provided in the array will populate the |
||
| 150 | 1 | * empty object, allowing you greater control without the expense of network transactions. To call the remote API |
|
| 151 | * and have the response populate the object, call {@see Port::retrieve}. |
||
| 152 | 1 | * |
|
| 153 | * @param string $id |
||
| 154 | * |
||
| 155 | * @return Port |
||
| 156 | */ |
||
| 157 | public function getPort(string $id): Port |
||
| 161 | |||
| 162 | 1 | /** |
|
| 163 | * List ports. |
||
| 164 | 1 | * |
|
| 165 | * @param array $options {@see \OpenStack\Networking\v2\Api::getPorts} |
||
| 166 | * |
||
| 167 | * @return \Generator |
||
| 168 | */ |
||
| 169 | public function listPorts(array $options = []): \Generator |
||
| 173 | |||
| 174 | /** |
||
| 175 | * Lists quotas for projects with non-default quota values. |
||
| 176 | * |
||
| 177 | * @return \Generator |
||
| 178 | */ |
||
| 179 | public function listQuotas(): \Generator |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Lists quotas for a project. |
||
| 186 | * |
||
| 187 | * Retrieve a quota object without calling the remote API. Any values provided in the array will populate the |
||
| 188 | * empty object, allowing you greater control without the expense of network transactions. To call the remote API |
||
| 189 | * and have the response populate the object, call {@see Quota::retrieve}. |
||
| 190 | * |
||
| 191 | * @param string $tenantId |
||
| 192 | * |
||
| 193 | * @return Quota |
||
| 194 | */ |
||
| 195 | public function getQuota(string $tenantId): Quota |
||
| 199 | |||
| 200 | /** |
||
| 201 | * Lists default quotas for a project |
||
| 202 | * |
||
| 203 | * @param string $tenantId |
||
| 204 | * |
||
| 205 | * @return Quota |
||
| 206 | */ |
||
| 207 | View Code Duplication | public function getDefaultQuota(string $tenantId): Quota |
|
| 208 | { |
||
| 209 | $quota = $this->model(Quota::class, ['tenantId' => $tenantId]); |
||
| 210 | $quota->populateFromResponse($this->execute($this->api->getQuotaDefault(), ['tenantId' => $tenantId])); |
||
| 211 | |||
| 212 | return $quota; |
||
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Lists loadbalancers for projects |
||
| 217 | * |
||
| 218 | * @return \Generator |
||
| 219 | */ |
||
| 220 | public function listLoadBalancers(): \Generator |
||
| 224 | |||
| 225 | /** |
||
| 226 | * Retrieve an instance of a LoadBalancer object |
||
| 227 | * |
||
| 228 | * @param string $id |
||
| 229 | * |
||
| 230 | * @return LoadBalancer |
||
| 231 | */ |
||
| 232 | public function getLoadBalancer(string $id): LoadBalancer |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Create a new loadbalancer resource. |
||
| 239 | * |
||
| 240 | * @param array $options {@see \OpenStack\Networking\v2\Api::postLoadBalancer} |
||
| 241 | * |
||
| 242 | * @return LoadBalancer |
||
| 243 | */ |
||
| 244 | public function createLoadBalancer(array $options): LoadBalancer |
||
| 248 | |||
| 249 | /** |
||
| 250 | * Lists loadbalancer listeners |
||
| 251 | * |
||
| 252 | * @return \Generator |
||
| 253 | */ |
||
| 254 | public function listLoadBalancerListeners(): \Generator |
||
| 258 | |||
| 259 | /** |
||
| 260 | * Retrieve an instance of a loadbalancer listener object |
||
| 261 | * |
||
| 262 | * @param string $id |
||
| 263 | * |
||
| 264 | * @return LoadBalancerListener |
||
| 265 | */ |
||
| 266 | public function getLoadBalancerListener(string $id): LoadBalancerListener |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Create a new loadbalancer Listener resource. |
||
| 273 | * |
||
| 274 | * @param array $options {@see \OpenStack\Networking\v2\Api::postLoadBalancerListener} |
||
| 275 | * |
||
| 276 | * @return LoadBalancerListener |
||
| 277 | */ |
||
| 278 | public function createLoadBalancerListener(array $options): LoadBalancerListener |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Lists loadbalancer pools |
||
| 285 | * |
||
| 286 | * @return \Generator |
||
| 287 | */ |
||
| 288 | public function listLoadBalancerPools(): \Generator |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Retrieve an instance of a loadbalancer Pool object |
||
| 295 | * |
||
| 296 | * @param string $id |
||
| 297 | * |
||
| 298 | * @return LoadBalancerPool |
||
| 299 | */ |
||
| 300 | public function getLoadBalancerPool(string $id): LoadBalancerPool |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Create a new loadbalancer Pool resource. |
||
| 307 | * |
||
| 308 | * @param array $options {@see \OpenStack\Networking\v2\Api::postLoadBalancerPool} |
||
| 309 | * |
||
| 310 | * @return LoadBalancerPool |
||
| 311 | */ |
||
| 312 | public function createLoadBalancerPool(array $options): LoadBalancerPool |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Lists loadbalancer members |
||
| 319 | * |
||
| 320 | * @param string $poolId |
||
| 321 | * @return \Generator |
||
| 322 | */ |
||
| 323 | public function listLoadBalancerMembers(string $poolId): \Generator |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Retrieve an instance of a loadbalancer Member object |
||
| 330 | * |
||
| 331 | * @param string $poolId |
||
| 332 | * @param string $memberId |
||
| 333 | * |
||
| 334 | * @return LoadBalancerMember |
||
| 335 | */ |
||
| 336 | public function getLoadBalancerMember(string $poolId, string $memberId): LoadBalancerMember |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Create a new loadbalancer member resource. |
||
| 343 | * |
||
| 344 | * @param array $options {@see \OpenStack\Networking\v2\Api::postLoadBalancerMember} |
||
| 345 | * |
||
| 346 | * @return LoadBalancerMember |
||
| 347 | */ |
||
| 348 | public function createLoadBalancerMember(array $options): LoadBalancerMember |
||
| 352 | |||
| 353 | /** |
||
| 354 | * Lists loadbalancer healthmonitors |
||
| 355 | * |
||
| 356 | * @return \Generator |
||
| 357 | */ |
||
| 358 | public function listLoadBalancerHealthMonitors(): \Generator |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Retrieve an instance of a loadbalancer healthmonitor object |
||
| 365 | * |
||
| 366 | * @param string $id |
||
| 367 | * |
||
| 368 | * @return LoadBalancerHealthMonitor |
||
| 369 | */ |
||
| 370 | public function getLoadBalancerHealthMonitor(string $id): LoadBalancerHealthMonitor |
||
| 374 | |||
| 375 | /** |
||
| 376 | * Create a new loadbalancer healthmonitor resource. |
||
| 377 | * |
||
| 378 | * @param array $options {@see \OpenStack\Networking\v2\Api::postLoadBalancerHealthMonitor} |
||
| 379 | * |
||
| 380 | * @return LoadBalancerHealthMonitor |
||
| 381 | */ |
||
| 382 | public function createLoadBalancerHealthMonitor(array $options): LoadBalancerHealthMonitor |
||
| 386 | } |
||
| 387 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: