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 |
||
| 17 | class MenuStyle |
||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var TerminalInterface |
||
| 21 | */ |
||
| 22 | protected $terminal; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var string |
||
| 26 | */ |
||
| 27 | protected $fg; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | protected $bg; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * @var int |
||
| 36 | */ |
||
| 37 | protected $width; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @var int |
||
| 41 | */ |
||
| 42 | protected $padding; |
||
| 43 | |||
| 44 | /** |
||
| 45 | * @var int |
||
| 46 | */ |
||
| 47 | protected $margin; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * @var int |
||
| 51 | */ |
||
| 52 | protected $contentWidth; |
||
| 53 | |||
| 54 | /** |
||
| 55 | * @var string |
||
| 56 | */ |
||
| 57 | private $selectedMarker; |
||
| 58 | |||
| 59 | /** |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $unselectedMarker; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var string |
||
| 66 | */ |
||
| 67 | private $itemExtra; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool |
||
| 71 | */ |
||
| 72 | private $displaysExtra; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @var string |
||
| 76 | */ |
||
| 77 | private $titleSeparator; |
||
| 78 | |||
| 79 | /** |
||
| 80 | * Default Values |
||
| 81 | * |
||
| 82 | * @var array |
||
| 83 | */ |
||
| 84 | private static $defaultStyleValues = [ |
||
| 85 | 'fg' => 'white', |
||
| 86 | 'bg' => 'blue', |
||
| 87 | 'width' => 100, |
||
| 88 | 'padding' => 2, |
||
| 89 | 'margin' => 2, |
||
| 90 | 'selectedMarker' => '●', |
||
| 91 | 'unselectedMarker' => '○', |
||
| 92 | 'itemExtra' => '✔', |
||
| 93 | 'displaysExtra' => false, |
||
| 94 | 'titleSeparator' => '=', |
||
| 95 | ]; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @return array |
||
| 99 | */ |
||
| 100 | public static function getDefaultStyleValues() |
||
| 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 | * @param TerminalInterface $terminal |
||
| 151 | */ |
||
| 152 | public function __construct(TerminalInterface $terminal = null) |
||
| 167 | |||
| 168 | /** |
||
| 169 | * @return array |
||
| 170 | */ |
||
| 171 | public static function getAvailableColours() |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @param string $text |
||
| 178 | * @return string |
||
| 179 | */ |
||
| 180 | public function getDisabledItemText($text) |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Get the colour code set for Bg and Fg |
||
| 192 | * |
||
| 193 | * @return string |
||
| 194 | */ |
||
| 195 | View Code Duplication | public function getSelectedSetCode() |
|
| 205 | |||
| 206 | /** |
||
| 207 | * Get the colour unset code for Bg and Fg |
||
| 208 | * |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | View Code Duplication | public function getSelectedUnsetCode() |
|
| 221 | |||
| 222 | /** |
||
| 223 | * Get the inverted colour code |
||
| 224 | * |
||
| 225 | * @return string |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function getUnselectedSetCode() |
|
| 237 | |||
| 238 | /** |
||
| 239 | * Get the inverted colour unset code |
||
| 240 | * |
||
| 241 | * @return string |
||
| 242 | */ |
||
| 243 | View Code Duplication | public function getUnselectedUnsetCode() |
|
| 253 | |||
| 254 | /** |
||
| 255 | * Calculate the contents width |
||
| 256 | */ |
||
| 257 | protected function calculateContentWidth() |
||
| 261 | |||
| 262 | /** |
||
| 263 | * @return string |
||
| 264 | */ |
||
| 265 | public function getFg() |
||
| 269 | |||
| 270 | /** |
||
| 271 | * @param string $fg |
||
| 272 | * @return MenuStyle |
||
| 273 | */ |
||
| 274 | public function setFg($fg) |
||
| 280 | |||
| 281 | /** |
||
| 282 | * @return string |
||
| 283 | */ |
||
| 284 | public function getBg() |
||
| 288 | |||
| 289 | /** |
||
| 290 | * @param string $bg |
||
| 291 | * @return MenuStyle |
||
| 292 | */ |
||
| 293 | public function setBg($bg) |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return int |
||
| 302 | */ |
||
| 303 | public function getWidth() |
||
| 307 | |||
| 308 | /** |
||
| 309 | * @param int $width |
||
| 310 | * @return MenuStyle |
||
| 311 | */ |
||
| 312 | public function setWidth($width) |
||
| 325 | |||
| 326 | /** |
||
| 327 | * @return int |
||
| 328 | */ |
||
| 329 | public function getPadding() |
||
| 333 | |||
| 334 | /** |
||
| 335 | * @param int $padding |
||
| 336 | * @return MenuStyle |
||
| 337 | */ |
||
| 338 | public function setPadding($padding) |
||
| 346 | |||
| 347 | /** |
||
| 348 | * @return int |
||
| 349 | */ |
||
| 350 | public function getMargin() |
||
| 354 | |||
| 355 | /** |
||
| 356 | * @param int $margin |
||
| 357 | * @return MenuStyle |
||
| 358 | */ |
||
| 359 | public function setMargin($margin) |
||
| 367 | |||
| 368 | /** |
||
| 369 | * @return int |
||
| 370 | */ |
||
| 371 | public function getContentWidth() |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Get padding for right had side of content |
||
| 378 | * |
||
| 379 | * @param $contentLength |
||
| 380 | * @return int |
||
| 381 | */ |
||
| 382 | public function getRightHandPadding($contentLength) |
||
| 386 | |||
| 387 | /** |
||
| 388 | * @return string |
||
| 389 | */ |
||
| 390 | public function getSelectedMarker() |
||
| 394 | |||
| 395 | /** |
||
| 396 | * @param string $marker |
||
| 397 | * @return $this |
||
| 398 | */ |
||
| 399 | public function setSelectedMarker($marker) |
||
| 405 | |||
| 406 | /** |
||
| 407 | * @return string |
||
| 408 | */ |
||
| 409 | public function getUnselectedMarker() |
||
| 413 | |||
| 414 | /** |
||
| 415 | * @param string $marker |
||
| 416 | * @return $this |
||
| 417 | */ |
||
| 418 | public function setUnselectedMarker($marker) |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Get the correct marker for the item |
||
| 427 | * |
||
| 428 | * @param bool $selected |
||
| 429 | * @return string |
||
| 430 | */ |
||
| 431 | public function getMarker($selected) |
||
| 435 | |||
| 436 | /** |
||
| 437 | * @param string $itemExtra |
||
| 438 | * @return $this |
||
| 439 | */ |
||
| 440 | public function setItemExtra($itemExtra) |
||
| 446 | |||
| 447 | /** |
||
| 448 | * @return string |
||
| 449 | */ |
||
| 450 | public function getItemExtra() |
||
| 454 | |||
| 455 | /** |
||
| 456 | * @return bool |
||
| 457 | */ |
||
| 458 | public function getDisplaysExtra() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * @param bool $displaysExtra |
||
| 465 | * @return $this |
||
| 466 | */ |
||
| 467 | public function setDisplaysExtra($displaysExtra) |
||
| 473 | |||
| 474 | /** |
||
| 475 | * @return string |
||
| 476 | */ |
||
| 477 | public function getTitleSeparator() |
||
| 481 | |||
| 482 | /** |
||
| 483 | * @param string $actionSeparator |
||
| 484 | * @return $this |
||
| 485 | */ |
||
| 486 | public function setTitleSeparator($actionSeparator) |
||
| 492 | } |
||
| 493 |
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: