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 |
||
18 | class MainMenu extends Widget |
||
19 | { |
||
20 | /** |
||
21 | * Main menu item config. |
||
22 | * |
||
23 | * @var MainMenuItem[] |
||
24 | */ |
||
25 | protected $menuItems = []; |
||
26 | |||
27 | /** |
||
28 | * @return MainMenuItem[] |
||
29 | */ |
||
30 | public function getMenuItems() |
||
34 | |||
35 | /** |
||
36 | * Main menu items setter. |
||
37 | * |
||
38 | * @param array|MainMenuItem[] $menuItems menu items config. |
||
39 | * |
||
40 | * @return $this |
||
41 | */ |
||
42 | public function setMenuItems(array $menuItems) |
||
50 | |||
51 | /** |
||
52 | * Create a MenuItem instance and fill menu items array. |
||
53 | * |
||
54 | * @param array|MainMenuItem $item |
||
55 | * |
||
56 | * @return void |
||
57 | */ |
||
58 | View Code Duplication | public function addMenuItem($item) |
|
68 | |||
69 | /** |
||
70 | * Returns main menu template with rendered menu items. |
||
71 | * |
||
72 | * @return string. |
||
73 | */ |
||
74 | public function run() |
||
80 | } |
||
81 |
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.