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 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. 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 CliMenuBuilder, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 23 | class CliMenuBuilder |
||
| 24 | { |
||
| 25 | /** |
||
| 26 | * @var bool |
||
| 27 | */ |
||
| 28 | private $isBuilt = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * @var null|self |
||
| 32 | */ |
||
| 33 | private $parent; |
||
| 34 | |||
| 35 | /** |
||
| 36 | * @var self[] |
||
| 37 | */ |
||
| 38 | private $subMenuBuilders = []; |
||
| 39 | |||
| 40 | /** |
||
| 41 | * @var CliMenu[] |
||
| 42 | */ |
||
| 43 | private $subMenus = []; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @var string |
||
| 47 | */ |
||
| 48 | private $goBackButtonText = 'Go Back'; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $exitButtonText = 'Exit'; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * @var array |
||
| 57 | */ |
||
| 58 | private $menuItems = []; |
||
| 59 | |||
| 60 | /** |
||
| 61 | * @var array |
||
| 62 | */ |
||
| 63 | private $style; |
||
| 64 | |||
| 65 | /** |
||
| 66 | * @var Terminal |
||
| 67 | */ |
||
| 68 | private $terminal; |
||
| 69 | |||
| 70 | /** |
||
| 71 | * @var string |
||
| 72 | */ |
||
| 73 | private $menuTitle = null; |
||
| 74 | |||
| 75 | /** |
||
| 76 | * @var bool |
||
| 77 | */ |
||
| 78 | private $disableDefaultItems = false; |
||
| 79 | |||
| 80 | /** |
||
| 81 | * @var bool |
||
| 82 | */ |
||
| 83 | private $disabled = false; |
||
| 84 | |||
| 85 | public function __construct(CliMenuBuilder $parent = null) |
||
| 93 | |||
| 94 | public function setTitle(string $title) : self |
||
| 100 | |||
| 101 | public function addMenuItem(MenuItemInterface $item) : self |
||
| 107 | |||
| 108 | public function addItem( |
||
| 118 | |||
| 119 | public function addItems(array $items) : self |
||
| 127 | |||
| 128 | public function addStaticItem(string $text) : self |
||
| 134 | |||
| 135 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
| 141 | |||
| 142 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
| 148 | |||
| 149 | /** |
||
| 150 | * Add a submenu with a string identifier |
||
| 151 | */ |
||
| 152 | public function addSubMenu(string $id, CliMenuBuilder $subMenuBuilder = null) : CliMenuBuilder |
||
| 164 | |||
| 165 | /** |
||
| 166 | * Add a split item |
||
| 167 | */ |
||
| 168 | public function addSplitItem() : SplitItem |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Disable a submenu |
||
| 178 | * |
||
| 179 | * @throws \InvalidArgumentException |
||
| 180 | */ |
||
| 181 | public function disableMenu() : self |
||
| 193 | |||
| 194 | public function isMenuDisabled() : bool |
||
| 198 | |||
| 199 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
| 205 | |||
| 206 | public function setExitButtonText(string $exitButtonText) : self |
||
| 212 | |||
| 213 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
| 223 | |||
| 224 | View Code Duplication | public function setForegroundColour(string $colour, string $fallback = null) : self |
|
| 234 | |||
| 235 | public function setWidth(int $width) : self |
||
| 241 | |||
| 242 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
| 253 | |||
| 254 | public function setPaddingTopBottom(int $topBottom) : self |
||
| 260 | |||
| 261 | public function setPaddingLeftRight(int $leftRight) : self |
||
| 267 | |||
| 268 | public function setMarginAuto() : self |
||
| 274 | |||
| 275 | public function setMargin(int $margin) : self |
||
| 282 | |||
| 283 | public function setUnselectedMarker(string $marker) : self |
||
| 289 | |||
| 290 | public function setSelectedMarker(string $marker) : self |
||
| 296 | |||
| 297 | public function setItemExtra(string $extra) : self |
||
| 303 | |||
| 304 | public function setTitleSeparator(string $separator) : self |
||
| 310 | |||
| 311 | public function setBorder( |
||
| 343 | |||
| 344 | public function setTerminal(Terminal $terminal) : self |
||
| 349 | |||
| 350 | public function getTerminal() : Terminal |
||
| 354 | |||
| 355 | private function getDefaultItems() : array |
||
| 365 | |||
| 366 | public function disableDefaultItems() : self |
||
| 372 | |||
| 373 | private function itemsHaveExtra(array $items) : bool |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Recursively drop back to the parents menu style |
||
| 382 | * when the current menu has a parent and has no changes |
||
| 383 | */ |
||
| 384 | private function getMenuStyle() : MenuStyle |
||
| 396 | |||
| 397 | private function buildStyle() : MenuStyle |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Return to parent builder |
||
| 423 | * |
||
| 424 | * @throws RuntimeException |
||
| 425 | */ |
||
| 426 | public function end() : CliMenuBuilder |
||
| 434 | |||
| 435 | /** |
||
| 436 | * @throws RuntimeException |
||
| 437 | */ |
||
| 438 | public function getSubMenu(string $id) : CliMenu |
||
| 446 | |||
| 447 | private function buildSubMenus(array $items) : array |
||
| 460 | |||
| 461 | public function build() : CliMenu |
||
| 486 | } |
||
| 487 |
This check looks for function calls that miss required arguments.