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 |
||
| 17 | class AdminGroups |
||
| 18 | { |
||
| 19 | /** @var AdminGroupConfiguration */ |
||
| 20 | protected $adminGroupConfiguration; |
||
| 21 | |||
| 22 | /** @var Factory */ |
||
| 23 | protected $userGroupFactory; |
||
| 24 | |||
| 25 | /** |
||
| 26 | * GroupsPlugin constructor. |
||
| 27 | * |
||
| 28 | * @param AdminGroupConfiguration $adminGroupConfiguration |
||
| 29 | * @param Factory $userGroupFactory |
||
| 30 | */ |
||
| 31 | 7 | public function __construct( |
|
| 38 | |||
| 39 | /** |
||
| 40 | * Get list of all user groups. |
||
| 41 | * Can be useful for creating group based GUI widgets. |
||
| 42 | * |
||
| 43 | * @return Group[] |
||
| 44 | */ |
||
| 45 | 1 | public function getUserGroups() |
|
| 55 | |||
| 56 | /** |
||
| 57 | * Get the group in which a user is. |
||
| 58 | * This is useful for gui actions. |
||
| 59 | * |
||
| 60 | * @param string $login |
||
| 61 | * |
||
| 62 | * @return Group |
||
| 63 | */ |
||
| 64 | 1 | public function getLoginUserGroups($login) |
|
| 73 | |||
| 74 | /** |
||
| 75 | * Get admin group Label |
||
| 76 | * |
||
| 77 | * @param string $login |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | View Code Duplication | public function getLoginGroupLabel($login) |
|
| 93 | |||
| 94 | |||
| 95 | /** |
||
| 96 | * Get (or create a new) admin user group |
||
| 97 | * |
||
| 98 | * @param string $groupName |
||
| 99 | * |
||
| 100 | * @return Group |
||
| 101 | */ |
||
| 102 | 2 | protected function getUserGroup($groupName) |
|
| 114 | |||
| 115 | /** |
||
| 116 | * Checks if group, a login or a player has a certain permission or not. |
||
| 117 | * |
||
| 118 | * @param string|Group|Player $recipient |
||
| 119 | * @param string $permission The permission to check for. |
||
| 120 | * |
||
| 121 | * @return bool |
||
| 122 | */ |
||
| 123 | 3 | public function hasPermission($recipient, $permission) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @param string $permission |
||
| 142 | */ |
||
| 143 | 3 | protected function hasLoginPermission($login, $permission) |
|
| 151 | |||
| 152 | |||
| 153 | /** |
||
| 154 | * Check if a group has a certain permission or not. |
||
| 155 | * |
||
| 156 | * @param string $groupName The name of the group to check permissions for. |
||
| 157 | * @param string $permission The permission to check for. |
||
| 158 | * |
||
| 159 | * @return bool |
||
| 160 | * @throws UnknownGroupException Thrown when group isn't an admin group. |
||
| 161 | */ |
||
| 162 | 3 | public function hasGroupPermission($groupName, $permission) |
|
| 184 | |||
| 185 | /** |
||
| 186 | * @param string $groupName |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getGroupLabel($groupName) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * gets if the player is admin |
||
| 201 | * |
||
| 202 | * @param string $login |
||
| 203 | * @return bool |
||
| 204 | */ |
||
| 205 | public function isAdmin($login) |
||
| 209 | |||
| 210 | } |
||
| 211 |
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.