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); |
||
| 16 | class Project extends OperatorResource implements Creatable, Retrievable, Listable, Updateable, Deletable |
||
| 17 | { |
||
| 18 | /** @var string */ |
||
| 19 | public $domainId; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | public $parentId; |
||
| 23 | |||
| 24 | /** @var bool */ |
||
| 25 | public $enabled; |
||
| 26 | |||
| 27 | /** @var string */ |
||
| 28 | public $description; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | public $id; |
||
| 32 | |||
| 33 | /** @var array */ |
||
| 34 | public $links; |
||
| 35 | |||
| 36 | /** @var string */ |
||
| 37 | public $name; |
||
| 38 | |||
| 39 | protected $aliases = [ |
||
| 40 | 'domain_id' => 'domainId', |
||
| 41 | 'parent_id' => 'parentId', |
||
| 42 | ]; |
||
| 43 | |||
| 44 | protected $resourceKey = 'project'; |
||
| 45 | protected $resourcesKey = 'projects'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritDoc} |
||
| 49 | * |
||
| 50 | * @param array $data {@see \OpenStack\Identity\v3\Api::postProjects} |
||
| 51 | */ |
||
| 52 | 1 | public function create(array $data): Creatable |
|
| 53 | { |
||
| 54 | 1 | $response = $this->execute($this->api->postProjects(), $data); |
|
| 55 | 1 | $this->populateFromResponse($response); |
|
| 56 | 1 | return $this; |
|
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritDoc} |
||
| 61 | */ |
||
| 62 | 1 | public function retrieve() |
|
| 67 | |||
| 68 | /** |
||
| 69 | * {@inheritDoc} |
||
| 70 | */ |
||
| 71 | 1 | public function update() |
|
| 76 | |||
| 77 | /** |
||
| 78 | * {@inheritDoc} |
||
| 79 | */ |
||
| 80 | 1 | public function delete() |
|
| 84 | |||
| 85 | /** |
||
| 86 | * @param array $options {@see \OpenStack\Identity\v3\Api::getProjectUserRoles} |
||
| 87 | * |
||
| 88 | * @return \Generator |
||
| 89 | */ |
||
| 90 | 1 | public function listUserRoles(array $options = []): \Generator |
|
| 91 | { |
||
| 92 | 1 | $options['projectId'] = $this->id; |
|
| 93 | 1 | return $this->model(Role::class)->enumerate($this->api->getProjectUserRoles(), $options); |
|
|
|
|||
| 94 | } |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @param array $options {@see \OpenStack\Identity\v3\Api::putProjectUserRole} |
||
| 98 | */ |
||
| 99 | 1 | public function grantUserRole(array $options) |
|
| 103 | |||
| 104 | /** |
||
| 105 | * @param array $options {@see \OpenStack\Identity\v3\Api::headProjectUserRole} |
||
| 106 | * |
||
| 107 | * @return bool |
||
| 108 | */ |
||
| 109 | 2 | View Code Duplication | public function checkUserRole(array $options): bool |
| 110 | { |
||
| 111 | try { |
||
| 112 | 2 | $this->execute($this->api->headProjectUserRole(), ['projectId' => $this->id] + $options); |
|
| 113 | 1 | return true; |
|
| 114 | 1 | } catch (BadResponseError $e) { |
|
| 115 | 1 | return false; |
|
| 116 | } |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * @param array $options {@see \OpenStack\Identity\v3\Api::deleteProjectUserRole} |
||
| 121 | */ |
||
| 122 | 1 | public function revokeUserRole(array $options) |
|
| 126 | |||
| 127 | /** |
||
| 128 | * @param array $options {@see \OpenStack\Identity\v3\Api::getProjectGroupRoles} |
||
| 129 | * |
||
| 130 | * @return \Generator |
||
| 131 | */ |
||
| 132 | 1 | public function listGroupRoles(array $options = []): \Generator |
|
| 133 | { |
||
| 134 | 1 | $options['projectId'] = $this->id; |
|
| 135 | 1 | return $this->model(Role::class)->enumerate($this->api->getProjectGroupRoles(), $options); |
|
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @param array $options {@see \OpenStack\Identity\v3\Api::putProjectGroupRole} |
||
| 140 | */ |
||
| 141 | 1 | public function grantGroupRole(array $options) |
|
| 145 | |||
| 146 | /** |
||
| 147 | * @param array $options {@see \OpenStack\Identity\v3\Api::headProjectGroupRole} |
||
| 148 | * |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 2 | View Code Duplication | public function checkGroupRole(array $options): bool |
| 152 | { |
||
| 153 | try { |
||
| 154 | 2 | $this->execute($this->api->headProjectGroupRole(), ['projectId' => $this->id] + $options); |
|
| 155 | 1 | return true; |
|
| 156 | 1 | } catch (BadResponseError $e) { |
|
| 157 | 1 | return false; |
|
| 158 | } |
||
| 159 | } |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @param array $options {@see \OpenStack\Identity\v3\Api::deleteProjectGroupRole} |
||
| 163 | */ |
||
| 164 | 1 | public function revokeGroupRole(array $options) |
|
| 168 | } |
||
| 169 |
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: