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 implements Builder |
||
| 24 | { |
||
| 25 | use BuilderUtils; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var bool |
||
| 29 | */ |
||
| 30 | private $isBuilt = false; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var SplitItemBuilder[] |
||
| 34 | */ |
||
| 35 | private $splitItemBuilders = []; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var SplitItem[] |
||
| 39 | */ |
||
| 40 | private $splitItems = []; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var string |
||
| 44 | */ |
||
| 45 | private $goBackButtonText = 'Go Back'; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var string |
||
| 49 | */ |
||
| 50 | private $exitButtonText = 'Exit'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var array |
||
| 54 | */ |
||
| 55 | private $style; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var Terminal |
||
| 59 | */ |
||
| 60 | private $terminal; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $menuTitle; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | private $disableDefaultItems = false; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var bool |
||
| 74 | */ |
||
| 75 | private $disabled = false; |
||
| 76 | |||
| 77 | public function __construct(Builder $parent = null) |
||
| 78 | { |
||
| 79 | $this->parent = $parent; |
||
| 80 | $this->terminal = $this->parent !== null |
||
| 81 | ? $this->parent->getTerminal() |
||
| 82 | : TerminalFactory::fromSystem(); |
||
| 83 | $this->style = MenuStyle::getDefaultStyleValues(); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function setTitle(string $title) : self |
||
| 92 | |||
| 93 | public function addMenuItem(MenuItemInterface $item) : self |
||
| 99 | |||
| 100 | public function addItems(array $items) : self |
||
| 108 | |||
| 109 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
| 115 | |||
| 116 | /** |
||
| 117 | * Injects a submenu directly (without going through the builder |
||
| 118 | */ |
||
| 119 | public function injectSubMenu(string $id, CliMenu $subMenu) : CliMenuBuilder |
||
| 125 | |||
| 126 | /** |
||
| 127 | * Add a split item |
||
| 128 | */ |
||
| 129 | public function addSplitItem() : SplitItemBuilder |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Disable a submenu |
||
| 139 | * |
||
| 140 | * @throws \InvalidArgumentException |
||
| 141 | */ |
||
| 142 | public function disableMenu() : self |
||
| 154 | |||
| 155 | public function isMenuDisabled() : bool |
||
| 159 | |||
| 160 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
| 166 | |||
| 167 | public function setExitButtonText(string $exitButtonText) : self |
||
| 173 | |||
| 174 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
| 184 | |||
| 185 | View Code Duplication | public function setForegroundColour(string $colour, string $fallback = null) : self |
|
| 195 | |||
| 196 | public function setWidth(int $width) : self |
||
| 202 | |||
| 203 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
| 214 | |||
| 215 | public function setPaddingTopBottom(int $topBottom) : self |
||
| 221 | |||
| 222 | public function setPaddingLeftRight(int $leftRight) : self |
||
| 228 | |||
| 229 | public function setMarginAuto() : self |
||
| 235 | |||
| 236 | public function setMargin(int $margin) : self |
||
| 243 | |||
| 244 | public function setUnselectedMarker(string $marker) : self |
||
| 250 | |||
| 251 | public function setSelectedMarker(string $marker) : self |
||
| 257 | |||
| 258 | public function setItemExtra(string $extra) : self |
||
| 264 | |||
| 265 | public function setTitleSeparator(string $separator) : self |
||
| 271 | |||
| 272 | public function setBorder( |
||
| 304 | |||
| 305 | public function setBorderTopWidth(int $width) : self |
||
| 311 | |||
| 312 | public function setBorderRightWidth(int $width) : self |
||
| 318 | |||
| 319 | public function setBorderBottomWidth(int $width) : self |
||
| 325 | |||
| 326 | public function setBorderLeftWidth(int $width) : self |
||
| 332 | |||
| 333 | public function setBorderColour(string $colour, $fallback = null) : self |
||
| 340 | |||
| 341 | public function setTerminal(Terminal $terminal) : self |
||
| 346 | |||
| 347 | public function getTerminal() : Terminal |
||
| 351 | |||
| 352 | private function getDefaultItems() : array |
||
| 362 | |||
| 363 | public function disableDefaultItems() : self |
||
| 369 | |||
| 370 | private function itemsHaveExtra(array $items) : bool |
||
| 376 | |||
| 377 | /** |
||
| 378 | * Recursively drop back to the parents menu style |
||
| 379 | * when the current menu has a parent and has no changes |
||
| 380 | */ |
||
| 381 | public function getMenuStyle() : MenuStyle |
||
| 393 | |||
| 394 | private function buildStyle() : MenuStyle |
||
| 417 | |||
| 418 | /** |
||
| 419 | * @throws RuntimeException |
||
| 420 | */ |
||
| 421 | public function getSubMenu(string $id) : CliMenu |
||
| 429 | |||
| 430 | private function buildSplitItems(array $items) : array |
||
| 443 | |||
| 444 | public function build() : CliMenu |
||
| 475 | } |
||
| 476 |
This check looks for function calls that miss required arguments.