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 |
||
22 | class CliMenuBuilder implements Builder |
||
23 | { |
||
24 | use BuilderUtils; |
||
25 | |||
26 | /** |
||
27 | * @var bool |
||
28 | */ |
||
29 | private $isBuilt = false; |
||
30 | |||
31 | /** |
||
32 | * @var SplitItemBuilder[] |
||
33 | */ |
||
34 | private $splitItemBuilders = []; |
||
35 | |||
36 | /** |
||
37 | * @var SplitItem[] |
||
38 | */ |
||
39 | private $splitItems = []; |
||
40 | |||
41 | /** |
||
42 | * @var string |
||
43 | */ |
||
44 | private $goBackButtonText = 'Go Back'; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $exitButtonText = 'Exit'; |
||
50 | |||
51 | /** |
||
52 | * @var array |
||
53 | */ |
||
54 | private $style; |
||
55 | |||
56 | /** |
||
57 | * @var Terminal |
||
58 | */ |
||
59 | private $terminal; |
||
60 | |||
61 | /** |
||
62 | * @var string |
||
63 | */ |
||
64 | private $menuTitle; |
||
65 | |||
66 | /** |
||
67 | * @var bool |
||
68 | */ |
||
69 | private $disableDefaultItems = false; |
||
70 | |||
71 | /** |
||
72 | * @var bool |
||
73 | */ |
||
74 | private $disabled = false; |
||
75 | |||
76 | public function __construct(Builder $parent = null) |
||
84 | |||
85 | public function setTitle(string $title) : self |
||
91 | |||
92 | public function addMenuItem(MenuItemInterface $item) : self |
||
98 | |||
99 | public function addItems(array $items) : self |
||
107 | |||
108 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
114 | |||
115 | /** |
||
116 | * Add a split item |
||
117 | */ |
||
118 | public function addSplitItem() : SplitItemBuilder |
||
125 | |||
126 | /** |
||
127 | * Disable a submenu |
||
128 | * |
||
129 | * @throws \InvalidArgumentException |
||
130 | */ |
||
131 | public function disableMenu() : self |
||
143 | |||
144 | public function isMenuDisabled() : bool |
||
148 | |||
149 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
155 | |||
156 | public function setExitButtonText(string $exitButtonText) : self |
||
162 | |||
163 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
173 | |||
174 | View Code Duplication | public function setForegroundColour(string $colour, string $fallback = null) : self |
|
184 | |||
185 | public function setWidth(int $width) : self |
||
191 | |||
192 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
203 | |||
204 | public function setPaddingTopBottom(int $topBottom) : self |
||
210 | |||
211 | public function setPaddingLeftRight(int $leftRight) : self |
||
217 | |||
218 | public function setMarginAuto() : self |
||
224 | |||
225 | public function setMargin(int $margin) : self |
||
232 | |||
233 | public function setUnselectedMarker(string $marker) : self |
||
239 | |||
240 | public function setSelectedMarker(string $marker) : self |
||
246 | |||
247 | public function setItemExtra(string $extra) : self |
||
253 | |||
254 | public function setTitleSeparator(string $separator) : self |
||
260 | |||
261 | public function setBorder( |
||
293 | |||
294 | public function setBorderTopWidth(int $width) : self |
||
300 | |||
301 | public function setBorderRightWidth(int $width) : self |
||
307 | |||
308 | public function setBorderBottomWidth(int $width) : self |
||
314 | |||
315 | public function setBorderLeftWidth(int $width) : self |
||
321 | |||
322 | public function setBorderColour(string $colour, $fallback = null) : self |
||
329 | |||
330 | public function setTerminal(Terminal $terminal) : self |
||
335 | |||
336 | public function getTerminal() : Terminal |
||
340 | |||
341 | private function getDefaultItems() : array |
||
351 | |||
352 | public function disableDefaultItems() : self |
||
358 | |||
359 | private function itemsHaveExtra(array $items) : bool |
||
365 | |||
366 | /** |
||
367 | * Recursively drop back to the parents menu style |
||
368 | * when the current menu has a parent and has no changes |
||
369 | */ |
||
370 | public function getMenuStyle() : MenuStyle |
||
382 | |||
383 | private function buildStyle() : MenuStyle |
||
406 | |||
407 | /** |
||
408 | * @throws RuntimeException |
||
409 | */ |
||
410 | public function getSubMenu(string $id) : CliMenu |
||
418 | |||
419 | private function buildSplitItems(array $items) : array |
||
432 | |||
433 | public function build() : CliMenu |
||
464 | } |
||
465 |
This check looks for function calls that miss required arguments.