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 Role |
||
18 | { |
||
19 | const APP_ADMIN = 'app-admin'; |
||
20 | const APP_MANAGER = 'app-manager'; |
||
21 | const APP_USER = 'app-user'; |
||
22 | const APP_PUBLIC = 'app-public'; |
||
23 | |||
24 | const PROJECT_MANAGER = 'project-manager'; |
||
25 | const PROJECT_MEMBER = 'project-member'; |
||
26 | const PROJECT_VIEWER = 'project-viewer'; |
||
27 | |||
28 | /** |
||
29 | * Get application roles. |
||
30 | * |
||
31 | * @return array |
||
32 | */ |
||
33 | public function getApplicationRoles() |
||
41 | |||
42 | /** |
||
43 | * Get project roles. |
||
44 | * |
||
45 | * @return array |
||
46 | */ |
||
47 | View Code Duplication | public function getProjectRoles() |
|
55 | |||
56 | /** |
||
57 | * Check if the given role is custom or not. |
||
58 | * |
||
59 | * @param string $role |
||
60 | * |
||
61 | * @return bool |
||
62 | */ |
||
63 | public function isCustomProjectRole($role) |
||
67 | |||
68 | /** |
||
69 | * Get role name. |
||
70 | * |
||
71 | * @param string $role |
||
72 | * |
||
73 | * @return string |
||
74 | */ |
||
75 | public function getRoleName($role) |
||
81 | } |
||
82 |
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.