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 implements Builder |
||
24 | { |
||
25 | use BuilderUtils; |
||
26 | |||
27 | /** |
||
28 | * @var bool |
||
29 | */ |
||
30 | private $isBuilt = false; |
||
31 | |||
32 | /** |
||
33 | * @var SplitItemBuilder[] |
||
34 | */ |
||
35 | private $splitItemBuilders = []; |
||
36 | |||
37 | /** |
||
38 | * @var SplitItem[] |
||
39 | */ |
||
40 | private $splitItems = []; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $goBackButtonText = 'Go Back'; |
||
46 | |||
47 | /** |
||
48 | * @var string |
||
49 | */ |
||
50 | private $exitButtonText = 'Exit'; |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $style; |
||
56 | |||
57 | /** |
||
58 | * @var Terminal |
||
59 | */ |
||
60 | private $terminal; |
||
61 | |||
62 | /** |
||
63 | * @var string |
||
64 | */ |
||
65 | private $menuTitle; |
||
66 | |||
67 | /** |
||
68 | * @var bool |
||
69 | */ |
||
70 | private $disableDefaultItems = false; |
||
71 | |||
72 | /** |
||
73 | * @var bool |
||
74 | */ |
||
75 | private $disabled = false; |
||
76 | |||
77 | public function __construct(Builder $parent = null) |
||
78 | { |
||
79 | $this->parent = $parent; |
||
80 | $this->terminal = $this->parent !== null |
||
81 | ? $this->parent->getTerminal() |
||
82 | : TerminalFactory::fromSystem(); |
||
83 | $this->style = MenuStyle::getDefaultStyleValues(); |
||
84 | } |
||
85 | |||
86 | public function setTitle(string $title) : self |
||
92 | |||
93 | public function addMenuItem(MenuItemInterface $item) : self |
||
99 | |||
100 | public function addItems(array $items) : self |
||
108 | |||
109 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
115 | |||
116 | /** |
||
117 | * Add a split item |
||
118 | */ |
||
119 | public function addSplitItem() : SplitItemBuilder |
||
126 | |||
127 | /** |
||
128 | * Disable a submenu |
||
129 | * |
||
130 | * @throws \InvalidArgumentException |
||
131 | */ |
||
132 | public function disableMenu() : self |
||
144 | |||
145 | public function isMenuDisabled() : bool |
||
149 | |||
150 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
156 | |||
157 | public function setExitButtonText(string $exitButtonText) : self |
||
163 | |||
164 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
174 | |||
175 | View Code Duplication | public function setForegroundColour(string $colour, string $fallback = null) : self |
|
185 | |||
186 | public function setWidth(int $width) : self |
||
192 | |||
193 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
204 | |||
205 | public function setPaddingTopBottom(int $topBottom) : self |
||
211 | |||
212 | public function setPaddingLeftRight(int $leftRight) : self |
||
218 | |||
219 | public function setMarginAuto() : self |
||
225 | |||
226 | public function setMargin(int $margin) : self |
||
233 | |||
234 | public function setUnselectedMarker(string $marker) : self |
||
240 | |||
241 | public function setSelectedMarker(string $marker) : self |
||
247 | |||
248 | public function setItemExtra(string $extra) : self |
||
254 | |||
255 | public function setTitleSeparator(string $separator) : self |
||
261 | |||
262 | public function setBorder( |
||
294 | |||
295 | public function setBorderTopWidth(int $width) : self |
||
301 | |||
302 | public function setBorderRightWidth(int $width) : self |
||
308 | |||
309 | public function setBorderBottomWidth(int $width) : self |
||
315 | |||
316 | public function setBorderLeftWidth(int $width) : self |
||
322 | |||
323 | public function setBorderColour(string $colour, $fallback = null) : self |
||
330 | |||
331 | public function setTerminal(Terminal $terminal) : self |
||
336 | |||
337 | public function getTerminal() : Terminal |
||
341 | |||
342 | private function getDefaultItems() : array |
||
352 | |||
353 | public function disableDefaultItems() : self |
||
359 | |||
360 | private function itemsHaveExtra(array $items) : bool |
||
366 | |||
367 | /** |
||
368 | * Recursively drop back to the parents menu style |
||
369 | * when the current menu has a parent and has no changes |
||
370 | */ |
||
371 | public function getMenuStyle() : MenuStyle |
||
383 | |||
384 | private function buildStyle() : MenuStyle |
||
407 | |||
408 | /** |
||
409 | * @throws RuntimeException |
||
410 | */ |
||
411 | public function getSubMenu(string $id) : CliMenu |
||
419 | |||
420 | private function buildSplitItems(array $items) : array |
||
433 | |||
434 | public function build() : CliMenu |
||
465 | } |
||
466 |
This check looks for function calls that miss required arguments.