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 |
||
25 | class MainMenuItem extends Widget |
||
26 | { |
||
27 | /** |
||
28 | * Css-class for icon of menu item. |
||
29 | * Example: fa fa-database. |
||
30 | * |
||
31 | * @var string |
||
32 | */ |
||
33 | protected $icon = ''; |
||
34 | |||
35 | /** |
||
36 | * Is menu item need to be opened (if has subitems) or selected. |
||
37 | * |
||
38 | * @var bool |
||
39 | */ |
||
40 | protected $active = false; |
||
41 | |||
42 | /** |
||
43 | * Is menu item need to be displayed. |
||
44 | * |
||
45 | * @var bool |
||
46 | */ |
||
47 | protected $display = true; |
||
48 | |||
49 | /** |
||
50 | * Title of menu item. |
||
51 | * |
||
52 | * @var string |
||
53 | */ |
||
54 | protected $title = ''; |
||
55 | |||
56 | /** |
||
57 | * Array of subItems menu items. |
||
58 | * |
||
59 | * @var MainMenu[] |
||
60 | */ |
||
61 | protected $subItems = []; |
||
62 | |||
63 | /** |
||
64 | * Url of menu item. |
||
65 | * |
||
66 | * @var string |
||
67 | */ |
||
68 | protected $url = ''; |
||
69 | |||
70 | /** |
||
71 | * Class for <li> tag. |
||
72 | * |
||
73 | * @var string |
||
74 | */ |
||
75 | protected $class; |
||
76 | |||
77 | /** |
||
78 | * Executes the widget. |
||
79 | * |
||
80 | * @return string the result of widget execution to be outputted. |
||
81 | */ |
||
82 | public function run(): string |
||
98 | |||
99 | /** |
||
100 | * Icon getter. |
||
101 | * |
||
102 | * @return string |
||
103 | */ |
||
104 | public function getIcon(): string |
||
108 | |||
109 | /** |
||
110 | * Icon setter. |
||
111 | * |
||
112 | * @param string $icon Icon of menu item |
||
113 | * |
||
114 | * @return $this |
||
115 | */ |
||
116 | public function setIcon($icon) |
||
121 | |||
122 | /** |
||
123 | * Url getter. |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | public function getUrl(): string |
||
131 | |||
132 | /** |
||
133 | * Url setter. |
||
134 | * |
||
135 | * @param string $url Url of menu item |
||
136 | * |
||
137 | * @return $this |
||
138 | */ |
||
139 | public function setUrl($url) |
||
144 | |||
145 | /** |
||
146 | * Get class for <li> tag. |
||
147 | * |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getClass(): string |
||
154 | |||
155 | /** |
||
156 | * Is menu item need to be opened (if has subitems) or selected. |
||
157 | * |
||
158 | * @return bool |
||
159 | */ |
||
160 | public function isActive(): bool |
||
164 | |||
165 | /** |
||
166 | * Set menu item need to be opened (if has subitems) or selected. |
||
167 | * |
||
168 | * @param bool $active |
||
169 | * |
||
170 | * @return $this |
||
171 | */ |
||
172 | public function setActive($active) |
||
177 | |||
178 | /** |
||
179 | * Is menu item need to be displayed. |
||
180 | * |
||
181 | * @return bool |
||
182 | */ |
||
183 | public function isDisplay() |
||
187 | |||
188 | /** |
||
189 | * Set menu item need to be displayed. |
||
190 | * |
||
191 | * @param bool $display |
||
192 | * |
||
193 | * @return $this |
||
194 | */ |
||
195 | public function setDisplay($display) |
||
200 | |||
201 | /** |
||
202 | * Get title of menu item. |
||
203 | * |
||
204 | * @return string |
||
205 | */ |
||
206 | public function getTitle(): string |
||
210 | |||
211 | /** |
||
212 | * Set title of menu item. |
||
213 | * |
||
214 | * @param string $title |
||
215 | * |
||
216 | * @return $this |
||
217 | */ |
||
218 | public function setTitle($title) |
||
223 | |||
224 | /** |
||
225 | * Get array of subItems menu items. |
||
226 | * |
||
227 | * @return MainMenu[] |
||
228 | */ |
||
229 | public function getSubItems() |
||
233 | |||
234 | /** |
||
235 | * Set array of subItems menu items. |
||
236 | * |
||
237 | * @param array|MainMenu[] $subItems |
||
238 | * |
||
239 | * @throws InvalidConfigException |
||
240 | * |
||
241 | * @return $this |
||
242 | */ |
||
243 | public function setSubItems(array $subItems) |
||
252 | |||
253 | /** |
||
254 | * Add to array of subItems menu items. |
||
255 | * |
||
256 | * @param array|MainMenu $menuItem |
||
257 | * |
||
258 | * @throws InvalidConfigException |
||
259 | * |
||
260 | * @return $this |
||
261 | */ |
||
262 | View Code Duplication | public function addSubItems($menuItem) |
|
275 | |||
276 | /** |
||
277 | * Check if item has subitems. |
||
278 | * |
||
279 | * @return bool |
||
280 | */ |
||
281 | public function hasSubItems() |
||
285 | } |
||
286 |
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.