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 |
||
10 | trait ModelDeleting |
||
11 | { |
||
12 | /** |
||
13 | * Method fired before the Delete action is undertaken. |
||
14 | * |
||
15 | * @return |
||
16 | */ |
||
17 | protected function beforeDelete() |
||
20 | |||
21 | /** |
||
22 | * Delete Action. |
||
23 | * |
||
24 | * Fires off beforeDelete(), doDelete() and afterDelete() |
||
25 | * |
||
26 | * @param int $modelitemId |
||
27 | * |
||
28 | * @return |
||
29 | */ |
||
30 | View Code Duplication | public function delete($modelitemId) |
|
44 | |||
45 | /** |
||
46 | * The actual delete action. |
||
47 | * |
||
48 | * @return |
||
49 | */ |
||
50 | private function doDelete() |
||
56 | |||
57 | /** |
||
58 | * Method fired after the Delete action is complete. |
||
59 | * |
||
60 | * @return |
||
61 | */ |
||
62 | protected function afterDelete() |
||
65 | } |
||
66 |
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.