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) |
||
| 92 | |||
| 93 | /** |
||
| 94 | * Pull the constructor params into an array with default values |
||
| 95 | * |
||
| 96 | * @return array |
||
| 97 | */ |
||
| 98 | private function getStyleClassDefaults() |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @param string $title |
||
| 112 | * @return $this |
||
| 113 | */ |
||
| 114 | public function setTitle($title) |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @param MenuItemInterface $item |
||
| 125 | * @return $this |
||
| 126 | */ |
||
| 127 | public function addMenuItem(MenuItemInterface $item) |
||
| 133 | |||
| 134 | /** |
||
| 135 | * @param string $text |
||
| 136 | * @param callable $itemCallable |
||
| 137 | * @param bool $showItemExtra |
||
| 138 | * @param bool $disabled |
||
| 139 | * @return $this |
||
| 140 | */ |
||
| 141 | public function addItem($text, callable $itemCallable, $showItemExtra = false, $disabled = false) |
||
| 149 | |||
| 150 | /** |
||
| 151 | * @param array $items |
||
| 152 | * @return $this |
||
| 153 | */ |
||
| 154 | public function addItems(array $items) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * @param string $text |
||
| 165 | * @return $this |
||
| 166 | */ |
||
| 167 | public function addStaticItem($text) |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $breakChar |
||
| 178 | * @param int $lines |
||
| 179 | * @return $this |
||
| 180 | */ |
||
| 181 | public function addLineBreak($breakChar = ' ', $lines = 1) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @param string $art |
||
| 193 | * @param string $position |
||
| 194 | * @return $this |
||
| 195 | */ |
||
| 196 | public function addAsciiArt($art, $position = AsciiArtItem::POSITION_CENTER) |
||
| 205 | |||
| 206 | /** |
||
| 207 | * @param string $id ID to reference and retrieve sub-menu |
||
| 208 | * @return CliMenuBuilder |
||
| 209 | */ |
||
| 210 | public function addSubMenu($id) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Disable a submenu |
||
| 222 | * @throws \InvalidArgumentException |
||
| 223 | * @return $this |
||
| 224 | */ |
||
| 225 | public function disableMenu() |
||
| 237 | |||
| 238 | /** |
||
| 239 | * @return bool |
||
| 240 | */ |
||
| 241 | public function isMenuDisabled() |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @param string $goBackButtonTest |
||
| 248 | * @return $this |
||
| 249 | */ |
||
| 250 | public function setGoBackButtonText($goBackButtonTest) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * @param string $exitButtonText |
||
| 259 | * @return $this |
||
| 260 | */ |
||
| 261 | public function setExitButtonText($exitButtonText) |
||
| 267 | |||
| 268 | /** |
||
| 269 | * @param string $colour |
||
| 270 | * @return $this |
||
| 271 | */ |
||
| 272 | public function setBackgroundColour($colour) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @param string $colour |
||
| 283 | * @return $this |
||
| 284 | */ |
||
| 285 | public function setForegroundColour($colour) |
||
| 293 | |||
| 294 | /** |
||
| 295 | * @param int $width |
||
| 296 | * @return $this |
||
| 297 | */ |
||
| 298 | public function setWidth($width) |
||
| 306 | |||
| 307 | /** |
||
| 308 | * @param int $padding |
||
| 309 | * @return $this |
||
| 310 | */ |
||
| 311 | public function setPadding($padding) |
||
| 319 | |||
| 320 | /** |
||
| 321 | * @param int $margin |
||
| 322 | * @return $this |
||
| 323 | */ |
||
| 324 | public function setMargin($margin) |
||
| 332 | |||
| 333 | /** |
||
| 334 | * @param string $marker |
||
| 335 | * @return $this |
||
| 336 | */ |
||
| 337 | public function setUnselectedMarker($marker) |
||
| 345 | |||
| 346 | /** |
||
| 347 | * @param string $marker |
||
| 348 | * @return $this |
||
| 349 | */ |
||
| 350 | public function setSelectedMarker($marker) |
||
| 358 | |||
| 359 | /** |
||
| 360 | * @param string $extra |
||
| 361 | * @return $this |
||
| 362 | */ |
||
| 363 | public function setItemExtra($extra) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * @param string $separator |
||
| 374 | * @return $this |
||
| 375 | */ |
||
| 376 | public function setTitleSeparator($separator) |
||
| 384 | |||
| 385 | /** |
||
| 386 | * @param TerminalInterface $terminal |
||
| 387 | * @return $this |
||
| 388 | */ |
||
| 389 | public function setTerminal(TerminalInterface $terminal) |
||
| 395 | |||
| 396 | /** |
||
| 397 | * @return array |
||
| 398 | */ |
||
| 399 | private function getDefaultItems() |
||
| 409 | |||
| 410 | /** |
||
| 411 | * @return $this |
||
| 412 | */ |
||
| 413 | public function disableDefaultItems() |
||
| 419 | |||
| 420 | /** |
||
| 421 | * @param array $items |
||
| 422 | * @return bool |
||
| 423 | */ |
||
| 424 | private function itemsHaveExtra(array $items) |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Recursively drop back to the parents menu style |
||
| 433 | * when the current menu has a parent and has no changes |
||
| 434 | * |
||
| 435 | * @return MenuStyle |
||
| 436 | */ |
||
| 437 | private function getMenuStyle() |
||
| 453 | |||
| 454 | /** |
||
| 455 | * Return to parent builder |
||
| 456 | * |
||
| 457 | * @return CliMenuBuilder |
||
| 458 | * @throws RuntimeException |
||
| 459 | */ |
||
| 460 | public function end() |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @param string $id |
||
| 471 | * @return CliMenuBuilder |
||
| 472 | * @throws RuntimeException |
||
| 473 | */ |
||
| 474 | public function getSubMenu($id) |
||
| 482 | |||
| 483 | /** |
||
| 484 | * @param array $items |
||
| 485 | * @return array |
||
| 486 | */ |
||
| 487 | private function buildSubMenus(array $items) |
||
| 500 | |||
| 501 | /** |
||
| 502 | * @return CliMenu |
||
| 503 | */ |
||
| 504 | public function build() |
||
| 529 | } |
||
| 530 |
This check looks for function calls that miss required arguments.