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 MenuStyle 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 MenuStyle, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 16 | class MenuStyle |
||
| 17 | { |
||
| 18 | /** |
||
| 19 | * @var Terminal |
||
| 20 | */ |
||
| 21 | protected $terminal; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * @var string |
||
| 25 | */ |
||
| 26 | protected $fg; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * @var string |
||
| 30 | */ |
||
| 31 | protected $bg; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @var int |
||
| 35 | */ |
||
| 36 | protected $width; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @var int |
||
| 40 | */ |
||
| 41 | protected $margin; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @var int |
||
| 45 | */ |
||
| 46 | protected $paddingTopBottom; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @var int |
||
| 50 | */ |
||
| 51 | protected $paddingLeftRight; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @var array |
||
| 55 | */ |
||
| 56 | private $paddingTopBottomRows = []; |
||
| 57 | |||
| 58 | /** |
||
| 59 | * @var int |
||
| 60 | */ |
||
| 61 | protected $contentWidth; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * @var string |
||
| 65 | */ |
||
| 66 | private $selectedMarker; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * @var string |
||
| 70 | */ |
||
| 71 | private $unselectedMarker; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * @var string |
||
| 75 | */ |
||
| 76 | private $itemExtra; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * @var bool |
||
| 80 | */ |
||
| 81 | private $displaysExtra; |
||
| 82 | |||
| 83 | /** |
||
| 84 | * @var string |
||
| 85 | */ |
||
| 86 | private $titleSeparator; |
||
| 87 | |||
| 88 | /** |
||
| 89 | * @var string |
||
| 90 | */ |
||
| 91 | private $coloursSetCode; |
||
| 92 | |||
| 93 | /** |
||
| 94 | * @var string |
||
| 95 | */ |
||
| 96 | private $invertedColoursSetCode = "\033[7m"; |
||
| 97 | |||
| 98 | /** |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $invertedColoursUnsetCode = "\033[27m"; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | private $coloursResetCode = "\033[0m"; |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @var int |
||
| 110 | */ |
||
| 111 | private $borderTopWidth; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * @var int |
||
| 115 | */ |
||
| 116 | private $borderRightWidth; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * @var int |
||
| 120 | */ |
||
| 121 | private $borderBottomWidth; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * @var int |
||
| 125 | */ |
||
| 126 | private $borderLeftWidth; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @var string |
||
| 130 | */ |
||
| 131 | private $borderColour = 'white'; |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | private $borderTopRows = []; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var array |
||
| 140 | */ |
||
| 141 | private $borderBottomRows = []; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * @var bool |
||
| 145 | */ |
||
| 146 | private $marginAuto = false; |
||
| 147 | |||
| 148 | /** |
||
| 149 | * Default Values |
||
| 150 | * |
||
| 151 | * @var array |
||
| 152 | */ |
||
| 153 | private static $defaultStyleValues = [ |
||
| 154 | 'fg' => 'white', |
||
| 155 | 'bg' => 'blue', |
||
| 156 | 'width' => 100, |
||
| 157 | 'paddingTopBottom' => 1, |
||
| 158 | 'paddingLeftRight' => 2, |
||
| 159 | 'margin' => 2, |
||
| 160 | 'selectedMarker' => '●', |
||
| 161 | 'unselectedMarker' => '○', |
||
| 162 | 'itemExtra' => '✔', |
||
| 163 | 'displaysExtra' => false, |
||
| 164 | 'titleSeparator' => '=', |
||
| 165 | 'borderTopWidth' => 0, |
||
| 166 | 'borderRightWidth' => 0, |
||
| 167 | 'borderBottomWidth' => 0, |
||
| 168 | 'borderLeftWidth' => 0, |
||
| 169 | 'borderColour' => 'white', |
||
| 170 | 'marginAuto' => false, |
||
| 171 | ]; |
||
| 172 | |||
| 173 | public static function getDefaultStyleValues() : array |
||
| 177 | |||
| 178 | /** |
||
| 179 | * @var array |
||
| 180 | */ |
||
| 181 | private static $availableForegroundColors = array( |
||
| 182 | 'black' => 30, |
||
| 183 | 'red' => 31, |
||
| 184 | 'green' => 32, |
||
| 185 | 'yellow' => 33, |
||
| 186 | 'blue' => 34, |
||
| 187 | 'magenta' => 35, |
||
| 188 | 'cyan' => 36, |
||
| 189 | 'white' => 37, |
||
| 190 | 'default' => 39, |
||
| 191 | ); |
||
| 192 | |||
| 193 | /** |
||
| 194 | * @var array |
||
| 195 | */ |
||
| 196 | private static $availableBackgroundColors = array( |
||
| 197 | 'black' => 40, |
||
| 198 | 'red' => 41, |
||
| 199 | 'green' => 42, |
||
| 200 | 'yellow' => 43, |
||
| 201 | 'blue' => 44, |
||
| 202 | 'magenta' => 45, |
||
| 203 | 'cyan' => 46, |
||
| 204 | 'white' => 47, |
||
| 205 | 'default' => 49, |
||
| 206 | ); |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @var array |
||
| 210 | */ |
||
| 211 | private static $availableOptions = array( |
||
| 212 | 'bold' => array('set' => 1, 'unset' => 22), |
||
| 213 | 'dim' => array('set' => 2, 'unset' => 22), |
||
| 214 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
| 215 | 'blink' => array('set' => 5, 'unset' => 25), |
||
| 216 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
| 217 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
| 218 | ); |
||
| 219 | |||
| 220 | /** |
||
| 221 | * Initialise style |
||
| 222 | */ |
||
| 223 | public function __construct(Terminal $terminal = null) |
||
| 247 | |||
| 248 | public function getDisabledItemText(string $text) : string |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Generates the ansi escape sequence to set the colours |
||
| 260 | */ |
||
| 261 | private function generateColoursSetCode() : void |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Get the colour code for Bg and Fg |
||
| 280 | */ |
||
| 281 | public function getColoursSetCode() : string |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Get the inverted escape sequence (used for selected elements) |
||
| 288 | */ |
||
| 289 | public function getInvertedColoursSetCode() : string |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Get the inverted escape sequence (used for selected elements) |
||
| 296 | */ |
||
| 297 | public function getInvertedColoursUnsetCode() : string |
||
| 301 | |||
| 302 | /** |
||
| 303 | * Get the escape sequence used to reset colours to default |
||
| 304 | */ |
||
| 305 | public function getColoursResetCode() : string |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Calculate the contents width |
||
| 312 | */ |
||
| 313 | protected function calculateContentWidth() : void |
||
| 319 | |||
| 320 | public function getFg() |
||
| 324 | |||
| 325 | View Code Duplication | public function setFg(string $fg, string $fallback = null) : self |
|
| 336 | |||
| 337 | public function getBg() |
||
| 341 | |||
| 342 | public function setBg(string $bg, string $fallback = null) : self |
||
| 355 | |||
| 356 | public function getWidth() : int |
||
| 360 | |||
| 361 | public function setWidth(int $width) : self |
||
| 380 | |||
| 381 | public function getPaddingTopBottom() : int |
||
| 385 | |||
| 386 | public function getPaddingLeftRight() : int |
||
| 390 | |||
| 391 | private function generatePaddingTopBottomRows() : void |
||
| 415 | |||
| 416 | public function getPaddingTopBottomRows() : array |
||
| 420 | |||
| 421 | View Code Duplication | public function setPadding(int $topBottom, int $leftRight = null) : self |
|
| 435 | |||
| 436 | public function setPaddingTopBottom(int $topBottom) : self |
||
| 445 | |||
| 446 | public function setPaddingLeftRight(int $leftRight) : self |
||
| 456 | |||
| 457 | public function getMargin() : int |
||
| 461 | |||
| 462 | public function setMarginAuto() : self |
||
| 472 | |||
| 473 | public function setMargin(int $margin) : self |
||
| 485 | |||
| 486 | public function getContentWidth() : int |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get padding for right had side of content |
||
| 493 | */ |
||
| 494 | public function getRightHandPadding(int $contentLength) : int |
||
| 504 | |||
| 505 | public function getSelectedMarker() : string |
||
| 509 | |||
| 510 | public function setSelectedMarker(string $marker) : self |
||
| 516 | |||
| 517 | public function getUnselectedMarker() : string |
||
| 521 | |||
| 522 | public function setUnselectedMarker(string $marker) : self |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Get the correct marker for the item |
||
| 531 | */ |
||
| 532 | public function getMarker(bool $selected) : string |
||
| 536 | |||
| 537 | public function setItemExtra(string $itemExtra) : self |
||
| 543 | |||
| 544 | public function getItemExtra() : string |
||
| 548 | |||
| 549 | public function getDisplaysExtra() : bool |
||
| 553 | |||
| 554 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
| 560 | |||
| 561 | public function getTitleSeparator() : string |
||
| 565 | |||
| 566 | public function setTitleSeparator(string $actionSeparator) : self |
||
| 572 | |||
| 573 | private function generateBorderRows() : void |
||
| 586 | |||
| 587 | public function getBorderTopRows() : array |
||
| 591 | |||
| 592 | public function getBorderBottomRows() : array |
||
| 596 | |||
| 597 | /** |
||
| 598 | * Shorthand function to set all borders values at once |
||
| 599 | */ |
||
| 600 | public function setBorder( |
||
| 636 | |||
| 637 | public function setBorderTopWidth(int $width) : self |
||
| 645 | |||
| 646 | public function setBorderRightWidth(int $width) : self |
||
| 655 | |||
| 656 | public function setBorderBottomWidth(int $width) : self |
||
| 664 | |||
| 665 | public function setBorderLeftWidth(int $width) : self |
||
| 674 | |||
| 675 | public function setBorderColour(string $colour, $fallback = null) : self |
||
| 688 | |||
| 689 | public function getBorderTopWidth() : int |
||
| 693 | |||
| 694 | public function getBorderRightWidth() : int |
||
| 698 | |||
| 699 | public function getBorderBottomWidth() : int |
||
| 703 | |||
| 704 | public function getBorderLeftWidth() : int |
||
| 708 | |||
| 709 | public function getBorderColour() : string |
||
| 713 | |||
| 714 | public function getBorderColourCode() : string |
||
| 724 | } |
||
| 725 |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClassto useselfinstead: