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 |
||
14 | trait ModelSoftDeleting |
||
15 | { |
||
16 | use ModelDeleting; |
||
17 | |||
18 | /** |
||
19 | * Overrides the ModelQuerying provided method |
||
20 | * with one which searches withTrashed scope. |
||
21 | * |
||
22 | * @param int $modelitem_id |
||
23 | * |
||
24 | * @return |
||
25 | */ |
||
26 | public function find($modelitem_id) |
||
30 | |||
31 | /** |
||
32 | * Finds model and includes withTrashed() scope in query. |
||
33 | * |
||
34 | * @param int $modelitem_id |
||
35 | * |
||
36 | * @return |
||
37 | */ |
||
38 | public function findWithTrashed($modelitem_id) |
||
44 | |||
45 | /** |
||
46 | * Finds model and includes onlyTrashed() scope in query. |
||
47 | * |
||
48 | * @param int $modelitem_id |
||
49 | * |
||
50 | * @return |
||
51 | */ |
||
52 | public function findOnlyTrashed($modelitem_id) |
||
58 | |||
59 | /** |
||
60 | * Method fired before the Delete action is undertaken. |
||
61 | * |
||
62 | * @return |
||
63 | */ |
||
64 | protected function beforeDelete() |
||
67 | |||
68 | /** |
||
69 | * Delete Action. |
||
70 | * |
||
71 | * Fires off beforeDelete(), doDelete() and afterDelete() |
||
72 | * |
||
73 | * @param int $modelitem_id |
||
74 | * |
||
75 | * @return |
||
76 | */ |
||
77 | View Code Duplication | public function delete($modelitem_id) |
|
91 | |||
92 | /** |
||
93 | * The actual delete action. |
||
94 | * |
||
95 | * @return |
||
96 | */ |
||
97 | private function doDelete() |
||
109 | |||
110 | /** |
||
111 | * Method fired after the Delete action is complete. |
||
112 | * |
||
113 | * @return |
||
114 | */ |
||
115 | protected function afterDelete() |
||
118 | |||
119 | /** |
||
120 | * Method fired before the Restore action is undertaken. |
||
121 | * |
||
122 | * @return |
||
123 | */ |
||
124 | protected function beforeRestore() |
||
127 | |||
128 | /** |
||
129 | * Restore Action. |
||
130 | * |
||
131 | * Fires off beforeRestore(), doRestore() and afterRestore() |
||
132 | * |
||
133 | * @param int $modelitem_id |
||
134 | * |
||
135 | * @return |
||
136 | */ |
||
137 | public function restore($modelitem_id) |
||
151 | |||
152 | /** |
||
153 | * The actual restore action. |
||
154 | * |
||
155 | * @return |
||
156 | */ |
||
157 | private function doRestore() |
||
163 | |||
164 | /** |
||
165 | * Method fired after the Restore action is complete. |
||
166 | * |
||
167 | * @return |
||
168 | */ |
||
169 | protected function afterRestore() |
||
172 | } |
||
173 |
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.