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 |
||
| 11 | class AdminGroupConfiguration |
||
| 12 | { |
||
| 13 | protected $config; |
||
| 14 | |||
| 15 | protected $loginGroups = []; |
||
| 16 | |||
| 17 | /** |
||
| 18 | * AdminGroupConfiguration constructor. |
||
| 19 | * @param $config |
||
| 20 | */ |
||
| 21 | 13 | public function __construct($config) |
|
| 33 | |||
| 34 | /** |
||
| 35 | * Get the list of all admin groups. |
||
| 36 | * |
||
| 37 | * @return string[] |
||
| 38 | */ |
||
| 39 | 4 | public function getGroups() |
|
| 43 | |||
| 44 | /** |
||
| 45 | * Get list of all users in a group (not just connected). |
||
| 46 | * |
||
| 47 | * @param $groupName |
||
| 48 | * |
||
| 49 | * @return string[]|null |
||
| 50 | */ |
||
| 51 | 3 | View Code Duplication | public function getGroupLogins($groupName) |
| 59 | |||
| 60 | /** |
||
| 61 | * Get list of all permissions given to a group. |
||
| 62 | * |
||
| 63 | * @param string $groupName |
||
| 64 | * |
||
| 65 | * @return string[] |
||
| 66 | */ |
||
| 67 | 5 | View Code Duplication | public function getGroupPermissions($groupName) |
| 75 | |||
| 76 | /** |
||
| 77 | * Get admin group label |
||
| 78 | * |
||
| 79 | * @param string $groupName |
||
| 80 | * @return string |
||
| 81 | */ |
||
| 82 | View Code Duplication | public function getGroupLabel($groupName) |
|
| 90 | |||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $login |
||
| 94 | * |
||
| 95 | * @return string|null |
||
| 96 | */ |
||
| 97 | 7 | public function getLoginGroupName($login) |
|
| 101 | |||
| 102 | /** |
||
| 103 | * @param string $login |
||
| 104 | * @param string $permission |
||
| 105 | * |
||
| 106 | * @return bool |
||
| 107 | */ |
||
| 108 | 4 | public function hasPermission($login, $permission) |
|
| 125 | } |
||
| 126 |
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.