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 |
||
11 | class MenuBuilder |
||
12 | { |
||
13 | /** |
||
14 | * @var MenuAdaptorInterface[] |
||
15 | */ |
||
16 | private $adaptors = array(); |
||
17 | |||
18 | /** |
||
19 | * @var MenuAdaptorInterface[] |
||
20 | */ |
||
21 | private $sorted = array(); |
||
22 | |||
23 | /** |
||
24 | * @var TopMenuItem[] |
||
25 | */ |
||
26 | private $topMenuItems = null; |
||
27 | |||
28 | /** |
||
29 | * @var ContainerInterface |
||
30 | */ |
||
31 | private $container; |
||
32 | |||
33 | /** |
||
34 | * @var MenuItem|null |
||
35 | */ |
||
36 | private $currentCache = null; |
||
37 | |||
38 | /** |
||
39 | * Constructor |
||
40 | * |
||
41 | * @param ContainerInterface $container The container |
||
42 | */ |
||
43 | 6 | public function __construct(ContainerInterface $container) |
|
47 | |||
48 | /** |
||
49 | * Add menu adaptor |
||
50 | * |
||
51 | * @param MenuAdaptorInterface $adaptor |
||
52 | */ |
||
53 | 2 | public function addAdaptMenu(MenuAdaptorInterface $adaptor, $priority = 0) |
|
58 | |||
59 | /** |
||
60 | * Get current menu item |
||
61 | * |
||
62 | * @return MenuItem|null |
||
63 | */ |
||
64 | 4 | public function getCurrent() |
|
88 | |||
89 | /** |
||
90 | * Get breadcrumb path for current menu item |
||
91 | * |
||
92 | * @return MenuItem[] |
||
93 | */ |
||
94 | 1 | public function getBreadCrumb() |
|
105 | |||
106 | /** |
||
107 | * Get top parent menu of current menu item |
||
108 | * |
||
109 | * @return TopMenuItem|null |
||
110 | */ |
||
111 | 3 | public function getLowestTopChild() |
|
123 | |||
124 | /** |
||
125 | * Get all top menu items |
||
126 | * |
||
127 | * @return MenuItem[] |
||
128 | */ |
||
129 | 2 | public function getTopChildren() |
|
142 | |||
143 | /** |
||
144 | * Get immediate children of the specified menu item |
||
145 | * |
||
146 | * @param MenuItem $parent |
||
147 | * |
||
148 | * @return MenuItem[] |
||
149 | */ |
||
150 | 1 | public function getChildren(MenuItem $parent = null) |
|
164 | |||
165 | 2 | private function getAdaptors() |
|
173 | |||
174 | /** |
||
175 | * Sorts the internal list of adaptors by priority. |
||
176 | */ |
||
177 | 2 | View Code Duplication | private function sortAdaptors() |
186 | } |
||
187 |
There are different options of fixing this problem.
If you want to be on the safe side, you can add an additional type-check:
If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:
Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.