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 ModelCreating |
||
11 | { |
||
12 | /** |
||
13 | * Method fired before the Create action is undertaken. |
||
14 | * |
||
15 | * @return |
||
16 | */ |
||
17 | protected function beforeCreate() |
||
20 | |||
21 | /** |
||
22 | * Create Action. |
||
23 | * |
||
24 | * Fires off beforeCreate(), doCreate() and afterCreate() |
||
25 | * |
||
26 | * @return |
||
27 | */ |
||
28 | public function create() |
||
40 | |||
41 | /** |
||
42 | * The actual Create action, which does all of hte pre-processing |
||
43 | * required before we are able to perform the save() function. |
||
44 | * |
||
45 | * @return |
||
46 | */ |
||
47 | View Code Duplication | private function doCreate() |
|
59 | |||
60 | /** |
||
61 | * Method fired after the Create action is complete. |
||
62 | * |
||
63 | * @return |
||
64 | */ |
||
65 | protected function afterCreate() |
||
68 | } |
||
69 |
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.