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 null|Builder |
||
28 | */ |
||
29 | private $parent; |
||
30 | |||
31 | /** |
||
32 | * @var bool |
||
33 | */ |
||
34 | private $isBuilt = false; |
||
35 | |||
36 | /** |
||
37 | * @var SplitItemBuilder[] |
||
38 | */ |
||
39 | private $splitItemBuilders = []; |
||
40 | |||
41 | /** |
||
42 | * @var SplitItem[] |
||
43 | */ |
||
44 | private $splitItems = []; |
||
45 | |||
46 | /** |
||
47 | * @var string |
||
48 | */ |
||
49 | private $goBackButtonText = 'Go Back'; |
||
50 | |||
51 | /** |
||
52 | * @var string |
||
53 | */ |
||
54 | private $exitButtonText = 'Exit'; |
||
55 | |||
56 | /** |
||
57 | * @var array |
||
58 | */ |
||
59 | private $style; |
||
60 | |||
61 | /** |
||
62 | * @var Terminal |
||
63 | */ |
||
64 | private $terminal; |
||
65 | |||
66 | /** |
||
67 | * @var string |
||
68 | */ |
||
69 | private $menuTitle; |
||
70 | |||
71 | /** |
||
72 | * @var bool |
||
73 | */ |
||
74 | private $disableDefaultItems = false; |
||
75 | |||
76 | /** |
||
77 | * @var bool |
||
78 | */ |
||
79 | private $disabled = false; |
||
80 | |||
81 | public function __construct(Builder $parent = null) |
||
89 | |||
90 | public function setTitle(string $title) : self |
||
96 | |||
97 | public function addMenuItem(MenuItemInterface $item) : self |
||
103 | |||
104 | public function addItems(array $items) : self |
||
112 | |||
113 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
119 | |||
120 | /** |
||
121 | * Add a split item |
||
122 | */ |
||
123 | public function addSplitItem() : SplitItemBuilder |
||
133 | |||
134 | /** |
||
135 | * Disable a submenu |
||
136 | * |
||
137 | * @throws \InvalidArgumentException |
||
138 | */ |
||
139 | public function disableMenu() : self |
||
151 | |||
152 | public function isMenuDisabled() : bool |
||
156 | |||
157 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
163 | |||
164 | public function setExitButtonText(string $exitButtonText) : self |
||
170 | |||
171 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
181 | |||
182 | public function setForegroundColour(string $colour, string $fallback = null) : self |
||
192 | |||
193 | public function setWidth(int $width) : self |
||
199 | |||
200 | public function setPadding(int $topBottom, int $leftRight = null) : self |
||
211 | |||
212 | public function setPaddingTopBottom(int $topBottom) : self |
||
218 | |||
219 | public function setPaddingLeftRight(int $leftRight) : self |
||
225 | |||
226 | public function setMarginAuto() : self |
||
232 | |||
233 | public function setMargin(int $margin) : self |
||
240 | |||
241 | public function setUnselectedMarker(string $marker) : self |
||
247 | |||
248 | public function setSelectedMarker(string $marker) : self |
||
254 | |||
255 | public function setItemExtra(string $extra) : self |
||
261 | |||
262 | public function setTitleSeparator(string $separator) : self |
||
268 | |||
269 | public function setBorder( |
||
301 | |||
302 | public function setBorderTopWidth(int $width) : self |
||
308 | |||
309 | public function setBorderRightWidth(int $width) : self |
||
315 | |||
316 | public function setBorderBottomWidth(int $width) : self |
||
322 | |||
323 | public function setBorderLeftWidth(int $width) : self |
||
329 | |||
330 | public function setBorderColour(string $colour, $fallback = null) : self |
||
337 | |||
338 | public function setTerminal(Terminal $terminal) : self |
||
343 | |||
344 | public function getTerminal() : Terminal |
||
348 | |||
349 | private function getDefaultItems() : array |
||
359 | |||
360 | public function disableDefaultItems() : self |
||
366 | |||
367 | private function itemsHaveExtra(array $items) : bool |
||
373 | |||
374 | /** |
||
375 | * Recursively drop back to the parents menu style |
||
376 | * when the current menu has a parent and has no changes |
||
377 | */ |
||
378 | public function getMenuStyle() : MenuStyle |
||
390 | |||
391 | private function buildStyle() : MenuStyle |
||
414 | |||
415 | /** |
||
416 | * @throws RuntimeException |
||
417 | */ |
||
418 | public function getSubMenu(string $id) : CliMenu |
||
426 | |||
427 | private function buildSplitItems(array $items) : array |
||
440 | |||
441 | public function build() : CliMenu |
||
472 | |||
473 | /** |
||
474 | * Return to parent builder |
||
475 | * |
||
476 | * @return CliMenuBuilder|SplitItemBuilder |
||
477 | */ |
||
478 | public function end() : ?Builder |
||
486 | } |
||
487 |
This check looks for function calls that miss required arguments.