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 Domain extends OperatorResource implements Creatable, Listable, Retrievable, Updateable, Deletable |
||
| 17 | { |
||
| 18 | /** @var string */ |
||
| 19 | public $id; |
||
| 20 | |||
| 21 | /** @var string */ |
||
| 22 | public $name; |
||
| 23 | |||
| 24 | /** @var array */ |
||
| 25 | public $links; |
||
| 26 | |||
| 27 | /** @var bool */ |
||
| 28 | public $enabled; |
||
| 29 | |||
| 30 | /** @var string */ |
||
| 31 | public $description; |
||
| 32 | |||
| 33 | protected $resourceKey = 'domain'; |
||
| 34 | protected $resourcesKey = 'domains'; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * {@inheritDoc} |
||
| 38 | * |
||
| 39 | * @param array $data {@see \OpenStack\Identity\v3\Api::postDomains} |
||
| 40 | */ |
||
| 41 | 1 | public function create(array $data): Creatable |
|
| 46 | |||
| 47 | /** |
||
| 48 | * {@inheritDoc} |
||
| 49 | */ |
||
| 50 | 1 | public function retrieve() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * {@inheritDoc} |
||
| 58 | */ |
||
| 59 | 1 | public function update() |
|
| 64 | |||
| 65 | /** |
||
| 66 | * {@inheritDoc} |
||
| 67 | */ |
||
| 68 | 1 | public function delete() |
|
| 72 | |||
| 73 | /** |
||
| 74 | * @param array $options {@see \OpenStack\Identity\v3\Api::getUserRoles} |
||
| 75 | * |
||
| 76 | * @return \Generator |
||
| 77 | */ |
||
| 78 | 1 | public function listUserRoles(array $options = []): \Generator |
|
| 79 | { |
||
| 80 | 1 | $options['domainId'] = $this->id; |
|
| 81 | 1 | return $this->model(Role::class)->enumerate($this->api->getUserRoles(), $options); |
|
|
|
|||
| 82 | } |
||
| 83 | |||
| 84 | /** |
||
| 85 | * @param array $options {@see \OpenStack\Identity\v3\Api::putUserRoles} |
||
| 86 | */ |
||
| 87 | 1 | public function grantUserRole(array $options = []) |
|
| 91 | |||
| 92 | /** |
||
| 93 | * @param array $options {@see \OpenStack\Identity\v3\Api::headUserRole} |
||
| 94 | * |
||
| 95 | * @return bool |
||
| 96 | */ |
||
| 97 | 3 | View Code Duplication | public function checkUserRole(array $options = []): bool |
| 98 | { |
||
| 99 | try { |
||
| 100 | 3 | $this->execute($this->api->headUserRole(), ['domainId' => $this->id] + $options); |
|
| 101 | 1 | return true; |
|
| 102 | 1 | } catch (BadResponseError $e) { |
|
| 103 | 1 | return false; |
|
| 104 | } |
||
| 105 | } |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @param array $options {@see \OpenStack\Identity\v3\Api::deleteUserRole} |
||
| 109 | */ |
||
| 110 | 1 | public function revokeUserRole(array $options = []) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * @param array $options {@see \OpenStack\Identity\v3\Api::getGroupRoles} |
||
| 117 | * |
||
| 118 | * @return \Generator |
||
| 119 | */ |
||
| 120 | 1 | public function listGroupRoles(array $options = []): \Generator |
|
| 121 | { |
||
| 122 | 1 | $options['domainId'] = $this->id; |
|
| 123 | 1 | return $this->model(Role::class)->enumerate($this->api->getGroupRoles(), $options); |
|
| 124 | } |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @param array $options {@see \OpenStack\Identity\v3\Api::putGroupRole} |
||
| 128 | */ |
||
| 129 | 1 | public function grantGroupRole(array $options = []) |
|
| 133 | |||
| 134 | /** |
||
| 135 | * @param array $options {@see \OpenStack\Identity\v3\Api::headGroupRole} |
||
| 136 | * |
||
| 137 | * @return bool |
||
| 138 | */ |
||
| 139 | 2 | View Code Duplication | public function checkGroupRole(array $options = []): bool |
| 140 | { |
||
| 141 | try { |
||
| 142 | 2 | $this->execute($this->api->headGroupRole(), ['domainId' => $this->id] + $options); |
|
| 143 | 1 | return true; |
|
| 144 | 1 | } catch (BadResponseError $e) { |
|
| 145 | 1 | return false; |
|
| 146 | } |
||
| 147 | } |
||
| 148 | |||
| 149 | /** |
||
| 150 | * @param array $options {@see \OpenStack\Identity\v3\Api::deleteGroupRole} |
||
| 151 | */ |
||
| 152 | 1 | public function revokeGroupRole(array $options = []) |
|
| 156 | } |
||
| 157 |
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: