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 | View Code Duplication | trait ManagesPages |
|
|
|||
8 | { |
||
9 | /** |
||
10 | * Add a blocks and links to model. |
||
11 | * |
||
12 | * @param HasContent $model Model to add extras to. |
||
13 | * @param array $data Array of posted user data. |
||
14 | * |
||
15 | * @return HasContent |
||
16 | */ |
||
17 | protected abstract function addModelExtras(HasContent $model, array $data); |
||
18 | |||
19 | /** |
||
20 | * Update a HasContent model and its children. |
||
21 | * |
||
22 | * @param HasContent $model Model to update. |
||
23 | * @param array $data Array of posted user data. |
||
24 | * |
||
25 | * @return HasContent |
||
26 | */ |
||
27 | protected abstract function updateModel(HasContent $model, array $data); |
||
28 | |||
29 | /** |
||
30 | * Permanently delete a model. |
||
31 | * |
||
32 | * @param HasContent $model Model to delete. |
||
33 | * |
||
34 | * @return boolean |
||
35 | */ |
||
36 | protected abstract function purgeModel(HasContent $model); |
||
37 | |||
38 | /** |
||
39 | * Add a page to the portfolio. |
||
40 | * |
||
41 | * @param array $data Array of data to save. |
||
42 | * |
||
43 | * @return Project |
||
44 | */ |
||
45 | public function addPage(array $data) |
||
53 | |||
54 | /** |
||
55 | * Update a page. |
||
56 | * |
||
57 | * @param Page $page Page to update. |
||
58 | * @param array $data Array of data to save. |
||
59 | * |
||
60 | * @return Page |
||
61 | */ |
||
62 | public function updatePage(Page $page, array $data) |
||
66 | |||
67 | /** |
||
68 | * Remove a page. |
||
69 | * |
||
70 | * @param Page $page Page to remove. |
||
71 | * |
||
72 | * @return bool|null |
||
73 | */ |
||
74 | public function removePage(Page $page) |
||
78 | |||
79 | /** |
||
80 | * Restore a soft deleted page. |
||
81 | * |
||
82 | * @param Page $page Page to restore. |
||
83 | * |
||
84 | * @return bool|null |
||
85 | */ |
||
86 | public function restorePage(Page $page) |
||
92 | |||
93 | /** |
||
94 | * Hard delete a page from the portfolio. |
||
95 | * |
||
96 | * @param Page $page Page to purge. |
||
97 | * |
||
98 | * @return bool|null |
||
99 | */ |
||
100 | public function purgePage(Page $page) |
||
104 | |||
105 | /** |
||
106 | * Update the order of pages in the portfolio. |
||
107 | * |
||
108 | * @param array $data Array of data for pages. |
||
109 | */ |
||
110 | public function updatePageOrder(array $data) |
||
120 | } |
||
121 |
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.