Total Complexity | 74 |
Total Lines | 575 |
Duplicated Lines | 0 % |
Changes | 7 | ||
Bugs | 0 | Features | 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 |
||
30 | class CliMenuBuilder |
||
31 | { |
||
32 | /** |
||
33 | * @var CliMenu |
||
34 | */ |
||
35 | private $menu; |
||
36 | |||
37 | /** |
||
38 | * @var string |
||
39 | */ |
||
40 | private $goBackButtonText = 'Go Back'; |
||
41 | |||
42 | /** |
||
43 | * @var string |
||
44 | */ |
||
45 | private $exitButtonText = 'Exit'; |
||
46 | |||
47 | /** |
||
48 | * @var MenuStyle |
||
49 | */ |
||
50 | private $style; |
||
51 | |||
52 | /** |
||
53 | * @var Terminal |
||
54 | */ |
||
55 | private $terminal; |
||
56 | |||
57 | /** |
||
58 | * @var bool |
||
59 | */ |
||
60 | private $disableDefaultItems = false; |
||
61 | |||
62 | /** |
||
63 | * @var bool |
||
64 | */ |
||
65 | private $disabled = false; |
||
66 | |||
67 | /** |
||
68 | * Whether or not to auto create keyboard shortcuts for items |
||
69 | * when they contain square brackets. Eg: [M]y item |
||
70 | * |
||
71 | * @var bool |
||
72 | */ |
||
73 | private $autoShortcuts = false; |
||
74 | |||
75 | /** |
||
76 | * Regex to auto match for shortcuts defaults to looking |
||
77 | * for a single character encased in square brackets |
||
78 | * |
||
79 | * @var string |
||
80 | */ |
||
81 | private $autoShortcutsRegex = '/\[(.)\]/'; |
||
82 | |||
83 | /** |
||
84 | * @var bool |
||
85 | */ |
||
86 | private $subMenu = false; |
||
87 | |||
88 | public function __construct(Terminal $terminal = null) |
||
89 | { |
||
90 | $this->terminal = $terminal ?? TerminalFactory::fromSystem(); |
||
91 | $this->style = new MenuStyle($this->terminal); |
||
92 | $this->menu = new CliMenu(null, [], $this->terminal, $this->style); |
||
93 | } |
||
94 | |||
95 | public static function newSubMenu(Terminal $terminal) : self |
||
96 | { |
||
97 | $instance = new self($terminal); |
||
98 | $instance->subMenu = true; |
||
99 | |||
100 | return $instance; |
||
101 | } |
||
102 | |||
103 | public function setTitle(string $title) : self |
||
104 | { |
||
105 | $this->menu->setTitle($title); |
||
106 | |||
107 | return $this; |
||
108 | } |
||
109 | |||
110 | public function addMenuItem(MenuItemInterface $item) : self |
||
111 | { |
||
112 | $this->menu->addItem($item); |
||
113 | |||
114 | $this->processItemShortcut($item); |
||
115 | |||
116 | return $this; |
||
117 | } |
||
118 | |||
119 | public function addItem( |
||
120 | string $text, |
||
121 | callable $itemCallable, |
||
122 | bool $showItemExtra = false, |
||
123 | bool $disabled = false |
||
124 | ) : self { |
||
125 | $this->addMenuItem(new SelectableItem($text, $itemCallable, $showItemExtra, $disabled)); |
||
126 | |||
127 | return $this; |
||
128 | } |
||
129 | |||
130 | public function addItems(array $items) : self |
||
131 | { |
||
132 | foreach ($items as $item) { |
||
133 | $this->addItem(...$item); |
||
|
|||
134 | } |
||
135 | |||
136 | return $this; |
||
137 | } |
||
138 | |||
139 | public function addCheckboxItem( |
||
140 | string $text, |
||
141 | callable $itemCallable, |
||
142 | bool $showItemExtra = false, |
||
143 | bool $disabled = false |
||
144 | ) : self { |
||
145 | $this->addMenuItem(new CheckboxItem($text, $itemCallable, $showItemExtra, $disabled)); |
||
146 | |||
147 | return $this; |
||
148 | } |
||
149 | |||
150 | public function addRadioItem( |
||
151 | string $text, |
||
152 | callable $itemCallable, |
||
153 | bool $showItemExtra = false, |
||
154 | bool $disabled = false |
||
155 | ) : self { |
||
156 | $this->addMenuItem(new RadioItem($text, $itemCallable, $showItemExtra, $disabled)); |
||
157 | |||
158 | return $this; |
||
159 | } |
||
160 | |||
161 | public function addStaticItem(string $text) : self |
||
162 | { |
||
163 | $this->addMenuItem(new StaticItem($text)); |
||
164 | |||
165 | return $this; |
||
166 | } |
||
167 | |||
168 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
169 | { |
||
170 | $this->addMenuItem(new LineBreakItem($breakChar, $lines)); |
||
171 | |||
172 | return $this; |
||
173 | } |
||
174 | |||
175 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
176 | { |
||
177 | $this->addMenuItem(new AsciiArtItem($art, $position, $alt)); |
||
178 | |||
179 | return $this; |
||
180 | } |
||
181 | |||
182 | public function addSubMenu(string $text, \Closure $callback) : self |
||
183 | { |
||
184 | $builder = self::newSubMenu($this->terminal); |
||
185 | |||
186 | if ($this->autoShortcuts) { |
||
187 | $builder->enableAutoShortcuts($this->autoShortcutsRegex); |
||
188 | } |
||
189 | |||
190 | $callback($builder); |
||
191 | |||
192 | $menu = $builder->build(); |
||
193 | $menu->setParent($this->menu); |
||
194 | |||
195 | $this->menu->addItem($item = new MenuMenuItem( |
||
196 | $text, |
||
197 | $menu, |
||
198 | $builder->isMenuDisabled() |
||
199 | )); |
||
200 | |||
201 | $this->processItemShortcut($item); |
||
202 | |||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | public function addSubMenuFromBuilder(string $text, CliMenuBuilder $builder) : self |
||
207 | { |
||
208 | $menu = $builder->build(); |
||
209 | $menu->setParent($this->menu); |
||
210 | |||
211 | $this->menu->addItem($item = new MenuMenuItem( |
||
212 | $text, |
||
213 | $menu, |
||
214 | $builder->isMenuDisabled() |
||
215 | )); |
||
216 | |||
217 | $this->processItemShortcut($item); |
||
218 | |||
219 | return $this; |
||
220 | } |
||
221 | |||
222 | public function enableAutoShortcuts(string $regex = null) : self |
||
223 | { |
||
224 | $this->autoShortcuts = true; |
||
225 | |||
226 | if (null !== $regex) { |
||
227 | $this->autoShortcutsRegex = $regex; |
||
228 | } |
||
229 | |||
230 | return $this; |
||
231 | } |
||
232 | |||
233 | private function extractShortcut(string $title) : ?string |
||
234 | { |
||
235 | preg_match($this->autoShortcutsRegex, $title, $match); |
||
236 | |||
237 | if (!isset($match[1])) { |
||
238 | return null; |
||
239 | } |
||
240 | |||
241 | if (mb_strlen($match[1]) > 1) { |
||
242 | throw InvalidShortcutException::fromShortcut($match[1]); |
||
243 | } |
||
244 | |||
245 | return isset($match[1]) ? strtolower($match[1]) : null; |
||
246 | } |
||
247 | |||
248 | private function processItemShortcut(MenuItemInterface $item) : void |
||
249 | { |
||
250 | $this->processIndividualShortcut($item, function (CliMenu $menu) use ($item) { |
||
251 | $menu->executeAsSelected($item); |
||
252 | }); |
||
253 | } |
||
254 | |||
255 | private function processSplitItemShortcuts(SplitItem $splitItem) : void |
||
256 | { |
||
257 | foreach ($splitItem->getItems() as $item) { |
||
258 | $this->processIndividualShortcut($item, function (CliMenu $menu) use ($splitItem, $item) { |
||
259 | $current = $splitItem->getSelectedItemIndex(); |
||
260 | |||
261 | $splitItem->setSelectedItemIndex( |
||
262 | array_search($item, $splitItem->getItems(), true) |
||
263 | ); |
||
264 | |||
265 | $menu->executeAsSelected($splitItem); |
||
266 | |||
267 | if ($current !== null) { |
||
268 | $splitItem->setSelectedItemIndex($current); |
||
269 | } |
||
270 | }); |
||
271 | } |
||
272 | } |
||
273 | |||
274 | private function processIndividualShortcut(MenuItemInterface $item, callable $callback) : void |
||
275 | { |
||
276 | if (!$this->autoShortcuts) { |
||
277 | return; |
||
278 | } |
||
279 | |||
280 | if ($shortcut = $this->extractShortcut($item->getText())) { |
||
281 | $this->menu->addCustomControlMapping( |
||
282 | $shortcut, |
||
283 | $callback |
||
284 | ); |
||
285 | } |
||
286 | } |
||
287 | |||
288 | public function addSplitItem(\Closure $callback) : self |
||
289 | { |
||
290 | $builder = new SplitItemBuilder($this->menu); |
||
291 | |||
292 | if ($this->autoShortcuts) { |
||
293 | $builder->enableAutoShortcuts($this->autoShortcutsRegex); |
||
294 | } |
||
295 | |||
296 | $callback($builder); |
||
297 | |||
298 | $this->menu->addItem($splitItem = $builder->build()); |
||
299 | |||
300 | $this->processSplitItemShortcuts($splitItem); |
||
301 | |||
302 | return $this; |
||
303 | } |
||
304 | |||
305 | /** |
||
306 | * Disable a submenu |
||
307 | * |
||
308 | * @throws \InvalidArgumentException |
||
309 | */ |
||
310 | public function disableMenu() : self |
||
311 | { |
||
312 | if (!$this->subMenu) { |
||
313 | throw new \InvalidArgumentException( |
||
314 | 'You can\'t disable the root menu' |
||
315 | ); |
||
316 | } |
||
317 | |||
318 | $this->disabled = true; |
||
319 | |||
320 | return $this; |
||
321 | } |
||
322 | |||
323 | public function isMenuDisabled() : bool |
||
324 | { |
||
325 | return $this->disabled; |
||
326 | } |
||
327 | |||
328 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
329 | { |
||
330 | $this->goBackButtonText = $goBackButtonTest; |
||
331 | |||
332 | return $this; |
||
333 | } |
||
334 | |||
335 | public function setExitButtonText(string $exitButtonText) : self |
||
336 | { |
||
337 | $this->exitButtonText = $exitButtonText; |
||
338 | |||
339 | return $this; |
||
340 | } |
||
341 | |||
342 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
343 | { |
||
344 | $this->style->setBg($colour, $fallback); |
||
345 | |||
346 | return $this; |
||
347 | } |
||
348 | |||
349 | public function setForegroundColour(string $colour, string $fallback = null) : self |
||
350 | { |
||
351 | $this->style->setFg($colour, $fallback); |
||
352 | |||
353 | return $this; |
||
354 | } |
||
355 | |||
356 | public function setWidth(int $width) : self |
||
357 | { |
||
358 | $this->style->setWidth($width); |
||
359 | |||
360 | return $this; |
||
361 | } |
||
362 | |||
363 | public function setPadding(int $topBottom, int $leftRight = null) : self |
||
364 | { |
||
365 | $this->style->setPadding($topBottom, $leftRight); |
||
366 | |||
367 | return $this; |
||
368 | } |
||
369 | |||
370 | public function setPaddingTopBottom(int $topBottom) : self |
||
371 | { |
||
372 | $this->style->setPaddingTopBottom($topBottom); |
||
373 | |||
374 | return $this; |
||
375 | } |
||
376 | |||
377 | public function setPaddingLeftRight(int $leftRight) : self |
||
378 | { |
||
379 | $this->style->setPaddingLeftRight($leftRight); |
||
380 | |||
381 | return $this; |
||
382 | } |
||
383 | |||
384 | public function setMarginAuto() : self |
||
385 | { |
||
386 | $this->style->setMarginAuto(); |
||
387 | |||
388 | return $this; |
||
389 | } |
||
390 | |||
391 | public function setMargin(int $margin) : self |
||
392 | { |
||
393 | $this->style->setMargin($margin); |
||
394 | |||
395 | return $this; |
||
396 | } |
||
397 | |||
398 | public function setItemExtra(string $extra) : self |
||
399 | { |
||
400 | $this->style->setItemExtra($extra); |
||
401 | $this->getSelectableStyle()->setItemExtra($extra); |
||
402 | |||
403 | // if we customise item extra, it means we most likely want to display it |
||
404 | $this->displayExtra(); |
||
405 | |||
406 | return $this; |
||
407 | } |
||
408 | |||
409 | public function setTitleSeparator(string $separator) : self |
||
414 | } |
||
415 | |||
416 | /** |
||
417 | * @param int|string|null $right |
||
418 | * @param int|string|null $bottom |
||
419 | * @param int|string|null $left |
||
420 | */ |
||
421 | public function setBorder(int $top, $right = null, $bottom = null, $left = null, string $colour = null) : self |
||
422 | { |
||
423 | $this->style->setBorder($top, $right, $bottom, $left, $colour); |
||
424 | |||
425 | return $this; |
||
426 | } |
||
427 | |||
428 | public function setBorderTopWidth(int $width) : self |
||
429 | { |
||
430 | $this->style->setBorderTopWidth($width); |
||
431 | |||
432 | return $this; |
||
433 | } |
||
434 | |||
435 | public function setBorderRightWidth(int $width) : self |
||
436 | { |
||
437 | $this->style->setBorderRightWidth($width); |
||
438 | |||
439 | return $this; |
||
440 | } |
||
441 | |||
442 | public function setBorderBottomWidth(int $width) : self |
||
443 | { |
||
444 | $this->style->setBorderBottomWidth($width); |
||
445 | |||
446 | return $this; |
||
447 | } |
||
448 | |||
449 | public function setBorderLeftWidth(int $width) : self |
||
450 | { |
||
451 | $this->style->setBorderLeftWidth($width); |
||
452 | |||
453 | return $this; |
||
454 | } |
||
455 | |||
456 | public function setBorderColour(string $colour, string $fallback = null) : self |
||
457 | { |
||
458 | $this->style->setBorderColour($colour, $fallback); |
||
459 | |||
460 | return $this; |
||
461 | } |
||
462 | |||
463 | public function getStyle() : MenuStyle |
||
464 | { |
||
465 | return $this->style; |
||
466 | } |
||
467 | |||
468 | public function getTerminal() : Terminal |
||
469 | { |
||
470 | return $this->terminal; |
||
471 | } |
||
472 | |||
473 | private function getDefaultItems() : array |
||
474 | { |
||
475 | $actions = []; |
||
476 | if ($this->subMenu) { |
||
477 | $actions[] = new SelectableItem($this->goBackButtonText, new GoBackAction); |
||
478 | } |
||
479 | |||
480 | $actions[] = new SelectableItem($this->exitButtonText, new ExitAction); |
||
481 | return $actions; |
||
482 | } |
||
483 | |||
484 | public function disableDefaultItems() : self |
||
485 | { |
||
486 | $this->disableDefaultItems = true; |
||
487 | |||
488 | return $this; |
||
489 | } |
||
490 | |||
491 | public function displayExtra() : self |
||
492 | { |
||
493 | $this->style->setDisplaysExtra(true); |
||
494 | $this->getSelectableStyle()->setDisplaysExtra(true); |
||
495 | |||
496 | return $this; |
||
497 | } |
||
498 | |||
499 | private function itemsHaveExtra(array $items) : bool |
||
500 | { |
||
501 | return !empty(array_filter($items, function (MenuItemInterface $item) { |
||
502 | return $item->showsItemExtra(); |
||
503 | })); |
||
504 | } |
||
505 | |||
506 | public function build() : CliMenu |
||
507 | { |
||
508 | if (!$this->disableDefaultItems) { |
||
509 | $this->menu->addItems($this->getDefaultItems()); |
||
510 | } |
||
511 | |||
512 | if (!$this->style->getDisplaysExtra()) { |
||
513 | $this->style->setDisplaysExtra($this->itemsHaveExtra($this->menu->getItems())); |
||
514 | } |
||
515 | |||
516 | if (!$this->subMenu) { |
||
517 | $this->menu->propagateStyles(); |
||
518 | } |
||
519 | |||
520 | return $this->menu; |
||
521 | } |
||
522 | |||
523 | public function getDefaultStyle() : DefaultStyle |
||
524 | { |
||
525 | $style = $this->menu->getItemStyle(DefaultStyle::class); |
||
526 | assert($style instanceof DefaultStyle); |
||
527 | return $style; |
||
528 | } |
||
529 | |||
530 | public function setDefaultStyle(DefaultStyle $style) : self |
||
535 | } |
||
536 | |||
537 | public function modifyDefaultStyle(callable $itemCallable) : self |
||
538 | { |
||
539 | $itemCallable($this->getDefaultStyle()); |
||
540 | |||
541 | return $this; |
||
542 | } |
||
543 | |||
544 | public function getSelectableStyle() : SelectableStyle |
||
545 | { |
||
546 | $style = $this->menu->getItemStyle(SelectableStyle::class); |
||
547 | assert($style instanceof SelectableStyle); |
||
548 | return $style; |
||
549 | } |
||
550 | |||
551 | public function setSelectableStyle(SelectableStyle $style) : self |
||
552 | { |
||
553 | $this->menu->setItemStyle($style, SelectableStyle::class); |
||
554 | |||
555 | return $this; |
||
556 | } |
||
557 | |||
558 | public function modifySelectableStyle(callable $itemCallable) : self |
||
563 | } |
||
564 | |||
565 | public function getCheckboxStyle() : CheckboxStyle |
||
566 | { |
||
567 | $style = $this->menu->getItemStyle(CheckboxStyle::class); |
||
568 | assert($style instanceof CheckboxStyle); |
||
569 | return $style; |
||
570 | } |
||
571 | |||
572 | public function setCheckboxStyle(CheckboxStyle $style) : self |
||
577 | } |
||
578 | |||
579 | public function modifyCheckboxStyle(callable $itemCallable) : self |
||
580 | { |
||
581 | $itemCallable($this->getCheckboxStyle()); |
||
582 | |||
583 | return $this; |
||
584 | } |
||
585 | |||
586 | public function getRadioStyle() : RadioStyle |
||
587 | { |
||
588 | $style = $this->menu->getItemStyle(RadioStyle::class); |
||
589 | assert($style instanceof RadioStyle); |
||
590 | return $style; |
||
591 | } |
||
592 | |||
593 | public function setRadioStyle(RadioStyle $style) : self |
||
594 | { |
||
595 | $this->menu->setItemStyle($style, RadioItem::class); |
||
596 | |||
597 | return $this; |
||
598 | } |
||
599 | |||
600 | public function modifyRadioStyle(callable $itemCallable) : self |
||
605 | } |
||
606 | } |
||
607 |
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.