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, \Closure $callback) : self |
||
| 158 | |||
| 159 | public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : self |
||
| 178 | |||
| 179 | public function addSplitItem(\Closure $callback) : self |
||
| 190 | |||
| 191 | /** |
||
| 192 | * Disable a submenu |
||
| 193 | * |
||
| 194 | * @throws \InvalidArgumentException |
||
| 195 | */ |
||
| 196 | public function disableMenu() : self |
||
| 208 | |||
| 209 | public function isMenuDisabled() : bool |
||
| 213 | |||
| 214 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
| 220 | |||
| 221 | public function setExitButtonText(string $exitButtonText) : self |
||
| 227 | |||
| 228 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
| 234 | |||
| 235 | public function setForegroundColour(string $colour, string $fallback = null) : self |
||
| 241 | |||
| 242 | public function setWidth(int $width) : self |
||
| 248 | |||
| 249 | public function setPadding(int $topBottom, int $leftRight = null) : self |
||
| 255 | |||
| 256 | public function setPaddingTopBottom(int $topBottom) : self |
||
| 262 | |||
| 263 | public function setPaddingLeftRight(int $leftRight) : self |
||
| 269 | |||
| 270 | public function setMarginAuto() : self |
||
| 276 | |||
| 277 | public function setMargin(int $margin) : self |
||
| 283 | |||
| 284 | public function setUnselectedMarker(string $marker) : self |
||
| 290 | |||
| 291 | public function setSelectedMarker(string $marker) : self |
||
| 297 | |||
| 298 | public function setItemExtra(string $extra) : self |
||
| 304 | |||
| 305 | public function setTitleSeparator(string $separator) : self |
||
| 311 | |||
| 312 | public function setBorder(int $top, $right = null, $bottom = null, $left = null, string $colour = null) : self |
||
| 318 | |||
| 319 | public function setBorderTopWidth(int $width) : self |
||
| 325 | |||
| 326 | public function setBorderRightWidth(int $width) : self |
||
| 332 | |||
| 333 | public function setBorderBottomWidth(int $width) : self |
||
| 339 | |||
| 340 | public function setBorderLeftWidth(int $width) : self |
||
| 346 | |||
| 347 | public function setBorderColour(string $colour, $fallback = null) : self |
||
| 353 | |||
| 354 | public function getTerminal() : Terminal |
||
| 358 | |||
| 359 | private function getDefaultItems() : array |
||
| 369 | |||
| 370 | public function disableDefaultItems() : self |
||
| 376 | |||
| 377 | private function itemsHaveExtra(array $items) : bool |
||
| 383 | |||
| 384 | public function build() : CliMenu |
||
| 394 | } |
||
| 395 |
This check looks for function calls that miss required arguments.