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 |
||
| 22 | class CliMenuBuilder |
||
| 23 | { |
||
| 24 | /** |
||
| 25 | * @var bool |
||
| 26 | */ |
||
| 27 | private $isBuilt = false; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var null|self |
||
| 31 | */ |
||
| 32 | private $parent; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var self[] |
||
| 36 | */ |
||
| 37 | private $subMenuBuilders = []; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var CliMenu[] |
||
| 41 | */ |
||
| 42 | private $subMenus = []; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var string |
||
| 46 | */ |
||
| 47 | private $goBackButtonText = 'Go Back'; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var string |
||
| 51 | */ |
||
| 52 | private $exitButtonText = 'Exit'; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var array |
||
| 56 | */ |
||
| 57 | private $menuItems = []; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var array |
||
| 61 | */ |
||
| 62 | private $style; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Terminal |
||
| 66 | */ |
||
| 67 | private $terminal; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var string |
||
| 71 | */ |
||
| 72 | private $menuTitle = null; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var bool |
||
| 76 | */ |
||
| 77 | private $disableDefaultItems = false; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @var bool |
||
| 81 | */ |
||
| 82 | private $disabled = false; |
||
| 83 | |||
| 84 | public function __construct(CliMenuBuilder $parent = null) |
||
| 92 | |||
| 93 | public function setTitle(string $title) : self |
||
| 99 | |||
| 100 | public function addMenuItem(MenuItemInterface $item) : self |
||
| 106 | |||
| 107 | public function addItem( |
||
| 117 | |||
| 118 | public function addItems(array $items) : self |
||
| 126 | |||
| 127 | public function addStaticItem(string $text) : self |
||
| 133 | |||
| 134 | public function addLineBreak(string $breakChar = ' ', int $lines = 1) : self |
||
| 140 | |||
| 141 | public function addAsciiArt(string $art, string $position = AsciiArtItem::POSITION_CENTER, string $alt = '') : self |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Add a submenu with a string identifier |
||
| 150 | */ |
||
| 151 | public function addSubMenu(string $id, CliMenuBuilder $subMenuBuilder = null) : CliMenuBuilder |
||
| 163 | |||
| 164 | /** |
||
| 165 | * Disable a submenu |
||
| 166 | * |
||
| 167 | * @throws \InvalidArgumentException |
||
| 168 | */ |
||
| 169 | public function disableMenu() : self |
||
| 181 | |||
| 182 | public function isMenuDisabled() : bool |
||
| 186 | |||
| 187 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
| 193 | |||
| 194 | public function setExitButtonText(string $exitButtonText) : self |
||
| 200 | |||
| 201 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
| 211 | |||
| 212 | View Code Duplication | public function setForegroundColour(string $colour, string $fallback = null) : self |
|
| 222 | |||
| 223 | public function setWidth(int $width) : self |
||
| 229 | |||
| 230 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
| 241 | |||
| 242 | public function setPaddingTopBottom(int $topBottom) : self |
||
| 248 | |||
| 249 | public function setPaddingLeftRight(int $leftRight) : self |
||
| 255 | |||
| 256 | public function setMarginAuto() : self |
||
| 262 | |||
| 263 | public function setMargin(int $margin) : self |
||
| 270 | |||
| 271 | public function setUnselectedMarker(string $marker) : self |
||
| 277 | |||
| 278 | public function setSelectedMarker(string $marker) : self |
||
| 284 | |||
| 285 | public function setItemExtra(string $extra) : self |
||
| 291 | |||
| 292 | public function setTitleSeparator(string $separator) : self |
||
| 298 | |||
| 299 | public function setBorder( |
||
| 331 | |||
| 332 | public function setTerminal(Terminal $terminal) : self |
||
| 337 | |||
| 338 | public function getTerminal() : Terminal |
||
| 342 | |||
| 343 | private function getDefaultItems() : array |
||
| 353 | |||
| 354 | public function disableDefaultItems() : self |
||
| 360 | |||
| 361 | private function itemsHaveExtra(array $items) : bool |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Recursively drop back to the parents menu style |
||
| 370 | * when the current menu has a parent and has no changes |
||
| 371 | */ |
||
| 372 | private function getMenuStyle() : MenuStyle |
||
| 384 | |||
| 385 | private function buildStyle() : MenuStyle |
||
| 408 | |||
| 409 | /** |
||
| 410 | * Return to parent builder |
||
| 411 | * |
||
| 412 | * @throws RuntimeException |
||
| 413 | */ |
||
| 414 | public function end() : CliMenuBuilder |
||
| 422 | |||
| 423 | /** |
||
| 424 | * @throws RuntimeException |
||
| 425 | */ |
||
| 426 | public function getSubMenu(string $id) : CliMenu |
||
| 434 | |||
| 435 | private function buildSubMenus(array $items) : array |
||
| 448 | |||
| 449 | public function build() : CliMenu |
||
| 474 | } |
||
| 475 |
This check looks for function calls that miss required arguments.