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 | * Default Values |
||
| 78 | * |
||
| 79 | * @var array |
||
| 80 | */ |
||
| 81 | private static $defaultStyleValues = [ |
||
| 82 | 'fg' => 'white', |
||
| 83 | 'bg' => 'blue', |
||
| 84 | 'width' => 100, |
||
| 85 | 'padding' => 2, |
||
| 86 | 'margin' => 2, |
||
| 87 | 'selectedMarker' => '●', |
||
| 88 | 'unselectedMarker' => '○', |
||
| 89 | 'itemExtra' => '✔', |
||
| 90 | 'displaysExtra' => false, |
||
| 91 | 'titleSeparator' => '=', |
||
| 92 | ]; |
||
| 93 | |||
| 94 | public static function getDefaultStyleValues() : array |
||
| 98 | |||
| 99 | /** |
||
| 100 | * @var array |
||
| 101 | */ |
||
| 102 | private static $availableForegroundColors = array( |
||
| 103 | 'black' => array('set' => 30, 'unset' => 39), |
||
| 104 | 'red' => array('set' => 31, 'unset' => 39), |
||
| 105 | 'green' => array('set' => 32, 'unset' => 39), |
||
| 106 | 'yellow' => array('set' => 33, 'unset' => 39), |
||
| 107 | 'blue' => array('set' => 34, 'unset' => 39), |
||
| 108 | 'magenta' => array('set' => 35, 'unset' => 39), |
||
| 109 | 'cyan' => array('set' => 36, 'unset' => 39), |
||
| 110 | 'white' => array('set' => 37, 'unset' => 39), |
||
| 111 | 'default' => array('set' => 39, 'unset' => 39), |
||
| 112 | ); |
||
| 113 | |||
| 114 | /** |
||
| 115 | * @var array |
||
| 116 | */ |
||
| 117 | private static $availableBackgroundColors = array( |
||
| 118 | 'black' => array('set' => 40, 'unset' => 49), |
||
| 119 | 'red' => array('set' => 41, 'unset' => 49), |
||
| 120 | 'green' => array('set' => 42, 'unset' => 49), |
||
| 121 | 'yellow' => array('set' => 43, 'unset' => 49), |
||
| 122 | 'blue' => array('set' => 44, 'unset' => 49), |
||
| 123 | 'magenta' => array('set' => 45, 'unset' => 49), |
||
| 124 | 'cyan' => array('set' => 46, 'unset' => 49), |
||
| 125 | 'white' => array('set' => 47, 'unset' => 49), |
||
| 126 | 'default' => array('set' => 49, 'unset' => 49), |
||
| 127 | ); |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private static $availableOptions = array( |
||
| 133 | 'bold' => array('set' => 1, 'unset' => 22), |
||
| 134 | 'dim' => array('set' => 2, 'unset' => 22), |
||
| 135 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
| 136 | 'blink' => array('set' => 5, 'unset' => 25), |
||
| 137 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
| 138 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
| 139 | ); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Initialise style |
||
| 143 | */ |
||
| 144 | public function __construct(Terminal $terminal = null) |
||
| 145 | { |
||
| 146 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
| 147 | |||
| 148 | $this->setFg(static::$defaultStyleValues['fg']); |
||
| 149 | $this->setBg(static::$defaultStyleValues['bg']); |
||
| 150 | $this->setWidth(static::$defaultStyleValues['width']); |
||
| 151 | $this->setPadding(static::$defaultStyleValues['padding']); |
||
| 152 | $this->setMargin(static::$defaultStyleValues['margin']); |
||
| 153 | $this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']); |
||
| 154 | $this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']); |
||
| 155 | $this->setItemExtra(static::$defaultStyleValues['itemExtra']); |
||
| 156 | $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); |
||
| 157 | $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); |
||
| 158 | } |
||
| 159 | |||
| 160 | public static function getAvailableColours() : array |
||
| 164 | |||
| 165 | public function getDisabledItemText(string $text) : string |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Get the colour code set for Bg and Fg |
||
| 177 | */ |
||
| 178 | View Code Duplication | public function getSelectedSetCode() : string |
|
| 188 | |||
| 189 | /** |
||
| 190 | * Get the colour unset code for Bg and Fg |
||
| 191 | */ |
||
| 192 | View Code Duplication | public function getSelectedUnsetCode() : string |
|
| 202 | |||
| 203 | /** |
||
| 204 | * Get the inverted colour code |
||
| 205 | */ |
||
| 206 | View Code Duplication | public function getUnselectedSetCode() : string |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Get the inverted colour unset code |
||
| 219 | */ |
||
| 220 | View Code Duplication | public function getUnselectedUnsetCode() : string |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Calculate the contents width |
||
| 233 | */ |
||
| 234 | protected function calculateContentWidth() : void |
||
| 238 | |||
| 239 | public function getFg() : string |
||
| 243 | |||
| 244 | public function setFg(string $fg) : self |
||
| 250 | |||
| 251 | public function getBg() : string |
||
| 255 | |||
| 256 | public function setBg(string $bg) : self |
||
| 262 | |||
| 263 | public function getWidth() : int |
||
| 267 | |||
| 268 | public function setWidth(int $width) : self |
||
| 282 | |||
| 283 | public function getPadding() : int |
||
| 287 | |||
| 288 | public function setPadding(int $padding) : self |
||
| 296 | |||
| 297 | public function getMargin() : int |
||
| 301 | |||
| 302 | public function setMargin(int $margin) : self |
||
| 312 | |||
| 313 | public function getContentWidth() : int |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Get padding for right had side of content |
||
| 320 | */ |
||
| 321 | public function getRightHandPadding(int $contentLength) : int |
||
| 325 | |||
| 326 | public function getSelectedMarker() : string |
||
| 330 | |||
| 331 | public function setSelectedMarker(string $marker) : self |
||
| 337 | |||
| 338 | public function getUnselectedMarker() : string |
||
| 342 | |||
| 343 | public function setUnselectedMarker(string $marker) : self |
||
| 349 | |||
| 350 | /** |
||
| 351 | * Get the correct marker for the item |
||
| 352 | */ |
||
| 353 | public function getMarker(bool $selected) : string |
||
| 357 | |||
| 358 | public function setItemExtra(string $itemExtra) : self |
||
| 364 | |||
| 365 | public function getItemExtra() : string |
||
| 369 | |||
| 370 | public function getDisplaysExtra() : bool |
||
| 374 | |||
| 375 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
| 381 | |||
| 382 | public function getTitleSeparator() : string |
||
| 386 | |||
| 387 | public function setTitleSeparator(string $actionSeparator) : self |
||
| 393 | } |
||
| 394 |
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: