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 |
||
| 10 | class AdmingeneratorMenuBuilder |
||
| 11 | { |
||
| 12 | /** |
||
| 13 | * @var array |
||
| 14 | */ |
||
| 15 | protected $dividers = array(); |
||
| 16 | |||
| 17 | /** |
||
| 18 | * @var string |
||
| 19 | */ |
||
| 20 | protected $translation_domain = 'Admingenerator'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var FactoryInterface |
||
| 24 | */ |
||
| 25 | protected $factory; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var RequestStack |
||
| 29 | */ |
||
| 30 | protected $requestStack; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string |
||
| 34 | */ |
||
| 35 | protected $dashboardRoute; |
||
| 36 | |||
| 37 | public function __construct(FactoryInterface $factory, RequestStack $requestStack, $dashboardRoute) |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Creates link to uri element and adds it to menu |
||
| 46 | * |
||
| 47 | * @param \Knp\Menu\ItemInterface $menu |
||
| 48 | * @param string $label Link label |
||
| 49 | * @param string $uri |
||
| 50 | * @return ItemInterface Link element |
||
| 51 | */ |
||
| 52 | View Code Duplication | protected function addLinkURI(ItemInterface $menu, $label, $uri) |
|
| 63 | |||
| 64 | /** |
||
| 65 | * Creates link to route element and adds it to menu |
||
| 66 | * |
||
| 67 | * @param \Knp\Menu\ItemInterface $menu |
||
| 68 | * @param string $label Link label |
||
| 69 | * @param string $route Link route |
||
| 70 | * @param array $routeParameters Route parameters |
||
| 71 | * @return ItemInterface Link element |
||
| 72 | */ |
||
| 73 | View Code Duplication | protected function addLinkRoute(ItemInterface $menu, $label, $route, $routeParameters = array()) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Set active class to current item and all its parents (so it is automatically opened) |
||
| 87 | * |
||
| 88 | * @param ItemInterface $item |
||
| 89 | */ |
||
| 90 | protected function setActive(ItemInterface $item = null) |
||
| 97 | |||
| 98 | /** |
||
| 99 | * Creates dropdown menu element and adds it to menu |
||
| 100 | * |
||
| 101 | * @param \Knp\Menu\ItemInterface $menu |
||
| 102 | * @param string $label Dropdown label |
||
| 103 | * @param bool $caret Wheather or not append caret |
||
| 104 | * @return ItemInterface Dropdown element |
||
| 105 | */ |
||
| 106 | protected function addDropdown(ItemInterface $menu, $label, $caret = true) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $uri |
||
| 118 | * @return bool |
||
| 119 | */ |
||
| 120 | protected function isCurrentUri($uri) |
||
| 126 | } |
||
| 127 |
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.