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\Cache; |
||
6 | class CacheMenuItemDecorator extends BaseCacheDecorator implements MenuItemRepository |
||
7 | { |
||
8 | /** |
||
9 | * @var MenuItemRepository |
||
10 | */ |
||
11 | protected $repository; |
||
12 | |||
13 | public function __construct(MenuItemRepository $menuItem) |
||
19 | |||
20 | /** |
||
21 | * Get all root elements |
||
22 | * |
||
23 | * @param int $menuId |
||
24 | * @return mixed |
||
25 | */ |
||
26 | View Code Duplication | public function rootsForMenu($menuId) |
|
36 | |||
37 | /** |
||
38 | * Get the menu items ready for routes |
||
39 | * |
||
40 | * @return mixed |
||
41 | */ |
||
42 | public function getForRoutes() |
||
52 | |||
53 | /** |
||
54 | * Get the root menu item for the given menu id |
||
55 | * |
||
56 | * @param int $menuId |
||
57 | * @return object |
||
58 | */ |
||
59 | View Code Duplication | public function getRootForMenu($menuId) |
|
69 | |||
70 | /** |
||
71 | * Return a complete tree for the given menu id |
||
72 | * |
||
73 | * @param int $menuId |
||
74 | * @return object |
||
75 | */ |
||
76 | View Code Duplication | public function getTreeForMenu($menuId) |
|
86 | |||
87 | /** |
||
88 | * Get all root elements |
||
89 | * |
||
90 | * @param int $menuId |
||
91 | * @return object |
||
92 | */ |
||
93 | View Code Duplication | public function allRootsForMenu($menuId) |
|
103 | |||
104 | /** |
||
105 | * @param string $uri |
||
106 | * @param string $locale |
||
107 | * @return object |
||
108 | */ |
||
109 | View Code Duplication | public function findByUriInLanguage($uri, $locale) |
|
119 | } |
||
120 |
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.