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 |
||
27 | trait CountTrait |
||
28 | { |
||
29 | /** |
||
30 | * @param int $deleted |
||
31 | * |
||
32 | * @return int |
||
33 | */ |
||
34 | 2 | public static function countUsers($deleted = User::NOT_DELETED_USERS) |
|
38 | |||
39 | /** |
||
40 | * Count number of assigned issues in a project. |
||
41 | * |
||
42 | * @param int $projectId |
||
43 | * |
||
44 | * @return int |
||
45 | */ |
||
46 | 19 | View Code Duplication | public function assignedIssuesCount($projectId = 0) |
57 | |||
58 | /** |
||
59 | * Count number of created issues in a project. |
||
60 | * |
||
61 | * @param int $projectId |
||
62 | * |
||
63 | * @return int |
||
64 | */ |
||
65 | 2 | View Code Duplication | public function createdIssuesCount($projectId = 0) |
76 | |||
77 | /** |
||
78 | * Returns all projects with open issue count. |
||
79 | * |
||
80 | * @param int $status |
||
81 | * |
||
82 | * @return $this |
||
83 | */ |
||
84 | 3 | public function projectsWithCountOpenIssues($status = Project::STATUS_OPEN) |
|
94 | } |
||
95 |
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.