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 namespace Modules\Menu\Repositories\Eloquent; |
||
10 | class EloquentMenuItemRepository extends EloquentBaseRepository implements MenuItemRepository |
||
11 | { |
||
12 | public function create($data) |
||
20 | |||
21 | public function update($menuItem, $data) |
||
27 | |||
28 | /** |
||
29 | * Get online root elements |
||
30 | * |
||
31 | * @param int $menuId |
||
32 | * @return object |
||
33 | */ |
||
34 | View Code Duplication | public function rootsForMenu($menuId) |
|
41 | |||
42 | /** |
||
43 | * Get all root elements |
||
44 | * |
||
45 | * @param int $menuId |
||
46 | * @return object |
||
47 | */ |
||
48 | public function allRootsForMenu($menuId) |
||
52 | |||
53 | /** |
||
54 | * Get Items to build routes |
||
55 | * |
||
56 | * @return Array |
||
57 | */ |
||
58 | public function getForRoutes() |
||
85 | |||
86 | /** |
||
87 | * Get the root menu item for the given menu id |
||
88 | * |
||
89 | * @param int $menuId |
||
90 | * @return object |
||
91 | */ |
||
92 | public function getRootForMenu($menuId) |
||
96 | |||
97 | /** |
||
98 | * Return a complete tree for the given menu id |
||
99 | * |
||
100 | * @param int $menuId |
||
101 | * @return object |
||
102 | */ |
||
103 | public function getTreeForMenu($menuId) |
||
109 | |||
110 | /** |
||
111 | * @param string $uri |
||
112 | * @param string $locale |
||
113 | * @return object |
||
114 | */ |
||
115 | public function findByUriInLanguage($uri, $locale) |
||
123 | } |
||
124 |
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.