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 | * Disable a submenu |
||
| 167 | * |
||
| 168 | * @throws \InvalidArgumentException |
||
| 169 | */ |
||
| 170 | public function disableMenu() : self |
||
| 182 | |||
| 183 | public function isMenuDisabled() : bool |
||
| 187 | |||
| 188 | public function setGoBackButtonText(string $goBackButtonTest) : self |
||
| 194 | |||
| 195 | public function setExitButtonText(string $exitButtonText) : self |
||
| 201 | |||
| 202 | public function setBackgroundColour(string $colour, string $fallback = null) : self |
||
| 203 | { |
||
| 204 | $this->style['bg'] = ColourUtil::validateColour( |
||
| 205 | $this->terminal, |
||
| 206 | $colour, |
||
| 207 | $fallback |
||
| 208 | ); |
||
| 209 | |||
| 210 | return $this; |
||
| 211 | } |
||
| 212 | |||
| 213 | View Code Duplication | public function setForegroundColour(string $colour, string $fallback = null) : self |
|
| 223 | |||
| 224 | public function setWidth(int $width) : self |
||
| 230 | |||
| 231 | public function setPadding(int $padding) : self |
||
| 237 | |||
| 238 | public function setMarginAuto() : self |
||
| 244 | |||
| 245 | public function setMargin(int $margin) : self |
||
| 254 | |||
| 255 | public function setUnselectedMarker(string $marker) : self |
||
| 261 | |||
| 262 | public function setSelectedMarker(string $marker) : self |
||
| 268 | |||
| 269 | public function setItemExtra(string $extra) : self |
||
| 275 | |||
| 276 | public function setTitleSeparator(string $separator) : self |
||
| 282 | |||
| 283 | public function setBorder( |
||
| 313 | |||
| 314 | public function setTerminal(Terminal $terminal) : self |
||
| 319 | |||
| 320 | public function getTerminal() : Terminal |
||
| 324 | |||
| 325 | private function getDefaultItems() : array |
||
| 335 | |||
| 336 | public function disableDefaultItems() : self |
||
| 342 | |||
| 343 | private function itemsHaveExtra(array $items) : bool |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Recursively drop back to the parents menu style |
||
| 352 | * when the current menu has a parent and has no changes |
||
| 353 | */ |
||
| 354 | private function getMenuStyle() : MenuStyle |
||
| 366 | |||
| 367 | private function buildStyle() : MenuStyle |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Return to parent builder |
||
| 392 | * |
||
| 393 | * @throws RuntimeException |
||
| 394 | */ |
||
| 395 | public function end() : CliMenuBuilder |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @throws RuntimeException |
||
| 406 | */ |
||
| 407 | public function getSubMenu(string $id) : CliMenu |
||
| 415 | |||
| 416 | private function buildSubMenus(array $items) : array |
||
| 429 | |||
| 430 | public function build() : CliMenu |
||
| 455 | } |
||
| 456 |
This check looks for function calls that miss required arguments.