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 |
||
27 | class ThemeMenusGenerator implements GeneratorInterface |
||
28 | { |
||
29 | /** |
||
30 | * @var RouteProviderInterface |
||
31 | */ |
||
32 | protected $routeProvider; |
||
33 | |||
34 | /** |
||
35 | * @var FormFactoryInterface |
||
36 | */ |
||
37 | protected $formFactory; |
||
38 | |||
39 | /** |
||
40 | * @var MenuFactoryInterface |
||
41 | */ |
||
42 | protected $menuFactory; |
||
43 | |||
44 | /** |
||
45 | * @var MenuItemRepositoryInterface |
||
46 | */ |
||
47 | protected $menuRepository; |
||
48 | |||
49 | /** |
||
50 | * @var MenuItemManagerInterface |
||
51 | */ |
||
52 | protected $menuManager; |
||
53 | |||
54 | /** |
||
55 | * ThemeMenusGenerator constructor. |
||
56 | * |
||
57 | * @param RouteProviderInterface $routeProvider |
||
58 | * @param FormFactoryInterface $formFactory |
||
59 | * @param MenuFactoryInterface $menuFactory |
||
60 | * @param MenuItemRepositoryInterface $menuRepository |
||
61 | * @param MenuItemManagerInterface $menuManager |
||
62 | */ |
||
63 | public function __construct(RouteProviderInterface $routeProvider, FormFactoryInterface $formFactory, MenuFactoryInterface $menuFactory, MenuItemRepositoryInterface $menuRepository, MenuItemManagerInterface $menuManager) |
||
71 | |||
72 | /** |
||
73 | * {@inheritdoc} |
||
74 | */ |
||
75 | public function generate(array $menus, MenuItemInterface $parent = null): void |
||
85 | |||
86 | /** |
||
87 | * @param array $menuData |
||
88 | * @param MenuItemInterface|null $parent |
||
89 | * |
||
90 | * @throws \Exception |
||
91 | */ |
||
92 | private function handleMenu(array $menuData, MenuItemInterface $parent = null) |
||
111 | |||
112 | /** |
||
113 | * @param array $menuData |
||
114 | * |
||
115 | * @return MenuItemInterface |
||
116 | * |
||
117 | * @throws \Exception |
||
118 | */ |
||
119 | View Code Duplication | private function createMenu(array $menuData): MenuItemInterface |
|
139 | } |
||
140 |
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.