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 |
||
242 | { |
||
243 | $this->draw(); |
||
244 | |||
245 | $reader = new NonCanonicalReader($this->terminal); |
||
246 | $reader->addControlMappings($this->defaultControlMappings); |
||
247 | |||
248 | while ($this->isOpen() && $char = $reader->readCharacter()) { |
||
249 | if (!$char->isHandledControl()) { |
||
250 | $rawChar = $char->get(); |
||
251 | if (isset($this->customControlMappings[$rawChar])) { |
||
252 | $this->customControlMappings[$rawChar]($this); |
||
253 | } |
||
254 | continue; |
||
255 | } |
||
256 | |||
257 | switch ($char->getControl()) { |
||
258 | case InputCharacter::UP: |
||
259 | case InputCharacter::DOWN: |
||
260 | $this->moveSelectionVertically($char->getControl()); |
||
261 | $this->draw(); |
||
262 | break; |
||
263 | case InputCharacter::LEFT: |
||
264 | case InputCharacter::RIGHT: |
||
265 | $this->moveSelectionHorizontally($char->getControl()); |
||
266 | $this->draw(); |
||
267 | break; |
||
268 | case InputCharacter::ENTER: |
||
269 | $this->executeCurrentItem(); |
||
270 | break; |
||
271 | } |
||
272 | } |
||
273 | } |
||
274 | |||
275 | /** |
||
276 | * Move the selection in a given direction, up / down |
||
277 | */ |
||
278 | protected function moveSelectionVertically(string $direction) : void |
||
279 | { |
||
280 | $itemKeys = array_keys($this->items); |
||
281 | |||
282 | do { |
||
283 | $direction === 'UP' |
||
284 | ? $this->selectedItem-- |
||
285 | : $this->selectedItem++; |
||
286 | |||
287 | if (!array_key_exists($this->selectedItem, $this->items)) { |
||
288 | $this->selectedItem = $direction === 'UP' |
||
289 | ? end($itemKeys) |
||
290 | : reset($itemKeys); |
||
291 | } |
||
292 | } while (!$this->getSelectedItem()->canSelect()); |
||
293 | } |
||
294 | |||
295 | /** |
||
296 | * Move the selection in a given direction, left / right |
||
297 | */ |
||
298 | protected function moveSelectionHorizontally(string $direction) : void |
||
299 | { |
||
300 | if (!$this->items[$this->selectedItem] instanceof SplitItem) { |
||
301 | return; |
||
302 | } |
||
303 | |||
304 | $item = $this->items[$this->selectedItem]; |
||
305 | $itemKeys = array_keys($item->getItems()); |
||
|
|||
306 | $selectedItemIndex = $item->getSelectedItemIndex(); |
||
307 | |||
308 | do { |
||
309 | $direction === 'LEFT' |
||
310 | ? $selectedItemIndex-- |
||
311 | : $selectedItemIndex++; |
||
312 | $item->setSelectedItemIndex($selectedItemIndex); |
||
313 | |||
314 | if (!array_key_exists($selectedItemIndex, $item->getItems())) { |
||
315 | $selectedItemIndex = $direction === 'LEFT' |
||
316 | ? end($itemKeys) |
||
317 | : reset($itemKeys); |
||
318 | $item->setSelectedItemIndex($selectedItemIndex); |
||
319 | } |
||
320 | } while (!$item->getSelectedItem()->canSelect()); |
||
321 | } |
||
322 | |||
323 | public function getSelectedItem() : MenuItemInterface |
||
324 | { |
||
325 | $item = $this->items[$this->selectedItem]; |
||
326 | return $item instanceof SplitItem |
||
327 | ? $item->getSelectedItem() |
||
328 | : $item; |
||
329 | } |
||
330 | |||
331 | /** |
||
332 | * Execute the current item |
||
333 | */ |
||
334 | protected function executeCurrentItem() : void |
||
343 | |||
344 | /** |
||
345 | * If true we clear the whole terminal screen, useful |
||
346 | * for example when reducing the width of the menu, to not |
||
347 | * leave leftovers of the previous wider menu. |
||
348 | * |
||
349 | * Redraw the menu |
||
350 | */ |
||
351 | public function redraw(bool $clear = false) : void |
||
360 | |||
361 | private function assertOpen() : void |
||
367 | |||
368 | /** |
||
369 | * Draw the menu to STDOUT |
||
370 | */ |
||
371 | protected function draw() : void |
||
416 | |||
417 | /** |
||
418 | * Draw a menu item |
||
419 | */ |
||
420 | protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) : array |
||
459 | |||
460 | /** |
||
461 | * @throws InvalidTerminalException |
||
462 | */ |
||
463 | public function open() : void |
||
473 | |||
474 | /** |
||
475 | * Close the menu |
||
476 | * |
||
477 | * @throws InvalidTerminalException |
||
478 | */ |
||
479 | public function close() : void |
||
490 | |||
491 | public function closeThis() : void |
||
497 | |||
498 | /** |
||
499 | * @return MenuItemInterface[] |
||
500 | */ |
||
501 | public function getItems() : array |
||
505 | |||
506 | public function removeItem(MenuItemInterface $item) : void |
||
517 | |||
518 | public function getStyle() : MenuStyle |
||
522 | |||
523 | public function getCurrentFrame() : Frame |
||
527 | |||
528 | public function flash(string $text, MenuStyle $style = null) : Flash |
||
538 | |||
539 | public function confirm(string $text, MenuStyle $style = null) : Confirm |
||
549 | |||
550 | public function askNumber(MenuStyle $style = null) : Number |
||
560 | |||
561 | public function askText(MenuStyle $style = null) : Text |
||
571 | |||
572 | public function askPassword(MenuStyle $style = null) : Password |
||
582 | |||
583 | private function guardSingleLine($text) : void |
||
589 | } |
||
590 |
Let’s take a look at an example:
In the above example, the authenticate() method works fine as long as you just pass instances of MyUser. However, if you now also want to pass a different implementation of User which does not have a getDisplayName() method, the code will break.
Available Fixes
Change the type-hint for the parameter:
Add an additional type-check:
Add the method to the interface: