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 |
||
20 | class Updater extends RepositoryUpdater |
||
21 | { |
||
22 | public function __construct(Project $model) |
||
26 | |||
27 | /** |
||
28 | * removes a user from a project. |
||
29 | * |
||
30 | * @param int $userId |
||
31 | * |
||
32 | * @return mixed |
||
33 | */ |
||
34 | public function unassignUser($userId) |
||
38 | |||
39 | /** |
||
40 | * Assign a user to a project. |
||
41 | * |
||
42 | * @param int $userId |
||
43 | * @param int $roleId |
||
44 | * |
||
45 | * @return Project\User |
||
46 | */ |
||
47 | View Code Duplication | public function assignUser($userId, $roleId = 0) |
|
58 | |||
59 | public function assignUsers(array $userIds) |
||
65 | |||
66 | /** |
||
67 | * Create a new project. |
||
68 | * |
||
69 | * @param array $input |
||
70 | * |
||
71 | * @return $this |
||
72 | */ |
||
73 | public function create(array $input = []) |
||
89 | |||
90 | /** |
||
91 | * Update project details. |
||
92 | * |
||
93 | * @param array $attributes |
||
94 | * |
||
95 | * @return bool |
||
96 | */ |
||
97 | public function update(array $attributes = []) |
||
103 | |||
104 | /** |
||
105 | * Delete a project. |
||
106 | * |
||
107 | * @return void |
||
108 | * |
||
109 | * @throws \Exception |
||
110 | */ |
||
111 | public function delete() |
||
115 | |||
116 | protected function deleteProject() |
||
144 | |||
145 | /** |
||
146 | * Save the project tags. |
||
147 | * |
||
148 | * @param array $tagIds |
||
149 | * |
||
150 | * @return bool |
||
151 | */ |
||
152 | protected function saveKanbanTags(array $tagIds) |
||
178 | // |
||
179 | // protected function createNote() |
||
180 | // { |
||
181 | // $this->model->notes()->create() |
||
182 | // } |
||
183 | } |
||
184 |
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.