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:
| 1 | <?php |
||
| 14 | class MenuStyle |
||
| 15 | { |
||
| 16 | /** |
||
| 17 | * @var Terminal |
||
| 18 | */ |
||
| 19 | protected $terminal; |
||
| 20 | |||
| 21 | /** |
||
| 22 | * @var string |
||
| 23 | */ |
||
| 24 | protected $fg; |
||
| 25 | |||
| 26 | /** |
||
| 27 | * @var string |
||
| 28 | */ |
||
| 29 | protected $bg; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int |
||
| 33 | */ |
||
| 34 | protected $width; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int |
||
| 38 | */ |
||
| 39 | protected $padding; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int |
||
| 43 | */ |
||
| 44 | protected $margin; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var int |
||
| 48 | */ |
||
| 49 | protected $contentWidth; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | private $selectedMarker; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | private $unselectedMarker; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | private $itemExtra; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var bool |
||
| 68 | */ |
||
| 69 | private $displaysExtra; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | private $titleSeparator; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var bool |
||
| 78 | */ |
||
| 79 | private $marginAuto = false; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * Default Values |
||
| 83 | * |
||
| 84 | * @var array |
||
| 85 | */ |
||
| 86 | private static $defaultStyleValues = [ |
||
| 87 | 'fg' => 'white', |
||
| 88 | 'bg' => 'blue', |
||
| 89 | 'width' => 100, |
||
| 90 | 'padding' => 2, |
||
| 91 | 'margin' => 2, |
||
| 92 | 'selectedMarker' => '●', |
||
| 93 | 'unselectedMarker' => '○', |
||
| 94 | 'itemExtra' => '✔', |
||
| 95 | 'displaysExtra' => false, |
||
| 96 | 'titleSeparator' => '=', |
||
| 97 | 'marginAuto' => false, |
||
| 98 | ]; |
||
| 99 | |||
| 100 | public static function getDefaultStyleValues() : array |
||
| 104 | |||
| 105 | /** |
||
| 106 | * @var array |
||
| 107 | */ |
||
| 108 | private static $availableForegroundColors = array( |
||
| 109 | 'black' => array('set' => 30, 'unset' => 39), |
||
| 110 | 'red' => array('set' => 31, 'unset' => 39), |
||
| 111 | 'green' => array('set' => 32, 'unset' => 39), |
||
| 112 | 'yellow' => array('set' => 33, 'unset' => 39), |
||
| 113 | 'blue' => array('set' => 34, 'unset' => 39), |
||
| 114 | 'magenta' => array('set' => 35, 'unset' => 39), |
||
| 115 | 'cyan' => array('set' => 36, 'unset' => 39), |
||
| 116 | 'white' => array('set' => 37, 'unset' => 39), |
||
| 117 | 'default' => array('set' => 39, 'unset' => 39), |
||
| 118 | ); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * @var array |
||
| 122 | */ |
||
| 123 | private static $availableBackgroundColors = array( |
||
| 124 | 'black' => array('set' => 40, 'unset' => 49), |
||
| 125 | 'red' => array('set' => 41, 'unset' => 49), |
||
| 126 | 'green' => array('set' => 42, 'unset' => 49), |
||
| 127 | 'yellow' => array('set' => 43, 'unset' => 49), |
||
| 128 | 'blue' => array('set' => 44, 'unset' => 49), |
||
| 129 | 'magenta' => array('set' => 45, 'unset' => 49), |
||
| 130 | 'cyan' => array('set' => 46, 'unset' => 49), |
||
| 131 | 'white' => array('set' => 47, 'unset' => 49), |
||
| 132 | 'default' => array('set' => 49, 'unset' => 49), |
||
| 133 | ); |
||
| 134 | |||
| 135 | /** |
||
| 136 | * @var array |
||
| 137 | */ |
||
| 138 | private static $availableOptions = array( |
||
| 139 | 'bold' => array('set' => 1, 'unset' => 22), |
||
| 140 | 'dim' => array('set' => 2, 'unset' => 22), |
||
| 141 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
| 142 | 'blink' => array('set' => 5, 'unset' => 25), |
||
| 143 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
| 144 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
| 145 | ); |
||
| 146 | |||
| 147 | /** |
||
| 148 | * Initialise style |
||
| 149 | */ |
||
| 150 | public function __construct(Terminal $terminal = null) |
||
| 165 | |||
| 166 | public static function getAvailableColours() : array |
||
| 170 | |||
| 171 | public function getDisabledItemText(string $text) : string |
||
| 180 | |||
| 181 | /** |
||
| 182 | * Get the colour code set for Bg and Fg |
||
| 183 | */ |
||
| 184 | View Code Duplication | public function getSelectedSetCode() : string |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Get the colour unset code for Bg and Fg |
||
| 197 | */ |
||
| 198 | View Code Duplication | public function getSelectedUnsetCode() : string |
|
| 208 | |||
| 209 | /** |
||
| 210 | * Get the inverted colour code |
||
| 211 | */ |
||
| 212 | View Code Duplication | public function getUnselectedSetCode() : string |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Get the inverted colour unset code |
||
| 225 | */ |
||
| 226 | View Code Duplication | public function getUnselectedUnsetCode() : string |
|
| 236 | |||
| 237 | /** |
||
| 238 | * Calculate the contents width |
||
| 239 | */ |
||
| 240 | protected function calculateContentWidth() : void |
||
| 244 | |||
| 245 | public function getFg() : string |
||
| 249 | |||
| 250 | public function setFg(string $fg) : self |
||
| 256 | |||
| 257 | public function getBg() : string |
||
| 261 | |||
| 262 | public function setBg(string $bg) : self |
||
| 268 | |||
| 269 | public function getWidth() : int |
||
| 273 | |||
| 274 | public function setWidth(int $width) : self |
||
| 288 | |||
| 289 | public function getPadding() : int |
||
| 293 | |||
| 294 | public function setPadding(int $padding) : self |
||
| 302 | |||
| 303 | public function getMargin() : int |
||
| 307 | |||
| 308 | public function setMarginAuto() : self |
||
| 315 | |||
| 316 | public function setMargin(int $margin) : self |
||
| 323 | |||
| 324 | public function getContentWidth() : int |
||
| 328 | |||
| 329 | /** |
||
| 330 | * Get padding for right had side of content |
||
| 331 | */ |
||
| 332 | public function getRightHandPadding(int $contentLength) : int |
||
| 336 | |||
| 337 | public function getSelectedMarker() : string |
||
| 341 | |||
| 342 | public function setSelectedMarker(string $marker) : self |
||
| 348 | |||
| 349 | public function getUnselectedMarker() : string |
||
| 353 | |||
| 354 | public function setUnselectedMarker(string $marker) : self |
||
| 360 | |||
| 361 | /** |
||
| 362 | * Get the correct marker for the item |
||
| 363 | */ |
||
| 364 | public function getMarker(bool $selected) : string |
||
| 368 | |||
| 369 | public function setItemExtra(string $itemExtra) : self |
||
| 375 | |||
| 376 | public function getItemExtra() : string |
||
| 380 | |||
| 381 | public function getDisplaysExtra() : bool |
||
| 385 | |||
| 386 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
| 392 | |||
| 393 | public function getTitleSeparator() : string |
||
| 397 | |||
| 398 | public function setTitleSeparator(string $actionSeparator) : self |
||
| 404 | } |
||
| 405 |
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: