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 |
||
12 | class MenuBuilder |
||
13 | { |
||
14 | /** |
||
15 | * @var MenuAdaptorInterface[] |
||
16 | */ |
||
17 | private $adaptors = array(); |
||
18 | |||
19 | /** |
||
20 | * @var MenuAdaptorInterface[] |
||
21 | */ |
||
22 | private $sorted = array(); |
||
23 | |||
24 | /** |
||
25 | * @var TopMenuItem[] |
||
26 | */ |
||
27 | private $topMenuItems = null; |
||
28 | |||
29 | /** |
||
30 | * @var ContainerInterface |
||
31 | */ |
||
32 | private $container; |
||
33 | |||
34 | /** |
||
35 | * @var MenuItem|null |
||
36 | */ |
||
37 | private $currentCache = null; |
||
38 | |||
39 | /** @var RequestStack */ |
||
40 | private $requestStack; |
||
41 | |||
42 | /** |
||
43 | * @param ContainerInterface|RequestStack $requestStack |
||
44 | */ |
||
45 | 7 | View Code Duplication | public function __construct(/* RequestStack */ $requestStack) |
58 | |||
59 | /** |
||
60 | * Add menu adaptor |
||
61 | * |
||
62 | * @param MenuAdaptorInterface $adaptor |
||
63 | */ |
||
64 | 2 | public function addAdaptMenu(MenuAdaptorInterface $adaptor, $priority = 0) |
|
69 | |||
70 | /** |
||
71 | * Get current menu item |
||
72 | * |
||
73 | * @return MenuItem|null |
||
74 | */ |
||
75 | 4 | public function getCurrent() |
|
99 | |||
100 | /** |
||
101 | * Get breadcrumb path for current menu item |
||
102 | * |
||
103 | * @return MenuItem[] |
||
104 | */ |
||
105 | 1 | public function getBreadCrumb() |
|
116 | |||
117 | /** |
||
118 | * Get top parent menu of current menu item |
||
119 | * |
||
120 | * @return TopMenuItem|null |
||
121 | */ |
||
122 | 3 | public function getLowestTopChild() |
|
134 | |||
135 | /** |
||
136 | * Get all top menu items |
||
137 | * |
||
138 | * @return MenuItem[] |
||
139 | */ |
||
140 | 2 | public function getTopChildren() |
|
153 | |||
154 | /** |
||
155 | * Get immediate children of the specified menu item |
||
156 | * |
||
157 | * @param MenuItem $parent |
||
158 | * |
||
159 | * @return MenuItem[] |
||
160 | */ |
||
161 | 1 | public function getChildren(MenuItem $parent = null) |
|
175 | |||
176 | 2 | private function getAdaptors() |
|
184 | |||
185 | /** |
||
186 | * Sorts the internal list of adaptors by priority. |
||
187 | */ |
||
188 | 2 | View Code Duplication | private function sortAdaptors() |
197 | } |
||
198 |
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.