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 |
||
29 | class CliMenu |
||
30 | { |
||
31 | /** |
||
32 | * @var Terminal |
||
33 | */ |
||
34 | protected $terminal; |
||
35 | |||
36 | /** |
||
37 | * @var MenuStyle |
||
38 | */ |
||
39 | protected $style; |
||
40 | |||
41 | /** |
||
42 | * @var ?string |
||
43 | */ |
||
44 | protected $title; |
||
45 | |||
46 | /** |
||
47 | * @var MenuItemInterface[] |
||
48 | */ |
||
49 | protected $items = []; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | protected $selectedItem; |
||
55 | |||
56 | /** |
||
57 | * @var bool |
||
58 | */ |
||
59 | protected $open = false; |
||
60 | |||
61 | /** |
||
62 | * @var CliMenu|null |
||
63 | */ |
||
64 | protected $parent; |
||
65 | |||
66 | /** |
||
67 | * @var Frame |
||
68 | */ |
||
69 | private $currentFrame; |
||
70 | |||
71 | public function __construct( |
||
84 | |||
85 | /** |
||
86 | * Configure the terminal to work with CliMenu |
||
87 | */ |
||
88 | protected function configureTerminal() : void |
||
97 | |||
98 | /** |
||
99 | * Revert changes made to the terminal |
||
100 | */ |
||
101 | protected function tearDownTerminal() : void |
||
105 | |||
106 | private function assertTerminalIsValidTTY() : void |
||
112 | |||
113 | |||
114 | public function setParent(CliMenu $parent) : void |
||
118 | |||
119 | public function getParent() : ?CliMenu |
||
123 | |||
124 | public function getTerminal() : Terminal |
||
128 | |||
129 | public function isOpen() : bool |
||
133 | |||
134 | /** |
||
135 | * Add a new Item to the menu |
||
136 | */ |
||
137 | public function addItem(MenuItemInterface $item) : void |
||
145 | |||
146 | /** |
||
147 | * Set the selected pointer to the first selectable item |
||
148 | */ |
||
149 | private function selectFirstItem() : void |
||
158 | |||
159 | /** |
||
160 | * Display menu and capture input |
||
161 | */ |
||
162 | private function display() : void |
||
193 | |||
194 | /** |
||
195 | * Move the selection in a given direction, up / down |
||
196 | */ |
||
197 | protected function moveSelection(string $direction) : void |
||
215 | |||
216 | public function getSelectedItem() : MenuItemInterface |
||
220 | |||
221 | /** |
||
222 | * Execute the current item |
||
223 | */ |
||
224 | protected function executeCurrentItem() : void |
||
233 | |||
234 | /** |
||
235 | * Redraw the menu |
||
236 | */ |
||
237 | public function redraw() : void |
||
242 | |||
243 | private function assertOpen() : void |
||
249 | |||
250 | /** |
||
251 | * Draw the menu to STDOUT |
||
252 | */ |
||
253 | protected function draw() : void |
||
282 | |||
283 | /** |
||
284 | * Draw a menu item |
||
285 | */ |
||
286 | protected function drawMenuItem(MenuItemInterface $item, bool $selected = false) : array |
||
311 | |||
312 | /** |
||
313 | * @throws InvalidTerminalException |
||
314 | */ |
||
315 | public function open() : void |
||
325 | |||
326 | /** |
||
327 | * Close the menu |
||
328 | * |
||
329 | * @throws InvalidTerminalException |
||
330 | */ |
||
331 | public function close() : void |
||
342 | |||
343 | public function closeThis() : void |
||
349 | |||
350 | /** |
||
351 | * @return MenuItemInterface[] |
||
352 | */ |
||
353 | public function getItems() : array |
||
357 | |||
358 | public function removeItem(MenuItemInterface $item) : void |
||
369 | |||
370 | public function getStyle() : MenuStyle |
||
374 | |||
375 | public function getCurrentFrame() : Frame |
||
379 | |||
380 | public function flash(string $text) : Flash |
||
390 | |||
391 | public function confirm($text) : Confirm |
||
401 | |||
402 | public function askNumber() : Number |
||
412 | |||
413 | public function askText() : Text |
||
423 | |||
424 | public function askPassword() : Password |
||
434 | |||
435 | private function guardSingleLine($text) |
||
441 | } |
||
442 |
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a mixed type is assigned to a property that is type hinted more strictly.
For example, imagine you have a variable
$accountId
that can either hold an Id object or false (if there is no account id yet). Your code now assigns that value to theid
property of an instance of theAccount
class. This class holds a proper account, so the id value must no longer be false.Either this assignment is in error or a type check should be added for that assignment.