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 | trait ManagesPages |
||
8 | { |
||
9 | /** |
||
10 | * Add a page to the portfolio. |
||
11 | * |
||
12 | * @param array $data Array of data to save. |
||
13 | * |
||
14 | * @return Project |
||
15 | */ |
||
16 | View Code Duplication | public function addPage(array $data) |
|
32 | |||
33 | /** |
||
34 | * Update a page. |
||
35 | * |
||
36 | * @param Page $page Page to update. |
||
37 | * @param array $data Array of data to save. |
||
38 | * |
||
39 | * @return Page |
||
40 | */ |
||
41 | public function updatePage(Page $page, array $data) |
||
51 | |||
52 | /** |
||
53 | * Remove a page. |
||
54 | * |
||
55 | * @param Page $page Page to remove. |
||
56 | * |
||
57 | * @return bool|null |
||
58 | */ |
||
59 | public function removePage(Page $page) |
||
63 | |||
64 | /** |
||
65 | * Restore a soft deleted page. |
||
66 | * |
||
67 | * @param Page $page Page to restore. |
||
68 | * |
||
69 | * @return bool|null |
||
70 | */ |
||
71 | public function restorePage(Page $page) |
||
77 | |||
78 | /** |
||
79 | * Hard delete a page from the portfolio. |
||
80 | * |
||
81 | * @param Page $page Page to purge. |
||
82 | * |
||
83 | * @return bool|null |
||
84 | */ |
||
85 | public function purgePage(Page $page) |
||
95 | } |
||
96 |
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.