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 |
||
30 | class CliMenu |
||
31 | { |
||
32 | /** |
||
33 | * @var Terminal |
||
34 | */ |
||
35 | protected $terminal; |
||
36 | |||
37 | /** |
||
38 | * @var MenuStyle |
||
39 | */ |
||
40 | protected $style; |
||
41 | |||
42 | /** |
||
43 | * @var ?string |
||
44 | */ |
||
45 | protected $title; |
||
46 | |||
47 | /** |
||
48 | * @var MenuItemInterface[] |
||
49 | */ |
||
50 | protected $items = []; |
||
51 | |||
52 | /** |
||
53 | * @var int |
||
54 | */ |
||
55 | protected $selectedItem; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | protected $open = false; |
||
61 | |||
62 | /** |
||
63 | * @var CliMenu|null |
||
64 | */ |
||
65 | protected $parent; |
||
66 | |||
67 | /** |
||
68 | * @var array |
||
69 | */ |
||
70 | protected $defaultControlMappings = [ |
||
71 | '^P' => InputCharacter::UP, |
||
72 | 'k' => InputCharacter::UP, |
||
73 | '^K' => InputCharacter::DOWN, |
||
74 | 'j' => InputCharacter::DOWN, |
||
75 | "\r" => InputCharacter::ENTER, |
||
76 | ' ' => InputCharacter::ENTER, |
||
77 | 'l' => InputCharacter::LEFT, |
||
78 | 'm' => InputCharacter::RIGHT, |
||
79 | ]; |
||
80 | |||
81 | /** |
||
82 | * @var array |
||
83 | */ |
||
84 | protected $customControlMappings = []; |
||
85 | |||
86 | /** |
||
87 | * @var Frame |
||
88 | */ |
||
89 | private $currentFrame; |
||
90 | |||
91 | public function __construct( |
||
92 | ?string $title, |
||
93 | array $items, |
||
94 | Terminal $terminal = null, |
||
95 | MenuStyle $style = null |
||
96 | ) { |
||
97 | $this->title = $title; |
||
98 | $this->items = $items; |
||
99 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
100 | $this->style = $style ?: new MenuStyle($this->terminal); |
||
101 | |||
102 | $this->selectFirstItem(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Configure the terminal to work with CliMenu |
||
107 | */ |
||
108 | protected function configureTerminal() : void |
||
109 | { |
||
110 | $this->assertTerminalIsValidTTY(); |
||
111 | |||
112 | $this->terminal->disableCanonicalMode(); |
||
113 | $this->terminal->disableEchoBack(); |
||
114 | $this->terminal->disableCursor(); |
||
115 | $this->terminal->clear(); |
||
116 | } |
||
117 | |||
118 | /** |
||
119 | * Revert changes made to the terminal |
||
120 | */ |
||
121 | protected function tearDownTerminal() : void |
||
122 | { |
||
123 | $this->terminal->restoreOriginalConfiguration(); |
||
124 | $this->terminal->enableCursor(); |
||
125 | } |
||
126 | |||
127 | private function assertTerminalIsValidTTY() : void |
||
128 | { |
||
129 | if (!$this->terminal->isInteractive()) { |
||
130 | throw new InvalidTerminalException('Terminal is not interactive (TTY)'); |
||
131 | } |
||
132 | } |
||
133 | |||
134 | |||
135 | public function setParent(CliMenu $parent) : void |
||
136 | { |
||
137 | $this->parent = $parent; |
||
138 | } |
||
139 | |||
140 | public function getParent() : ?CliMenu |
||
141 | { |
||
142 | return $this->parent; |
||
143 | } |
||
144 | |||
145 | public function getTerminal() : Terminal |
||
146 | { |
||
147 | return $this->terminal; |
||
148 | } |
||
149 | |||
150 | public function isOpen() : bool |
||
154 | |||
155 | /** |
||
156 | * Add a new Item to the menu |
||
157 | */ |
||
158 | public function addItem(MenuItemInterface $item) : void |
||
159 | { |
||
160 | $this->items[] = $item; |
||
161 | |||
166 | |||
167 | /** |
||
168 | * Add multiple Items to the menu |
||
169 | */ |
||
170 | public function addItems(array $items) : void |
||
180 | |||
181 | /** |
||
182 | * Set Items of the menu |
||
183 | */ |
||
184 | public function setItems(array $items) : void |
||
190 | |||
191 | /** |
||
192 | * Set the selected pointer to the first selectable item |
||
193 | */ |
||
194 | private function selectFirstItem() : void |
||
203 | |||
204 | /** |
||
205 | * Adds a custom control mapping |
||
206 | */ |
||
207 | public function addCustomControlMapping(string $input, callable $callable) : void |
||
215 | |||
216 | /** |
||
217 | * Shorthand function to add multiple custom control mapping at once |
||
218 | */ |
||
219 | public function addCustomControlMappings(array $map) : void |
||
225 | |||
226 | /** |
||
227 | * Removes a custom control mapping |
||
228 | */ |
||
229 | public function removeCustomControlMapping(string $input) : void |
||
237 | |||
238 | /** |
||
239 | * Display menu and capture input |
||
240 | */ |
||
241 | private function display() : void |
||
274 | |||
275 | /** |
||
276 | * Move the selection in a given direction, up / down |
||
277 | */ |
||
278 | protected function moveSelectionVertically(string $direction) : void |
||
294 | |||
295 | /** |
||
296 | * Move the selection in a given direction, left / right |
||
297 | */ |
||
298 | protected function moveSelectionHorizontally(string $direction) : void |
||
323 | |||
324 | /** |
||
325 | * Can the currently selected item actually be selected? |
||
326 | * |
||
327 | * For example: |
||
328 | * selectable item -> yes |
||
329 | * static item -> no |
||
330 | * split item with only static items -> no |
||
331 | * split item with at least one selectable item -> yes |
||
332 | * |
||
333 | * @return bool |
||
334 | */ |
||
335 | private function canSelect() : bool |
||
339 | |||
340 | /** |
||
341 | * Retrieve the item the user actually selected |
||
342 | * |
||
343 | */ |
||
344 | public function getSelectedItem() : MenuItemInterface |
||
351 | |||
352 | /** |
||
353 | * Execute the current item |
||
354 | */ |
||
355 | protected function executeCurrentItem() : void |
||
364 | |||
365 | /** |
||
366 | * If true we clear the whole terminal screen, useful |
||
367 | * for example when reducing the width of the menu, to not |
||
368 | * leave leftovers of the previous wider menu. |
||
369 | * |
||
370 | * Redraw the menu |
||
371 | */ |
||
372 | public function redraw(bool $clear = false) : void |
||
381 | |||
382 | private function assertOpen() : void |
||
388 | |||
389 | /** |
||
390 | * Draw the menu to STDOUT |
||
391 | */ |
||
392 | protected function draw() : void |
||
437 | |||
438 | /** |
||
439 | * Draw a menu item |
||
440 | */ |
||
441 | protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) : array |
||
480 | |||
481 | /** |
||
482 | * @throws InvalidTerminalException |
||
483 | */ |
||
484 | public function open() : void |
||
494 | |||
495 | /** |
||
496 | * Close the menu |
||
497 | * |
||
498 | * @throws InvalidTerminalException |
||
499 | */ |
||
500 | public function close() : void |
||
511 | |||
512 | public function closeThis() : void |
||
518 | |||
519 | /** |
||
520 | * @return MenuItemInterface[] |
||
521 | */ |
||
522 | public function getItems() : array |
||
526 | |||
527 | public function removeItem(MenuItemInterface $item) : void |
||
538 | |||
539 | public function getStyle() : MenuStyle |
||
543 | |||
544 | public function getCurrentFrame() : Frame |
||
548 | |||
549 | public function flash(string $text, MenuStyle $style = null) : Flash |
||
559 | |||
560 | public function confirm(string $text, MenuStyle $style = null) : Confirm |
||
570 | |||
571 | public function askNumber(MenuStyle $style = null) : Number |
||
581 | |||
582 | public function askText(MenuStyle $style = null) : Text |
||
592 | |||
593 | public function askPassword(MenuStyle $style = null) : Password |
||
603 | |||
604 | private function guardSingleLine($text) : void |
||
610 | } |
||
611 |