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:
Complex classes like CliMenuBuilder often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CliMenuBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class CliMenuBuilder |
||
24 | { |
||
25 | /** |
||
26 | * @var bool |
||
27 | */ |
||
28 | private $isBuilt = false; |
||
29 | |||
30 | /** |
||
31 | * @var null|self |
||
32 | */ |
||
33 | private $parent; |
||
34 | |||
35 | private $previousBuilder = null; |
||
36 | |||
37 | /** |
||
38 | * @var self[] |
||
39 | */ |
||
40 | private $subMenuBuilders = []; |
||
41 | |||
42 | /** |
||
43 | * @var CliMenu[] |
||
44 | */ |
||
45 | private $subMenus = []; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $goBackButtonText = 'Go Back'; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $exitButtonText = 'Exit'; |
||
56 | |||
57 | /** |
||
58 | * @var array |
||
59 | */ |
||
60 | private $menuItems = []; |
||
61 | |||
62 | /** |
||
63 | * @var array |
||
64 | */ |
||
65 | private $style; |
||
66 | |||
67 | /** |
||
68 | * @var Terminal |
||
69 | */ |
||
70 | private $terminal; |
||
71 | |||
72 | /** |
||
73 | * @var string |
||
74 | */ |
||
75 | private $menuTitle = null; |
||
76 | |||
77 | /** |
||
78 | * @var bool |
||
79 | */ |
||
80 | private $disableDefaultItems = false; |
||
81 | |||
82 | /** |
||
83 | * @var bool |
||
84 | */ |
||
85 | private $disabled = false; |
||
86 | |||
87 | public function __construct(CliMenuBuilder $parent = null, $previousBuilder = null) |
||
96 | |||
97 | public function setTitle(string $title) : self |
||
103 | |||
104 | public function addMenuItem(MenuItemInterface $item) : self |
||
110 | |||
111 | public function addItem( |
||
121 | |||
122 | public function addItems(array $items) : self |
||
130 | |||
131 | public function addStaticItem(string $text) : self |
||
137 | |||
138 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
144 | |||
145 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
151 | |||
152 | /** |
||
153 | * Add a submenu with a string identifier |
||
154 | */ |
||
155 | public function addSubMenu(string $id, CliMenuBuilder $subMenuBuilder = null) : CliMenuBuilder |
||
167 | |||
168 | /** |
||
169 | * Injects a submenu directly (without going through the builder |
||
170 | */ |
||
171 | public function injectSubMenu(string $id, CliMenu $subMenu) : CliMenuBuilder |
||
177 | |||
178 | /** |
||
179 | * Add a split item |
||
180 | */ |
||
181 | public function addSplitItem() : SplitItem |
||
188 | |||
189 | /** |
||
190 | * Disable a submenu |
||
191 | * |
||
192 | * @throws \InvalidArgumentException |
||
193 | */ |
||
194 | public function disableMenu() : self |
||
206 | |||
207 | public function isMenuDisabled() : bool |
||
211 | |||
212 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
218 | |||
219 | public function setExitButtonText(string $exitButtonText) : self |
||
225 | |||
226 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
236 | |||
237 | View Code Duplication | public function setForegroundColour(string $colour, string $fallback = null) : self |
|
247 | |||
248 | public function setWidth(int $width) : self |
||
254 | |||
255 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
266 | |||
267 | public function setPaddingTopBottom(int $topBottom) : self |
||
273 | |||
274 | public function setPaddingLeftRight(int $leftRight) : self |
||
280 | |||
281 | public function setMarginAuto() : self |
||
287 | |||
288 | public function setMargin(int $margin) : self |
||
295 | |||
296 | public function setUnselectedMarker(string $marker) : self |
||
302 | |||
303 | public function setSelectedMarker(string $marker) : self |
||
309 | |||
310 | public function setItemExtra(string $extra) : self |
||
316 | |||
317 | public function setTitleSeparator(string $separator) : self |
||
323 | |||
324 | public function setBorder( |
||
356 | |||
357 | public function setBorderTopWidth(int $width) : self |
||
363 | |||
364 | public function setBorderRightWidth(int $width) : self |
||
370 | |||
371 | public function setBorderBottomWidth(int $width) : self |
||
377 | |||
378 | public function setBorderLeftWidth(int $width) : self |
||
384 | |||
385 | public function setBorderColour(string $colour, $fallback = null) : self |
||
392 | |||
393 | public function setTerminal(Terminal $terminal) : self |
||
398 | |||
399 | public function getTerminal() : Terminal |
||
403 | |||
404 | private function getDefaultItems() : array |
||
414 | |||
415 | public function disableDefaultItems() : self |
||
421 | |||
422 | private function itemsHaveExtra(array $items) : bool |
||
428 | |||
429 | /** |
||
430 | * Recursively drop back to the parents menu style |
||
431 | * when the current menu has a parent and has no changes |
||
432 | */ |
||
433 | private function getMenuStyle() : MenuStyle |
||
445 | |||
446 | private function buildStyle() : MenuStyle |
||
469 | |||
470 | /** |
||
471 | * Return to parent builder |
||
472 | * |
||
473 | * @throws RuntimeException |
||
474 | */ |
||
475 | public function end() |
||
487 | |||
488 | /** |
||
489 | * @throws RuntimeException |
||
490 | */ |
||
491 | public function getSubMenu(string $id) : CliMenu |
||
499 | |||
500 | private function buildSubMenus(array $items) : array |
||
513 | |||
514 | public function build() : CliMenu |
||
539 | } |
||
540 |
This check looks for function calls that miss required arguments.