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 |
||
14 | trait BuilderUtils |
||
15 | { |
||
16 | /** |
||
17 | * @var self[] |
||
18 | */ |
||
19 | private $subMenuBuilders = []; |
||
20 | |||
21 | /** |
||
22 | * @var CliMenu[] |
||
23 | */ |
||
24 | private $subMenus = []; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | private $menuItems = []; |
||
30 | |||
31 | public function addItem( |
||
41 | |||
42 | public function addStaticItem(string $text) : self |
||
48 | |||
49 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
55 | |||
56 | /** |
||
57 | * Add a submenu with a unique ID and the text. The text will be displayed as the item text |
||
58 | * in the parent menu. |
||
59 | */ |
||
60 | View Code Duplication | public function addSubMenu(string $id, string $text) : CliMenuBuilder |
|
77 | |||
78 | /** |
||
79 | * Add a submenu from an existing builder. Required a unique ID and the text. The text will be displayed as the |
||
80 | * item text in the parent menu. |
||
81 | */ |
||
82 | View Code Duplication | public function addSubMenuFromExistingBuilder(string $id, string $text, CliMenuBuilder $subMenuBuilder) : self |
|
99 | |||
100 | private function buildSubMenus(array $items) : array |
||
117 | } |
||
118 |
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.