Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
28 | class CliMenu |
||
29 | { |
||
30 | /** |
||
31 | * @var TerminalInterface |
||
32 | */ |
||
33 | protected $terminal; |
||
34 | |||
35 | /** |
||
36 | * @var MenuStyle |
||
37 | */ |
||
38 | protected $style; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | protected $title; |
||
44 | |||
45 | /** |
||
46 | * @var MenuItemInterface[] |
||
47 | */ |
||
48 | protected $items = []; |
||
49 | |||
50 | /** |
||
51 | * @var int |
||
52 | */ |
||
53 | protected $selectedItem; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | protected $open = false; |
||
59 | |||
60 | /** |
||
61 | * @var CliMenu|null |
||
62 | */ |
||
63 | protected $parent; |
||
64 | |||
65 | /** |
||
66 | * @var Frame|null |
||
67 | */ |
||
68 | private $currentFrame; |
||
69 | |||
70 | /** |
||
71 | * @param string $title |
||
72 | * @param array $items |
||
73 | * @param TerminalInterface|null $terminal |
||
74 | * @param MenuStyle|null $style |
||
75 | * |
||
76 | * @throws InvalidTerminalException |
||
77 | */ |
||
78 | public function __construct( |
||
91 | |||
92 | /** |
||
93 | * Configure the terminal to work with CliMenu |
||
94 | * |
||
95 | * @throws InvalidTerminalException |
||
96 | */ |
||
97 | protected function configureTerminal() |
||
105 | |||
106 | /** |
||
107 | * Revert changes made to the terminal |
||
108 | * |
||
109 | * @throws InvalidTerminalException |
||
110 | */ |
||
111 | protected function tearDownTerminal() |
||
118 | |||
119 | private function assertTerminalIsValidTTY() |
||
127 | |||
128 | /** |
||
129 | * @param CliMenu $parent |
||
130 | */ |
||
131 | public function setParent(CliMenu $parent) |
||
135 | |||
136 | /** |
||
137 | * @return CliMenu|null |
||
138 | */ |
||
139 | public function getParent() |
||
143 | |||
144 | /** |
||
145 | * @return TerminalInterface |
||
146 | */ |
||
147 | public function getTerminal() |
||
151 | |||
152 | /** |
||
153 | * @return bool |
||
154 | */ |
||
155 | public function isOpen() |
||
159 | |||
160 | /** |
||
161 | * Add a new Item to the listing |
||
162 | * |
||
163 | * @param MenuItemInterface $item |
||
164 | */ |
||
165 | public function addItem(MenuItemInterface $item) |
||
173 | |||
174 | /** |
||
175 | * Set the selected pointer to the first selectable item |
||
176 | */ |
||
177 | private function selectFirstItem() |
||
186 | |||
187 | /** |
||
188 | * Display menu and capture input |
||
189 | */ |
||
190 | private function display() |
||
207 | |||
208 | /** |
||
209 | * Move the selection in a given direction, up / down |
||
210 | * |
||
211 | * @param $direction |
||
212 | */ |
||
213 | protected function moveSelection($direction) |
||
231 | |||
232 | /** |
||
233 | * @return MenuItemInterface |
||
234 | */ |
||
235 | public function getSelectedItem() |
||
239 | |||
240 | /** |
||
241 | * Execute the current item |
||
242 | */ |
||
243 | protected function executeCurrentItem() |
||
252 | |||
253 | /** |
||
254 | * Redraw the menu |
||
255 | */ |
||
256 | public function redraw() |
||
264 | |||
265 | /** |
||
266 | * Draw the menu to STDOUT |
||
267 | */ |
||
268 | protected function draw() |
||
297 | |||
298 | /** |
||
299 | * Draw a menu item |
||
300 | * |
||
301 | * @param MenuItemInterface $item |
||
302 | * @param bool|false $selected |
||
303 | * @return array |
||
304 | */ |
||
305 | protected function drawMenuItem(MenuItemInterface $item, $selected = false) |
||
330 | |||
331 | /** |
||
332 | * @throws InvalidTerminalException |
||
333 | */ |
||
334 | public function open() |
||
344 | |||
345 | /** |
||
346 | * Close the menu |
||
347 | * |
||
348 | * @throws InvalidTerminalException |
||
349 | */ |
||
350 | public function close() |
||
361 | |||
362 | /** |
||
363 | * @throws InvalidTerminalException |
||
364 | */ |
||
365 | public function closeThis() |
||
371 | |||
372 | /** |
||
373 | * @return MenuItemInterface[] |
||
374 | */ |
||
375 | public function getItems() |
||
379 | |||
380 | /** |
||
381 | * @param MenuItemInterface $item |
||
382 | */ |
||
383 | public function removeItem(MenuItemInterface $item) |
||
394 | |||
395 | /** |
||
396 | * @return MenuStyle |
||
397 | */ |
||
398 | public function getStyle() |
||
402 | |||
403 | public function getCurrentFrame() |
||
407 | |||
408 | /** |
||
409 | * @param string $text |
||
410 | * @return Flash |
||
411 | */ |
||
412 | View Code Duplication | public function flash($text) |
|
422 | |||
423 | /** |
||
424 | * @param string $text |
||
425 | * @return Confirm |
||
426 | */ |
||
427 | View Code Duplication | public function confirm($text) |
|
437 | |||
438 | View Code Duplication | public function askNumber() : Number |
|
446 | |||
447 | View Code Duplication | public function askText() : Text |
|
455 | |||
456 | View Code Duplication | public function askPassword() : Password |
|
464 | |||
465 | private function guardSingleLine($text) |
||
471 | } |
||
472 |
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.