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 |
||
| 25 | class CliMenuBuilder |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $isBuilt = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var null|self |
||
| 34 | */ |
||
| 35 | private $parent; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var self[]|CliMenu[] |
||
| 39 | */ |
||
| 40 | private $subMenus = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $goBackButtonText = 'Go Back'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $exitButtonText = 'Exit'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $menuItems = []; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var array |
||
| 59 | */ |
||
| 60 | private $style; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var TerminalInterface |
||
| 64 | */ |
||
| 65 | private $terminal; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var string |
||
| 69 | */ |
||
| 70 | private $menuTitle; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | private $disableDefaultItems = false; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var bool |
||
| 79 | */ |
||
| 80 | private $disabled = false; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param CliMenuBuilder|null $parent |
||
| 84 | */ |
||
| 85 | public function __construct(CliMenuBuilder $parent = null) |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @param string $title |
||
| 94 | * @return $this |
||
| 95 | */ |
||
| 96 | public function setTitle($title) |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @param MenuItemInterface $item |
||
| 107 | * @return $this |
||
| 108 | */ |
||
| 109 | public function addMenuItem(MenuItemInterface $item) |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $text |
||
| 118 | * @param callable $itemCallable |
||
| 119 | * @param bool $showItemExtra |
||
| 120 | * @param bool $disabled |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | public function addItem($text, callable $itemCallable, $showItemExtra = false, $disabled = false) |
||
| 131 | |||
| 132 | /** |
||
| 133 | * @param array $items |
||
| 134 | * @return $this |
||
| 135 | */ |
||
| 136 | public function addItems(array $items) |
||
| 144 | |||
| 145 | /** |
||
| 146 | * @param string $text |
||
| 147 | * @return $this |
||
| 148 | */ |
||
| 149 | public function addStaticItem($text) |
||
| 157 | |||
| 158 | /** |
||
| 159 | * @param string $breakChar |
||
| 160 | * @param int $lines |
||
| 161 | * @return $this |
||
| 162 | */ |
||
| 163 | public function addLineBreak($breakChar = ' ', $lines = 1) |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param string $art |
||
| 175 | * @param string $position |
||
| 176 | * @return $this |
||
| 177 | */ |
||
| 178 | public function addAsciiArt($art, $position = AsciiArtItem::POSITION_CENTER) |
||
| 187 | |||
| 188 | /** |
||
| 189 | * @param string $id ID to reference and retrieve sub-menu |
||
| 190 | * @return CliMenuBuilder |
||
| 191 | */ |
||
| 192 | public function addSubMenu($id) |
||
| 201 | |||
| 202 | /** |
||
| 203 | * Disable a submenu |
||
| 204 | * @throws \InvalidArgumentException |
||
| 205 | * @return $this |
||
| 206 | */ |
||
| 207 | public function disableMenu() |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return bool |
||
| 222 | */ |
||
| 223 | public function isMenuDisabled() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $goBackButtonTest |
||
| 230 | * @return $this |
||
| 231 | */ |
||
| 232 | public function setGoBackButtonText($goBackButtonTest) |
||
| 238 | |||
| 239 | /** |
||
| 240 | * @param string $exitButtonText |
||
| 241 | * @return $this |
||
| 242 | */ |
||
| 243 | public function setExitButtonText($exitButtonText) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * @param string $colour |
||
| 252 | * @return $this |
||
| 253 | */ |
||
| 254 | public function setBackgroundColour($colour) |
||
| 262 | |||
| 263 | /** |
||
| 264 | * @param string $colour |
||
| 265 | * @return $this |
||
| 266 | */ |
||
| 267 | public function setForegroundColour($colour) |
||
| 275 | |||
| 276 | /** |
||
| 277 | * @param int $width |
||
| 278 | * @return $this |
||
| 279 | */ |
||
| 280 | public function setWidth($width) |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param int $padding |
||
| 291 | * @return $this |
||
| 292 | */ |
||
| 293 | public function setPadding($padding) |
||
| 301 | |||
| 302 | /** |
||
| 303 | * @param int $margin |
||
| 304 | * @return $this |
||
| 305 | */ |
||
| 306 | public function setMargin($margin) |
||
| 314 | |||
| 315 | /** |
||
| 316 | * @param string $marker |
||
| 317 | * @return $this |
||
| 318 | */ |
||
| 319 | public function setUnselectedMarker($marker) |
||
| 327 | |||
| 328 | /** |
||
| 329 | * @param string $marker |
||
| 330 | * @return $this |
||
| 331 | */ |
||
| 332 | public function setSelectedMarker($marker) |
||
| 340 | |||
| 341 | /** |
||
| 342 | * @param string $extra |
||
| 343 | * @return $this |
||
| 344 | */ |
||
| 345 | public function setItemExtra($extra) |
||
| 353 | |||
| 354 | /** |
||
| 355 | * @param string $separator |
||
| 356 | * @return $this |
||
| 357 | */ |
||
| 358 | public function setTitleSeparator($separator) |
||
| 366 | |||
| 367 | /** |
||
| 368 | * @param TerminalInterface $terminal |
||
| 369 | * @return $this |
||
| 370 | */ |
||
| 371 | public function setTerminal(TerminalInterface $terminal) |
||
| 376 | |||
| 377 | /** |
||
| 378 | * @return array |
||
| 379 | */ |
||
| 380 | private function getDefaultItems() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * @return $this |
||
| 393 | */ |
||
| 394 | public function disableDefaultItems() |
||
| 400 | |||
| 401 | /** |
||
| 402 | * @param array $items |
||
| 403 | * @return bool |
||
| 404 | */ |
||
| 405 | private function itemsHaveExtra(array $items) |
||
| 411 | |||
| 412 | /** |
||
| 413 | * Recursively drop back to the parents menu style |
||
| 414 | * when the current menu has a parent and has no changes |
||
| 415 | * |
||
| 416 | * @return MenuStyle |
||
| 417 | */ |
||
| 418 | private function getMenuStyle() |
||
| 430 | |||
| 431 | /** |
||
| 432 | * @return MenuStyle |
||
| 433 | */ |
||
| 434 | private function buildStyle() |
||
| 448 | |||
| 449 | /** |
||
| 450 | * Return to parent builder |
||
| 451 | * |
||
| 452 | * @return CliMenuBuilder |
||
| 453 | * @throws RuntimeException |
||
| 454 | */ |
||
| 455 | public function end() |
||
| 463 | |||
| 464 | /** |
||
| 465 | * @param string $id |
||
| 466 | * @return CliMenu |
||
| 467 | * @throws RuntimeException |
||
| 468 | */ |
||
| 469 | public function getSubMenu($id) |
||
| 477 | |||
| 478 | /** |
||
| 479 | * @param array $items |
||
| 480 | * @return array |
||
| 481 | */ |
||
| 482 | private function buildSubMenus(array $items) |
||
| 495 | |||
| 496 | /** |
||
| 497 | * @return CliMenu |
||
| 498 | */ |
||
| 499 | public function build() |
||
| 524 | } |
||
| 525 |
This check looks for function calls that miss required arguments.