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 |
||
| 7 | trait ManagesProjects |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * Add a project to the portfolio. |
||
| 11 | * |
||
| 12 | * @param array $data Array of data to save. |
||
| 13 | * |
||
| 14 | * @return Project |
||
| 15 | */ |
||
| 16 | View Code Duplication | public function addProject(array $data) |
|
| 32 | |||
| 33 | /** |
||
| 34 | * Update a project in the portfolio. |
||
| 35 | * |
||
| 36 | * @param Project $project Project to update. |
||
| 37 | * @param array $data Array of data to save. |
||
| 38 | * |
||
| 39 | * @return Project |
||
| 40 | */ |
||
| 41 | public function updateProject(Project $project, array $data) |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Remove a project from the portfolio. |
||
| 54 | * |
||
| 55 | * @param Project $project Project to remove. |
||
| 56 | * |
||
| 57 | * @return bool|null |
||
| 58 | */ |
||
| 59 | public function removeProject(Project $project) |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Restore a soft deleted project. |
||
| 66 | * |
||
| 67 | * @param Project $project Project to restore. |
||
| 68 | * |
||
| 69 | * @return bool|null |
||
| 70 | */ |
||
| 71 | public function restoreProject(Project $project) |
||
| 77 | |||
| 78 | /** |
||
| 79 | * Hard delete a project from the portfolio. |
||
| 80 | * |
||
| 81 | * @param Project $project Project to purge. |
||
| 82 | * |
||
| 83 | * @return bool|null |
||
| 84 | */ |
||
| 85 | public function purgeProject(Project $project) |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Update the order of projects in the portfolio. |
||
| 98 | * |
||
| 99 | * @param array $data Array of data for projects. |
||
| 100 | */ |
||
| 101 | public function updateProjectOrder(array $data) |
||
| 111 | } |
||
| 112 |
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.