Complex classes like CliMenu 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 CliMenu, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 31 | class CliMenu |
||
| 32 | { |
||
| 33 | /** |
||
| 34 | * @var Terminal |
||
| 35 | */ |
||
| 36 | protected $terminal; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var MenuStyle |
||
| 40 | */ |
||
| 41 | protected $style; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var ?string |
||
| 45 | */ |
||
| 46 | protected $title; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var MenuItemInterface[] |
||
| 50 | */ |
||
| 51 | protected $items = []; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var int |
||
| 55 | */ |
||
| 56 | protected $selectedItem; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var bool |
||
| 60 | */ |
||
| 61 | protected $open = false; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var CliMenu|null |
||
| 65 | */ |
||
| 66 | protected $parent; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var array |
||
| 70 | */ |
||
| 71 | protected $defaultControlMappings = [ |
||
| 72 | '^P' => InputCharacter::UP, |
||
| 73 | 'k' => InputCharacter::UP, |
||
| 74 | '^K' => InputCharacter::DOWN, |
||
| 75 | 'j' => InputCharacter::DOWN, |
||
| 76 | "\r" => InputCharacter::ENTER, |
||
| 77 | ' ' => InputCharacter::ENTER, |
||
| 78 | 'l' => InputCharacter::LEFT, |
||
| 79 | 'm' => InputCharacter::RIGHT, |
||
| 80 | ]; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var array |
||
| 84 | */ |
||
| 85 | protected $customControlMappings = []; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var Frame |
||
| 89 | */ |
||
| 90 | private $currentFrame; |
||
| 91 | |||
| 92 | public function __construct( |
||
| 93 | ?string $title, |
||
| 94 | array $items, |
||
| 95 | Terminal $terminal = null, |
||
| 96 | MenuStyle $style = null |
||
| 97 | ) { |
||
| 98 | $this->title = $title; |
||
| 99 | $this->items = $items; |
||
| 100 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
| 101 | $this->style = $style ?: new MenuStyle($this->terminal); |
||
| 102 | |||
| 103 | $this->selectFirstItem(); |
||
| 104 | } |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Configure the terminal to work with CliMenu |
||
| 108 | */ |
||
| 109 | protected function configureTerminal() : void |
||
| 110 | { |
||
| 111 | $this->assertTerminalIsValidTTY(); |
||
| 112 | |||
| 113 | $this->terminal->disableCanonicalMode(); |
||
| 114 | $this->terminal->disableEchoBack(); |
||
| 115 | $this->terminal->disableCursor(); |
||
| 116 | $this->terminal->clear(); |
||
| 117 | } |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Revert changes made to the terminal |
||
| 121 | */ |
||
| 122 | protected function tearDownTerminal() : void |
||
| 123 | { |
||
| 124 | $this->terminal->restoreOriginalConfiguration(); |
||
| 125 | $this->terminal->enableCursor(); |
||
| 126 | } |
||
| 127 | |||
| 128 | private function assertTerminalIsValidTTY() : void |
||
| 129 | { |
||
| 130 | if (!$this->terminal->isInteractive()) { |
||
| 131 | throw new InvalidTerminalException('Terminal is not interactive (TTY)'); |
||
| 132 | } |
||
| 133 | } |
||
| 134 | |||
| 135 | public function setTitle(string $title) : void |
||
| 136 | { |
||
| 137 | $this->title = $title; |
||
| 138 | } |
||
| 139 | |||
| 140 | public function setParent(CliMenu $parent) : void |
||
| 141 | { |
||
| 142 | $this->parent = $parent; |
||
| 143 | } |
||
| 144 | |||
| 145 | public function getParent() : ?CliMenu |
||
| 146 | { |
||
| 147 | return $this->parent; |
||
| 148 | } |
||
| 149 | |||
| 150 | public function getTerminal() : Terminal |
||
| 151 | { |
||
| 152 | return $this->terminal; |
||
| 153 | } |
||
| 154 | |||
| 155 | public function isOpen() : bool |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Add a new Item to the menu |
||
| 162 | */ |
||
| 163 | public function addItem(MenuItemInterface $item) : void |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Add multiple Items to the menu |
||
| 174 | */ |
||
| 175 | public function addItems(array $items) : void |
||
| 185 | |||
| 186 | /** |
||
| 187 | * Set Items of the menu |
||
| 188 | */ |
||
| 189 | public function setItems(array $items) : void |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Set the selected pointer to the first selectable item |
||
| 198 | */ |
||
| 199 | private function selectFirstItem() : void |
||
| 208 | |||
| 209 | /** |
||
| 210 | * Adds a custom control mapping |
||
| 211 | */ |
||
| 212 | public function addCustomControlMapping(string $input, callable $callable) : void |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Shorthand function to add multiple custom control mapping at once |
||
| 223 | */ |
||
| 224 | public function addCustomControlMappings(array $map) : void |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Removes a custom control mapping |
||
| 233 | */ |
||
| 234 | public function removeCustomControlMapping(string $input) : void |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Display menu and capture input |
||
| 245 | */ |
||
| 246 | private function display() : void |
||
| 279 | |||
| 280 | /** |
||
| 281 | * Move the selection in a given direction, up / down |
||
| 282 | */ |
||
| 283 | protected function moveSelectionVertically(string $direction) : void |
||
| 284 | { |
||
| 299 | |||
| 300 | /** |
||
| 301 | * Move the selection in a given direction, left / right |
||
| 302 | */ |
||
| 303 | protected function moveSelectionHorizontally(string $direction) : void |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Can the currently selected item actually be selected? |
||
| 331 | * |
||
| 332 | * For example: |
||
| 333 | * selectable item -> yes |
||
| 334 | * static item -> no |
||
| 335 | * split item with only static items -> no |
||
| 336 | * split item with at least one selectable item -> yes |
||
| 337 | * |
||
| 338 | * @return bool |
||
| 339 | */ |
||
| 340 | private function canSelect() : bool |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Retrieve the item the user actually selected |
||
| 347 | * |
||
| 348 | */ |
||
| 349 | public function getSelectedItem() : MenuItemInterface |
||
| 356 | |||
| 357 | /** |
||
| 358 | * Execute the current item |
||
| 359 | */ |
||
| 360 | protected function executeCurrentItem() : void |
||
| 369 | |||
| 370 | /** |
||
| 371 | * If true we clear the whole terminal screen, useful |
||
| 372 | * for example when reducing the width of the menu, to not |
||
| 373 | * leave leftovers of the previous wider menu. |
||
| 374 | * |
||
| 375 | * Redraw the menu |
||
| 376 | */ |
||
| 377 | public function redraw(bool $clear = false) : void |
||
| 386 | |||
| 387 | private function assertOpen() : void |
||
| 393 | |||
| 394 | /** |
||
| 395 | * Draw the menu to STDOUT |
||
| 396 | */ |
||
| 397 | protected function draw() : void |
||
| 442 | |||
| 443 | /** |
||
| 444 | * Draw a menu item |
||
| 445 | */ |
||
| 446 | protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) : array |
||
| 485 | |||
| 486 | /** |
||
| 487 | * @throws InvalidTerminalException |
||
| 488 | */ |
||
| 489 | public function open() : void |
||
| 503 | |||
| 504 | /** |
||
| 505 | * Close the menu |
||
| 506 | * |
||
| 507 | * @throws InvalidTerminalException |
||
| 508 | */ |
||
| 509 | public function close() : void |
||
| 520 | |||
| 521 | public function closeThis() : void |
||
| 527 | |||
| 528 | /** |
||
| 529 | * @return MenuItemInterface[] |
||
| 530 | */ |
||
| 531 | public function getItems() : array |
||
| 535 | |||
| 536 | public function removeItem(MenuItemInterface $item) : void |
||
| 547 | |||
| 548 | public function getStyle() : MenuStyle |
||
| 552 | |||
| 553 | public function setStyle(MenuStyle $style) : void |
||
| 557 | |||
| 558 | public function getCurrentFrame() : Frame |
||
| 562 | |||
| 563 | public function flash(string $text, MenuStyle $style = null) : Flash |
||
| 573 | |||
| 574 | public function confirm(string $text, MenuStyle $style = null) : Confirm |
||
| 584 | |||
| 585 | public function askNumber(MenuStyle $style = null) : Number |
||
| 595 | |||
| 596 | public function askText(MenuStyle $style = null) : Text |
||
| 606 | |||
| 607 | public function askPassword(MenuStyle $style = null) : Password |
||
| 617 | |||
| 618 | private function guardSingleLine($text) : void |
||
| 624 | } |
||
| 625 |