Total Complexity | 53 |
Total Lines | 424 |
Duplicated Lines | 0 % |
Changes | 0 |
Complex classes like CliMenuBuilder 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.
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 CliMenuBuilder, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
23 | class CliMenuBuilder |
||
24 | { |
||
25 | /** |
||
26 | * @var CliMenu |
||
27 | */ |
||
28 | private $menu; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $goBackButtonText = 'Go Back'; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $exitButtonText = 'Exit'; |
||
39 | |||
40 | /** |
||
41 | * @var MenuStyle |
||
42 | */ |
||
43 | private $style; |
||
44 | |||
45 | /** |
||
46 | * @var Terminal |
||
47 | */ |
||
48 | private $terminal; |
||
49 | |||
50 | /** |
||
51 | * @var bool |
||
52 | */ |
||
53 | private $disableDefaultItems = false; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $disabled = false; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $subMenu = false; |
||
64 | |||
65 | public function __construct(Terminal $terminal = null) |
||
66 | { |
||
67 | $this->terminal = $terminal ?? TerminalFactory::fromSystem(); |
||
68 | $this->style = new MenuStyle($this->terminal); |
||
69 | $this->menu = new CliMenu(null, [], $this->terminal, $this->style); |
||
70 | } |
||
71 | |||
72 | public static function newSubMenu(Terminal $terminal) : self |
||
73 | { |
||
74 | $instance = new self($terminal); |
||
75 | $instance->subMenu = true; |
||
76 | |||
77 | return $instance; |
||
78 | } |
||
79 | |||
80 | public function setTitle(string $title) : self |
||
81 | { |
||
82 | $this->menu->setTitle($title); |
||
83 | |||
84 | return $this; |
||
85 | } |
||
86 | |||
87 | public function addMenuItem(MenuItemInterface $item) : self |
||
88 | { |
||
89 | $this->menu->addItem($item); |
||
90 | |||
91 | $this->processItemShortcut($item); |
||
92 | |||
93 | return $this; |
||
94 | } |
||
95 | |||
96 | public function addItem( |
||
97 | string $text, |
||
98 | callable $itemCallable, |
||
99 | bool $showItemExtra = false, |
||
100 | bool $disabled = false |
||
101 | ) : self { |
||
102 | $this->addMenuItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); |
||
103 | |||
104 | return $this; |
||
105 | } |
||
106 | |||
107 | public function addItems(array $items) : self |
||
108 | { |
||
109 | foreach ($items as $item) { |
||
110 | $this->addItem(...$item); |
||
|
|||
111 | } |
||
112 | |||
113 | return $this; |
||
114 | } |
||
115 | |||
116 | public function addStaticItem(string $text) : self |
||
117 | { |
||
118 | $this->addMenuItem(new StaticItem($text)); |
||
119 | |||
120 | return $this; |
||
121 | } |
||
122 | |||
123 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
124 | { |
||
125 | $this->addMenuItem(new LineBreakItem($breakChar, $lines)); |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
131 | { |
||
132 | $this->addMenuItem(new AsciiArtItem($art, $position, $alt)); |
||
133 | |||
134 | return $this; |
||
135 | } |
||
136 | |||
137 | public function addSubMenu(string $text, \Closure $callback) : self |
||
138 | { |
||
139 | $builder = self::newSubMenu($this->terminal); |
||
140 | |||
141 | $callback = $callback->bindTo($builder); |
||
142 | $callback($builder); |
||
143 | |||
144 | $menu = $builder->build(); |
||
145 | $menu->setParent($this->menu); |
||
146 | |||
147 | //we apply the parent theme if nothing was changed |
||
148 | //if no styles were changed in this sub-menu |
||
149 | if (!$menu->getStyle()->hasChangedFromDefaults()) { |
||
150 | $menu->setStyle($this->menu->getStyle()); |
||
151 | } |
||
152 | |||
153 | $this->menu->addItem($item = new MenuMenuItem( |
||
154 | $text, |
||
155 | $menu, |
||
156 | $builder->isMenuDisabled() |
||
157 | )); |
||
158 | |||
159 | $this->processItemShortcut($item); |
||
160 | |||
161 | return $this; |
||
162 | } |
||
163 | |||
164 | public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : self |
||
165 | { |
||
166 | $menu = $builder->build(); |
||
167 | $menu->setParent($this->menu); |
||
168 | |||
169 | //we apply the parent theme if nothing was changed |
||
170 | //if no styles were changed in this sub-menu |
||
171 | if (!$menu->getStyle()->hasChangedFromDefaults()) { |
||
172 | $menu->setStyle($this->menu->getStyle()); |
||
173 | } |
||
174 | |||
175 | $this->menu->addItem($item = new MenuMenuItem( |
||
176 | $text, |
||
177 | $menu, |
||
178 | $builder->isMenuDisabled() |
||
179 | )); |
||
180 | |||
181 | $this->processItemShortcut($item); |
||
182 | |||
183 | return $this; |
||
184 | } |
||
185 | |||
186 | private function extractShortcut(string $title) : ?string |
||
187 | { |
||
188 | preg_match('/\[(.)\]/', $title, $match); |
||
189 | return isset($match[1]) ? strtolower($match[1]) : null; |
||
190 | } |
||
191 | |||
192 | private function processItemShortcut(MenuItemInterface $item) : void |
||
193 | { |
||
194 | $this->processIndividualShortcut($item, function (CliMenu $menu) use ($item) { |
||
195 | $menu->executeAsSelected($item); |
||
196 | }); |
||
197 | } |
||
198 | |||
199 | private function processSplitItemShortcuts(SplitItem $splitItem) : void |
||
200 | { |
||
201 | foreach ($splitItem->getItems() as $item) { |
||
202 | $this->processIndividualShortcut($item, function (CliMenu $menu) use ($splitItem, $item) { |
||
203 | $current = $splitItem->getSelectedItemIndex(); |
||
204 | |||
205 | $splitItem->setSelectedItemIndex( |
||
206 | array_search($item, $splitItem->getItems(), true) |
||
207 | ); |
||
208 | |||
209 | $menu->executeAsSelected($splitItem); |
||
210 | |||
211 | $splitItem->setSelectedItemIndex($current); |
||
212 | }); |
||
213 | } |
||
214 | } |
||
215 | |||
216 | private function processIndividualShortcut(MenuItemInterface $item, callable $callback) : void |
||
217 | { |
||
218 | if ($shortcut = $this->extractShortcut($item->getText())) { |
||
219 | $this->menu->addCustomControlMapping( |
||
220 | $shortcut, |
||
221 | $callback |
||
222 | ); |
||
223 | } |
||
224 | } |
||
225 | |||
226 | public function addSplitItem(\Closure $callback) : self |
||
227 | { |
||
228 | $builder = new SplitItemBuilder($this->menu); |
||
229 | |||
230 | $callback = $callback->bindTo($builder); |
||
231 | $callback($builder); |
||
232 | |||
233 | $this->menu->addItem($splitItem = $builder->build()); |
||
234 | |||
235 | $this->processSplitItemShortcuts($splitItem); |
||
236 | |||
237 | return $this; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * Disable a submenu |
||
242 | * |
||
243 | * @throws \InvalidArgumentException |
||
244 | */ |
||
245 | public function disableMenu() : self |
||
246 | { |
||
247 | if (!$this->subMenu) { |
||
248 | throw new \InvalidArgumentException( |
||
249 | 'You can\'t disable the root menu' |
||
250 | ); |
||
251 | } |
||
252 | |||
253 | $this->disabled = true; |
||
254 | |||
255 | return $this; |
||
256 | } |
||
257 | |||
258 | public function isMenuDisabled() : bool |
||
259 | { |
||
260 | return $this->disabled; |
||
261 | } |
||
262 | |||
263 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
264 | { |
||
265 | $this->goBackButtonText = $goBackButtonTest; |
||
266 | |||
267 | return $this; |
||
268 | } |
||
269 | |||
270 | public function setExitButtonText(string $exitButtonText) : self |
||
271 | { |
||
272 | $this->exitButtonText = $exitButtonText; |
||
273 | |||
274 | return $this; |
||
275 | } |
||
276 | |||
277 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
278 | { |
||
279 | $this->style->setBg($colour, $fallback); |
||
280 | |||
281 | return $this; |
||
282 | } |
||
283 | |||
284 | public function setForegroundColour(string $colour, string $fallback = null) : self |
||
285 | { |
||
286 | $this->style->setFg($colour, $fallback); |
||
287 | |||
288 | return $this; |
||
289 | } |
||
290 | |||
291 | public function setWidth(int $width) : self |
||
296 | } |
||
297 | |||
298 | public function setPadding(int $topBottom, int $leftRight = null) : self |
||
299 | { |
||
300 | $this->style->setPadding($topBottom, $leftRight); |
||
301 | |||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | public function setPaddingTopBottom(int $topBottom) : self |
||
306 | { |
||
307 | $this->style->setPaddingTopBottom($topBottom); |
||
308 | |||
309 | return $this; |
||
310 | } |
||
311 | |||
312 | public function setPaddingLeftRight(int $leftRight) : self |
||
313 | { |
||
314 | $this->style->setPaddingLeftRight($leftRight); |
||
315 | |||
316 | return $this; |
||
317 | } |
||
318 | |||
319 | public function setMarginAuto() : self |
||
320 | { |
||
321 | $this->style->setMarginAuto(); |
||
322 | |||
323 | return $this; |
||
324 | } |
||
325 | |||
326 | public function setMargin(int $margin) : self |
||
327 | { |
||
328 | $this->style->setMargin($margin); |
||
329 | |||
330 | return $this; |
||
331 | } |
||
332 | |||
333 | public function setUnselectedMarker(string $marker) : self |
||
334 | { |
||
335 | $this->style->setUnselectedMarker($marker); |
||
336 | |||
337 | return $this; |
||
338 | } |
||
339 | |||
340 | public function setSelectedMarker(string $marker) : self |
||
341 | { |
||
342 | $this->style->setSelectedMarker($marker); |
||
343 | |||
344 | return $this; |
||
345 | } |
||
346 | |||
347 | public function setItemExtra(string $extra) : self |
||
348 | { |
||
349 | $this->style->setItemExtra($extra); |
||
350 | |||
351 | return $this; |
||
352 | } |
||
353 | |||
354 | public function setTitleSeparator(string $separator) : self |
||
359 | } |
||
360 | |||
361 | public function setBorder(int $top, $right = null, $bottom = null, $left = null, string $colour = null) : self |
||
362 | { |
||
363 | $this->style->setBorder($top, $right, $bottom, $left, $colour); |
||
364 | |||
365 | return $this; |
||
366 | } |
||
367 | |||
368 | public function setBorderTopWidth(int $width) : self |
||
369 | { |
||
370 | $this->style->setBorderTopWidth($width); |
||
371 | |||
372 | return $this; |
||
373 | } |
||
374 | |||
375 | public function setBorderRightWidth(int $width) : self |
||
380 | } |
||
381 | |||
382 | public function setBorderBottomWidth(int $width) : self |
||
383 | { |
||
384 | $this->style->setBorderBottomWidth($width); |
||
385 | |||
386 | return $this; |
||
387 | } |
||
388 | |||
389 | public function setBorderLeftWidth(int $width) : self |
||
390 | { |
||
391 | $this->style->setBorderLeftWidth($width); |
||
392 | |||
393 | return $this; |
||
394 | } |
||
395 | |||
396 | public function setBorderColour(string $colour, $fallback = null) : self |
||
401 | } |
||
402 | |||
403 | public function getStyle() : MenuStyle |
||
404 | { |
||
405 | return $this->style; |
||
406 | } |
||
407 | |||
408 | public function getTerminal() : Terminal |
||
409 | { |
||
410 | return $this->terminal; |
||
411 | } |
||
412 | |||
413 | private function getDefaultItems() : array |
||
414 | { |
||
415 | $actions = []; |
||
416 | if ($this->subMenu) { |
||
417 | $actions[] = new SelectableItem($this->goBackButtonText, new GoBackAction); |
||
418 | } |
||
419 | |||
420 | $actions[] = new SelectableItem($this->exitButtonText, new ExitAction); |
||
421 | return $actions; |
||
422 | } |
||
423 | |||
424 | public function disableDefaultItems() : self |
||
429 | } |
||
430 | |||
431 | private function itemsHaveExtra(array $items) : bool |
||
432 | { |
||
433 | return !empty(array_filter($items, function (MenuItemInterface $item) { |
||
434 | return $item->showsItemExtra(); |
||
435 | })); |
||
436 | } |
||
437 | |||
438 | public function build() : CliMenu |
||
439 | { |
||
440 | if (!$this->disableDefaultItems) { |
||
441 | $this->menu->addItems($this->getDefaultItems()); |
||
442 | } |
||
443 | |||
444 | $this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems())); |
||
445 | |||
447 | } |
||
448 | } |
||
449 |
This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.