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 |
||
20 | class CliMenu |
||
21 | { |
||
22 | /** |
||
23 | * @var TerminalInterface |
||
24 | */ |
||
25 | protected $terminal; |
||
26 | |||
27 | /** |
||
28 | * @var MenuStyle |
||
29 | */ |
||
30 | protected $style; |
||
31 | |||
32 | /** |
||
33 | * @var string |
||
34 | */ |
||
35 | protected $title; |
||
36 | |||
37 | /** |
||
38 | * @var MenuItemInterface[] |
||
39 | */ |
||
40 | protected $items = []; |
||
41 | |||
42 | /** |
||
43 | * @var int |
||
44 | */ |
||
45 | protected $selectedItem; |
||
46 | |||
47 | /** |
||
48 | * @var bool |
||
49 | */ |
||
50 | protected $open = false; |
||
51 | |||
52 | /** |
||
53 | * @var string |
||
54 | */ |
||
55 | private $allowedConsumer = 'PhpSchool\CliMenu\CliMenuBuilder'; |
||
56 | |||
57 | /** |
||
58 | * @var CliMenu|null |
||
59 | */ |
||
60 | protected $parent; |
||
61 | |||
62 | /** |
||
63 | * @param string $title |
||
64 | * @param array $items |
||
65 | * @param TerminalInterface|null $terminal |
||
66 | * @param MenuStyle|null $style |
||
67 | * @throws InvalidInstantiationException |
||
68 | * @throws InvalidTerminalException |
||
69 | */ |
||
70 | public function __construct( |
||
71 | $title, |
||
72 | array $items, |
||
73 | TerminalInterface $terminal = null, |
||
74 | MenuStyle $style = null |
||
75 | ) { |
||
76 | $builder = debug_backtrace(); |
||
77 | View Code Duplication | if (count($builder) < 2 || !isset($builder[1]['class']) || $builder[1]['class'] !== $this->allowedConsumer) { |
|
|
|||
78 | throw new InvalidInstantiationException( |
||
79 | sprintf('The CliMenu must be instantiated by "%s"', $this->allowedConsumer) |
||
80 | ); |
||
81 | } |
||
82 | |||
83 | $this->title = $title; |
||
84 | $this->items = $items; |
||
85 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
86 | $this->style = $style ?: new MenuStyle(); |
||
87 | |||
88 | $this->selectFirstItem(); |
||
89 | } |
||
90 | |||
91 | /** |
||
92 | * Configure the terminal to work with CliMenu |
||
93 | * |
||
94 | * @throws InvalidTerminalException |
||
95 | */ |
||
96 | protected function configureTerminal() |
||
97 | { |
||
98 | $this->assertTerminalIsValidTTY(); |
||
99 | |||
100 | $this->terminal->setCanonicalMode(); |
||
101 | $this->terminal->disableCursor(); |
||
102 | $this->terminal->clear(); |
||
103 | } |
||
104 | |||
105 | /** |
||
106 | * Revert changes made to the terminal |
||
107 | * |
||
108 | * @throws InvalidTerminalException |
||
109 | */ |
||
110 | protected function tearDownTerminal() |
||
111 | { |
||
112 | $this->assertTerminalIsValidTTY(); |
||
113 | |||
114 | $this->terminal->setCanonicalMode(false); |
||
115 | $this->terminal->enableCursor(); |
||
116 | } |
||
117 | |||
118 | private function assertTerminalIsValidTTY() |
||
119 | { |
||
120 | if (!$this->terminal->isTTY()) { |
||
121 | throw new InvalidTerminalException( |
||
122 | sprintf('Terminal "%s" is not a valid TTY', $this->terminal->getDetails()) |
||
123 | ); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | /** |
||
128 | * @param CliMenu $parent |
||
129 | */ |
||
130 | public function setParent(CliMenu $parent) |
||
134 | |||
135 | /** |
||
136 | * @return CliMenu|null |
||
137 | */ |
||
138 | public function getParent() |
||
142 | |||
143 | /** |
||
144 | * @return TerminalInterface |
||
145 | */ |
||
146 | public function getTerminal() |
||
150 | |||
151 | /** |
||
152 | * @return bool |
||
153 | */ |
||
154 | public function isOpen() |
||
158 | |||
159 | /** |
||
160 | * Add a new Item to the listing |
||
161 | * |
||
162 | * @param MenuItemInterface $item |
||
163 | */ |
||
164 | public function addItem(MenuItemInterface $item) |
||
168 | |||
169 | /** |
||
170 | * Set the selected pointer to the first selectable item |
||
171 | */ |
||
172 | private function selectFirstItem() |
||
181 | |||
182 | /** |
||
183 | * Display menu and capture input |
||
184 | */ |
||
185 | private function display() |
||
202 | |||
203 | /** |
||
204 | * Move the selection in a given direction, up / down |
||
205 | * |
||
206 | * @param $direction |
||
207 | */ |
||
208 | protected function moveSelection($direction) |
||
227 | |||
228 | /** |
||
229 | * @return MenuItemInterface |
||
230 | */ |
||
231 | public function getSelectedItem() |
||
235 | |||
236 | /** |
||
237 | * Execute the current item |
||
238 | */ |
||
239 | protected function executeCurrentItem() |
||
248 | |||
249 | /** |
||
250 | * Draw the menu to STDOUT |
||
251 | */ |
||
252 | protected function draw() |
||
273 | |||
274 | /** |
||
275 | * Draw a menu item |
||
276 | * |
||
277 | * @param MenuItemInterface $item |
||
278 | * @param bool|false $selected |
||
279 | */ |
||
280 | protected function drawMenuItem(MenuItemInterface $item, $selected = false) |
||
307 | |||
308 | /** |
||
309 | * @throws InvalidTerminalException |
||
310 | */ |
||
311 | public function open() |
||
321 | |||
322 | /** |
||
323 | * Close the menu |
||
324 | * |
||
325 | * @throws InvalidTerminalException |
||
326 | */ |
||
327 | public function close() |
||
338 | |||
339 | /** |
||
340 | * @throws InvalidTerminalException |
||
341 | */ |
||
342 | public function closeThis() |
||
348 | } |
||
349 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.