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 |
||
21 | class CliMenuBuilder implements Builder |
||
22 | { |
||
23 | use BuilderUtils; |
||
24 | |||
25 | /** |
||
26 | * @var null|Builder |
||
27 | */ |
||
28 | private $parent; |
||
29 | |||
30 | /** |
||
31 | * @var bool |
||
32 | */ |
||
33 | private $isBuilt = false; |
||
34 | |||
35 | /** |
||
36 | * @var SplitItemBuilder[] |
||
37 | */ |
||
38 | private $splitItemBuilders = []; |
||
39 | |||
40 | /** |
||
41 | * @var SplitItem[] |
||
42 | */ |
||
43 | private $splitItems = []; |
||
44 | |||
45 | /** |
||
46 | * @var string |
||
47 | */ |
||
48 | private $goBackButtonText = 'Go Back'; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $exitButtonText = 'Exit'; |
||
54 | |||
55 | /** |
||
56 | * @var MenuStyle |
||
57 | */ |
||
58 | private $style; |
||
59 | |||
60 | /** |
||
61 | * @var Terminal |
||
62 | */ |
||
63 | private $terminal; |
||
64 | |||
65 | /** |
||
66 | * @var string |
||
67 | */ |
||
68 | private $menuTitle; |
||
69 | |||
70 | /** |
||
71 | * @var bool |
||
72 | */ |
||
73 | private $disableDefaultItems = false; |
||
74 | |||
75 | /** |
||
76 | * @var bool |
||
77 | */ |
||
78 | private $disabled = false; |
||
79 | |||
80 | public function __construct(Terminal $terminal = null, Builder $parent = null) |
||
86 | |||
87 | public static function newFromParent(Builder $parent) : self |
||
91 | |||
92 | public function setTitle(string $title) : self |
||
98 | |||
99 | public function addMenuItem(MenuItemInterface $item) : self |
||
105 | |||
106 | public function addItems(array $items) : self |
||
114 | |||
115 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
121 | |||
122 | /** |
||
123 | * Add a split item |
||
124 | */ |
||
125 | public function addSplitItem() : SplitItemBuilder |
||
135 | |||
136 | /** |
||
137 | * Disable a submenu |
||
138 | * |
||
139 | * @throws \InvalidArgumentException |
||
140 | */ |
||
141 | public function disableMenu() : self |
||
153 | |||
154 | public function isMenuDisabled() : bool |
||
158 | |||
159 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
165 | |||
166 | public function setExitButtonText(string $exitButtonText) : self |
||
172 | |||
173 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
179 | |||
180 | public function setForegroundColour(string $colour, string $fallback = null) : self |
||
186 | |||
187 | public function setWidth(int $width) : self |
||
193 | |||
194 | public function setPadding(int $topBottom, int $leftRight = null) : self |
||
200 | |||
201 | public function setPaddingTopBottom(int $topBottom) : self |
||
207 | |||
208 | public function setPaddingLeftRight(int $leftRight) : self |
||
214 | |||
215 | public function setMarginAuto() : self |
||
221 | |||
222 | public function setMargin(int $margin) : self |
||
228 | |||
229 | public function setUnselectedMarker(string $marker) : self |
||
235 | |||
236 | public function setSelectedMarker(string $marker) : self |
||
242 | |||
243 | public function setItemExtra(string $extra) : self |
||
249 | |||
250 | public function setTitleSeparator(string $separator) : self |
||
256 | |||
257 | public function setBorder(int $top, $right = null, $bottom = null, $left = null, string $colour = null) : self |
||
263 | |||
264 | public function setBorderTopWidth(int $width) : self |
||
270 | |||
271 | public function setBorderRightWidth(int $width) : self |
||
277 | |||
278 | public function setBorderBottomWidth(int $width) : self |
||
284 | |||
285 | public function setBorderLeftWidth(int $width) : self |
||
291 | |||
292 | public function setBorderColour(string $colour, $fallback = null) : self |
||
298 | |||
299 | public function getTerminal() : Terminal |
||
303 | |||
304 | private function getDefaultItems() : array |
||
314 | |||
315 | public function disableDefaultItems() : self |
||
321 | |||
322 | private function itemsHaveExtra(array $items) : bool |
||
328 | |||
329 | /** |
||
330 | * Recursively drop back to the parents menu style |
||
331 | * when the current menu has a parent and has no changes |
||
332 | */ |
||
333 | public function getMenuStyle() : MenuStyle |
||
345 | |||
346 | /** |
||
347 | * @throws RuntimeException |
||
348 | */ |
||
349 | public function getSubMenu(string $id) : CliMenu |
||
357 | |||
358 | private function buildSplitItems(array $items) : array |
||
371 | |||
372 | public function build() : CliMenu |
||
403 | |||
404 | /** |
||
405 | * Return to parent builder |
||
406 | * |
||
407 | * @return CliMenuBuilder|SplitItemBuilder |
||
408 | */ |
||
409 | public function end() : ?Builder |
||
417 | } |
||
418 |
This check looks for function calls that miss required arguments.