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 |
||
23 | { |
||
24 | /** |
||
25 | * @var CliMenu |
||
26 | */ |
||
27 | private $menu; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $goBackButtonText = 'Go Back'; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $exitButtonText = 'Exit'; |
||
38 | |||
39 | /** |
||
40 | * @var MenuStyle |
||
41 | */ |
||
42 | private $style; |
||
43 | |||
44 | /** |
||
45 | * @var Terminal |
||
46 | */ |
||
47 | private $terminal; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $disableDefaultItems = false; |
||
53 | |||
54 | /** |
||
55 | * @var bool |
||
56 | */ |
||
57 | private $disabled = false; |
||
58 | |||
59 | /** |
||
60 | * @var bool |
||
61 | */ |
||
62 | private $subMenu = false; |
||
63 | |||
64 | public function __construct(Terminal $terminal = null) |
||
65 | { |
||
66 | $this->terminal = $terminal ?? TerminalFactory::fromSystem(); |
||
67 | $this->style = new MenuStyle($this->terminal); |
||
68 | $this->menu = new CliMenu(null, [], $this->terminal, $this->style); |
||
69 | } |
||
70 | |||
71 | public static function newSubMenu(Terminal $terminal) : self |
||
72 | { |
||
73 | $instance = new self($terminal); |
||
74 | $instance->subMenu = true; |
||
75 | |||
76 | return $instance; |
||
77 | } |
||
78 | |||
79 | public function setTitle(string $title) : self |
||
80 | { |
||
81 | $this->menu->setTitle($title); |
||
82 | |||
83 | return $this; |
||
84 | } |
||
85 | |||
86 | public function addMenuItem(MenuItemInterface $item) : self |
||
87 | { |
||
88 | $this->menu->addItem($item); |
||
89 | |||
90 | return $this; |
||
91 | } |
||
92 | |||
93 | public function addItem( |
||
94 | string $text, |
||
95 | callable $itemCallable, |
||
96 | bool $showItemExtra = false, |
||
97 | bool $disabled = false |
||
98 | ) : self { |
||
99 | $this->addMenuItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); |
||
100 | |||
101 | return $this; |
||
102 | } |
||
103 | |||
104 | public function addItems(array $items) : self |
||
112 | |||
113 | public function addStaticItem(string $text) : self |
||
114 | { |
||
115 | $this->addMenuItem(new StaticItem($text)); |
||
116 | |||
117 | return $this; |
||
119 | |||
120 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
126 | |||
127 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
133 | |||
134 | public function addSubMenu(string $text, callable $callback) : self |
||
157 | |||
158 | public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : self |
||
177 | |||
178 | public function addSplitItem(callable $callback) : self |
||
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 |
||
232 | |||
233 | public function setForegroundColour(string $colour, string $fallback = null) : self |
||
239 | |||
240 | public function setWidth(int $width) : self |
||
246 | |||
247 | public function setPadding(int $topBottom, int $leftRight = null) : self |
||
253 | |||
254 | public function setPaddingTopBottom(int $topBottom) : self |
||
260 | |||
261 | public function setPaddingLeftRight(int $leftRight) : self |
||
267 | |||
268 | public function setMarginAuto() : self |
||
274 | |||
275 | public function setMargin(int $margin) : self |
||
281 | |||
282 | public function setUnselectedMarker(string $marker) : self |
||
288 | |||
289 | public function setSelectedMarker(string $marker) : self |
||
295 | |||
296 | public function setItemExtra(string $extra) : self |
||
302 | |||
303 | public function setTitleSeparator(string $separator) : self |
||
309 | |||
310 | public function setBorder(int $top, $right = null, $bottom = null, $left = null, string $colour = null) : self |
||
316 | |||
317 | public function setBorderTopWidth(int $width) : self |
||
323 | |||
324 | public function setBorderRightWidth(int $width) : self |
||
330 | |||
331 | public function setBorderBottomWidth(int $width) : self |
||
337 | |||
338 | public function setBorderLeftWidth(int $width) : self |
||
344 | |||
345 | public function setBorderColour(string $colour, $fallback = null) : self |
||
351 | |||
352 | public function getTerminal() : Terminal |
||
356 | |||
357 | private function getDefaultItems() : array |
||
367 | |||
368 | public function disableDefaultItems() : self |
||
374 | |||
375 | private function itemsHaveExtra(array $items) : bool |
||
381 | |||
382 | public function build() : CliMenu |
||
392 | } |
||
393 |
This check looks for function calls that miss required arguments.