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 |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $isBuilt = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var null|self |
||
| 32 | */ |
||
| 33 | private $parent; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var self[] |
||
| 37 | */ |
||
| 38 | private $subMenuBuilders = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var CliMenu[] |
||
| 42 | */ |
||
| 43 | private $subMenus = []; |
||
| 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 array |
||
| 57 | */ |
||
| 58 | private $menuItems = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $style; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Terminal |
||
| 67 | */ |
||
| 68 | private $terminal; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $menuTitle = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | private $disableDefaultItems = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | private $disabled = false; |
||
| 84 | |||
| 85 | public function __construct(CliMenuBuilder $parent = null) |
||
| 93 | |||
| 94 | public function setTitle(string $title) : self |
||
| 100 | |||
| 101 | public function addMenuItem(MenuItemInterface $item) : self |
||
| 107 | |||
| 108 | public function addItem( |
||
| 118 | |||
| 119 | public function addItems(array $items) : self |
||
| 127 | |||
| 128 | public function addStaticItem(string $text) : self |
||
| 134 | |||
| 135 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
| 141 | |||
| 142 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Add a submenu with a string identifier |
||
| 151 | */ |
||
| 152 | public function addSubMenu(string $id, CliMenuBuilder $subMenuBuilder = null) : CliMenuBuilder |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Disable a submenu |
||
| 167 | * |
||
| 168 | * @throws \InvalidArgumentException |
||
| 169 | */ |
||
| 170 | public function disableMenu() : self |
||
| 182 | |||
| 183 | public function isMenuDisabled() : bool |
||
| 187 | |||
| 188 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
| 194 | |||
| 195 | public function setExitButtonText(string $exitButtonText) : self |
||
| 201 | |||
| 202 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
| 203 | { |
||
| 204 | $this->style['bg'] = ColourUtil::validateColour( |
||
| 205 | $this->terminal, |
||
| 206 | $colour, |
||
| 207 | $fallback |
||
| 208 | ); |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | public function setForegroundColour(string $colour, string $fallback = null) : self |
||
| 214 | { |
||
| 215 | $this->style['fg'] = ColourUtil::validateColour( |
||
| 216 | $this->terminal, |
||
| 217 | $colour, |
||
| 218 | $fallback |
||
| 219 | ); |
||
| 220 | |||
| 221 | return $this; |
||
| 222 | } |
||
| 223 | |||
| 224 | public function setWidth(int $width) : self |
||
| 230 | |||
| 231 | public function setPadding(int $padding) : self |
||
| 237 | |||
| 238 | public function setMarginAuto() : self |
||
| 244 | |||
| 245 | public function setMargin(int $margin) : self |
||
| 254 | |||
| 255 | public function setUnselectedMarker(string $marker) : self |
||
| 261 | |||
| 262 | public function setSelectedMarker(string $marker) : self |
||
| 268 | |||
| 269 | public function setItemExtra(string $extra) : self |
||
| 275 | |||
| 276 | public function setTitleSeparator(string $separator) : self |
||
| 282 | |||
| 283 | public function setBorder( |
||
| 284 | int $topWidth, |
||
| 285 | $rightWidth = null, |
||
| 286 | $bottomWidth = null, |
||
| 287 | $leftWidth = null, |
||
| 288 | string $colour = null |
||
| 289 | ) : self { |
||
| 290 | if (!is_int($rightWidth)) { |
||
| 291 | $colour = $rightWidth; |
||
| 292 | $rightWidth = $bottomWidth = $leftWidth = $topWidth; |
||
| 293 | } elseif (!is_int($bottomWidth)) { |
||
| 294 | $colour = $bottomWidth; |
||
| 295 | $bottomWidth = $topWidth; |
||
| 296 | $leftWidth = $rightWidth; |
||
| 297 | } elseif (!is_int($leftWidth)) { |
||
| 298 | $colour = $leftWidth; |
||
| 299 | $leftWidth = $rightWidth; |
||
| 300 | } |
||
| 301 | |||
| 302 | $this->style['borderTopWidth'] = $topWidth; |
||
| 303 | $this->style['borderRightWidth'] = $rightWidth; |
||
| 304 | $this->style['borderBottomWidth'] = $bottomWidth; |
||
| 305 | $this->style['borderLeftWidth'] = $leftWidth; |
||
| 306 | |||
| 307 | if (is_string($colour)) { |
||
| 308 | $this->style['borderColour'] = $colour; |
||
| 309 | } elseif ($colour !== null) { |
||
| 310 | throw new \InvalidArgumentException('Invalid colour'); |
||
| 311 | } |
||
| 312 | |||
| 313 | return $this; |
||
| 314 | } |
||
| 315 | |||
| 316 | public function setTerminal(Terminal $terminal) : self |
||
| 321 | |||
| 322 | public function getTerminal() : Terminal |
||
| 326 | |||
| 327 | private function getDefaultItems() : array |
||
| 337 | |||
| 338 | public function disableDefaultItems() : self |
||
| 344 | |||
| 345 | private function itemsHaveExtra(array $items) : bool |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Recursively drop back to the parents menu style |
||
| 354 | * when the current menu has a parent and has no changes |
||
| 355 | */ |
||
| 356 | private function getMenuStyle() : MenuStyle |
||
| 368 | |||
| 369 | private function buildStyle() : MenuStyle |
||
| 391 | |||
| 392 | /** |
||
| 393 | * Return to parent builder |
||
| 394 | * |
||
| 395 | * @throws RuntimeException |
||
| 396 | */ |
||
| 397 | public function end() : CliMenuBuilder |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @throws RuntimeException |
||
| 408 | */ |
||
| 409 | public function getSubMenu(string $id) : CliMenu |
||
| 417 | |||
| 418 | private function buildSubMenus(array $items) : array |
||
| 431 | |||
| 432 | public function build() : CliMenu |
||
| 457 | } |
||
| 458 |
This check looks for function calls that miss required arguments.