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 |
||
| 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 int |
||
| 78 | */ |
||
| 79 | private $borderTopWidth; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var int |
||
| 83 | */ |
||
| 84 | private $borderRightWidth; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | private $borderBottomWidth; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var int |
||
| 93 | */ |
||
| 94 | private $borderLeftWidth; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var string |
||
| 98 | */ |
||
| 99 | private $borderColour; |
||
| 100 | |||
| 101 | /** |
||
| 102 | * Default Values |
||
| 103 | * |
||
| 104 | * @var array |
||
| 105 | */ |
||
| 106 | private static $defaultStyleValues = [ |
||
| 107 | 'fg' => 'white', |
||
| 108 | 'bg' => 'blue', |
||
| 109 | 'width' => 100, |
||
| 110 | 'padding' => 2, |
||
| 111 | 'margin' => 2, |
||
| 112 | 'selectedMarker' => '●', |
||
| 113 | 'unselectedMarker' => '○', |
||
| 114 | 'itemExtra' => '✔', |
||
| 115 | 'displaysExtra' => false, |
||
| 116 | 'titleSeparator' => '=', |
||
| 117 | 'borderTopWidth' => 0, |
||
| 118 | 'borderRightWidth' => 0, |
||
| 119 | 'borderBottomWidth' => 0, |
||
| 120 | 'borderLeftWidth' => 0, |
||
| 121 | 'borderColour' => 'white', |
||
| 122 | ]; |
||
| 123 | |||
| 124 | public static function getDefaultStyleValues() : array |
||
| 125 | { |
||
| 126 | return static::$defaultStyleValues; |
||
|
|
|||
| 127 | } |
||
| 128 | |||
| 129 | /** |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private static $availableForegroundColors = array( |
||
| 133 | 'black' => array('set' => 30, 'unset' => 39), |
||
| 134 | 'red' => array('set' => 31, 'unset' => 39), |
||
| 135 | 'green' => array('set' => 32, 'unset' => 39), |
||
| 136 | 'yellow' => array('set' => 33, 'unset' => 39), |
||
| 137 | 'blue' => array('set' => 34, 'unset' => 39), |
||
| 138 | 'magenta' => array('set' => 35, 'unset' => 39), |
||
| 139 | 'cyan' => array('set' => 36, 'unset' => 39), |
||
| 140 | 'white' => array('set' => 37, 'unset' => 39), |
||
| 141 | 'default' => array('set' => 39, 'unset' => 39), |
||
| 142 | ); |
||
| 143 | |||
| 144 | /** |
||
| 145 | * @var array |
||
| 146 | */ |
||
| 147 | private static $availableBackgroundColors = array( |
||
| 148 | 'black' => array('set' => 40, 'unset' => 49), |
||
| 149 | 'red' => array('set' => 41, 'unset' => 49), |
||
| 150 | 'green' => array('set' => 42, 'unset' => 49), |
||
| 151 | 'yellow' => array('set' => 43, 'unset' => 49), |
||
| 152 | 'blue' => array('set' => 44, 'unset' => 49), |
||
| 153 | 'magenta' => array('set' => 45, 'unset' => 49), |
||
| 154 | 'cyan' => array('set' => 46, 'unset' => 49), |
||
| 155 | 'white' => array('set' => 47, 'unset' => 49), |
||
| 156 | 'default' => array('set' => 49, 'unset' => 49), |
||
| 157 | ); |
||
| 158 | |||
| 159 | /** |
||
| 160 | * @var array |
||
| 161 | */ |
||
| 162 | private static $availableOptions = array( |
||
| 163 | 'bold' => array('set' => 1, 'unset' => 22), |
||
| 164 | 'dim' => array('set' => 2, 'unset' => 22), |
||
| 165 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
| 166 | 'blink' => array('set' => 5, 'unset' => 25), |
||
| 167 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
| 168 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
| 169 | ); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * Initialise style |
||
| 173 | */ |
||
| 174 | public function __construct(Terminal $terminal = null) |
||
| 175 | { |
||
| 176 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
| 177 | |||
| 178 | $this->setFg(static::$defaultStyleValues['fg']); |
||
| 179 | $this->setBg(static::$defaultStyleValues['bg']); |
||
| 180 | $this->setWidth(static::$defaultStyleValues['width']); |
||
| 181 | $this->setPadding(static::$defaultStyleValues['padding']); |
||
| 182 | $this->setMargin(static::$defaultStyleValues['margin']); |
||
| 183 | $this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']); |
||
| 184 | $this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']); |
||
| 185 | $this->setItemExtra(static::$defaultStyleValues['itemExtra']); |
||
| 186 | $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); |
||
| 187 | $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); |
||
| 188 | $this->setBorderTopWidth(static::$defaultStyleValues['borderTopWidth']); |
||
| 189 | $this->setBorderRightWidth(static::$defaultStyleValues['borderRightWidth']); |
||
| 190 | $this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']); |
||
| 191 | $this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']); |
||
| 192 | $this->setBorderColour(static::$defaultStyleValues['borderColour']); |
||
| 193 | } |
||
| 194 | |||
| 195 | public static function getAvailableColours() : array |
||
| 196 | { |
||
| 197 | return array_keys(self::$availableBackgroundColors); |
||
| 198 | } |
||
| 199 | |||
| 200 | public function getDisabledItemText(string $text) : string |
||
| 201 | { |
||
| 202 | return sprintf( |
||
| 203 | "\033[%sm%s\033[%sm", |
||
| 204 | self::$availableOptions['dim']['set'], |
||
| 205 | $text, |
||
| 206 | self::$availableOptions['dim']['unset'] |
||
| 207 | ); |
||
| 208 | } |
||
| 209 | |||
| 210 | /** |
||
| 211 | * Get the colour code set for Bg and Fg |
||
| 212 | */ |
||
| 213 | View Code Duplication | public function getSelectedSetCode() : string |
|
| 214 | { |
||
| 215 | return sprintf( |
||
| 216 | "\033[%sm", |
||
| 217 | implode(';', [ |
||
| 218 | self::$availableBackgroundColors[$this->getFg()]['set'], |
||
| 219 | self::$availableForegroundColors[$this->getBg()]['set'], |
||
| 220 | ]) |
||
| 221 | ); |
||
| 222 | } |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Get the colour unset code for Bg and Fg |
||
| 226 | */ |
||
| 227 | View Code Duplication | public function getSelectedUnsetCode() : string |
|
| 228 | { |
||
| 229 | return sprintf( |
||
| 230 | "\033[%sm", |
||
| 231 | implode(';', [ |
||
| 232 | self::$availableBackgroundColors[$this->getBg()]['unset'], |
||
| 233 | self::$availableForegroundColors[$this->getFg()]['unset'], |
||
| 234 | ]) |
||
| 235 | ); |
||
| 236 | } |
||
| 237 | |||
| 238 | /** |
||
| 239 | * Get the inverted colour code |
||
| 240 | */ |
||
| 241 | View Code Duplication | public function getUnselectedSetCode() : string |
|
| 242 | { |
||
| 243 | return sprintf( |
||
| 244 | "\033[%sm", |
||
| 245 | implode(';', [ |
||
| 246 | self::$availableBackgroundColors[$this->getBg()]['set'], |
||
| 247 | self::$availableForegroundColors[$this->getFg()]['set'], |
||
| 248 | ]) |
||
| 249 | ); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the inverted colour unset code |
||
| 254 | */ |
||
| 255 | View Code Duplication | public function getUnselectedUnsetCode() : string |
|
| 256 | { |
||
| 257 | return sprintf( |
||
| 258 | "\033[%sm", |
||
| 259 | implode(';', [ |
||
| 260 | self::$availableBackgroundColors[$this->getBg()]['unset'], |
||
| 261 | self::$availableForegroundColors[$this->getFg()]['unset'], |
||
| 262 | ]) |
||
| 263 | ); |
||
| 264 | } |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Calculate the contents width |
||
| 268 | */ |
||
| 269 | protected function calculateContentWidth() : void |
||
| 270 | { |
||
| 271 | $this->contentWidth = $this->width |
||
| 272 | - ($this->padding*2) |
||
| 273 | - ($this->margin*2) |
||
| 274 | - ($this->borderRightWidth + $this->borderLeftWidth); |
||
| 275 | } |
||
| 276 | |||
| 277 | public function getFg() : string |
||
| 278 | { |
||
| 279 | return $this->fg; |
||
| 280 | } |
||
| 281 | |||
| 282 | public function setFg(string $fg) : self |
||
| 283 | { |
||
| 284 | $this->fg = $fg; |
||
| 285 | |||
| 286 | return $this; |
||
| 287 | } |
||
| 288 | |||
| 289 | public function getBg() : string |
||
| 290 | { |
||
| 291 | return $this->bg; |
||
| 292 | } |
||
| 293 | |||
| 294 | public function setBg(string $bg) : self |
||
| 295 | { |
||
| 296 | $this->bg = $bg; |
||
| 297 | |||
| 298 | return $this; |
||
| 299 | } |
||
| 300 | |||
| 301 | public function getWidth() : int |
||
| 302 | { |
||
| 303 | return $this->width; |
||
| 304 | } |
||
| 305 | |||
| 306 | public function setWidth(int $width) : self |
||
| 307 | { |
||
| 308 | $availableWidth = $this->terminal->getWidth() |
||
| 309 | - ($this->margin * 2) |
||
| 310 | - ($this->padding * 2) |
||
| 311 | - ($this->borderRightWidth + $this->borderLeftWidth); |
||
| 312 | |||
| 313 | if ($width >= $availableWidth) { |
||
| 314 | $width = $availableWidth; |
||
| 315 | } |
||
| 316 | |||
| 317 | $this->width = $width; |
||
| 318 | $this->calculateContentWidth(); |
||
| 319 | |||
| 320 | return $this; |
||
| 321 | } |
||
| 322 | |||
| 323 | public function getPadding() : int |
||
| 327 | |||
| 328 | public function setPadding(int $padding) : self |
||
| 329 | { |
||
| 330 | $this->padding = $padding; |
||
| 331 | |||
| 332 | $this->calculateContentWidth(); |
||
| 333 | |||
| 334 | return $this; |
||
| 335 | } |
||
| 336 | |||
| 337 | public function getMargin() : int |
||
| 338 | { |
||
| 339 | return $this->margin; |
||
| 340 | } |
||
| 341 | |||
| 342 | public function setMargin(int $margin) : self |
||
| 343 | { |
||
| 344 | $this->margin = $margin; |
||
| 350 | |||
| 351 | public function getContentWidth() : int |
||
| 355 | |||
| 356 | /** |
||
| 357 | * Get padding for right had side of content |
||
| 358 | */ |
||
| 359 | public function getRightHandPadding(int $contentLength) : int |
||
| 363 | |||
| 364 | public function getSelectedMarker() : string |
||
| 368 | |||
| 369 | public function setSelectedMarker(string $marker) : self |
||
| 375 | |||
| 376 | public function getUnselectedMarker() : string |
||
| 380 | |||
| 381 | public function setUnselectedMarker(string $marker) : self |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get the correct marker for the item |
||
| 390 | */ |
||
| 391 | public function getMarker(bool $selected) : string |
||
| 395 | |||
| 396 | public function setItemExtra(string $itemExtra) : self |
||
| 402 | |||
| 403 | public function getItemExtra() : string |
||
| 407 | |||
| 408 | public function getDisplaysExtra() : bool |
||
| 412 | |||
| 413 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
| 419 | |||
| 420 | public function getTitleSeparator() : string |
||
| 424 | |||
| 425 | public function setTitleSeparator(string $actionSeparator) : self |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Shorthand function to set all borders values at once |
||
| 434 | */ |
||
| 435 | public function setBorder( |
||
| 467 | |||
| 468 | public function setBorderTopWidth(int $width) : self |
||
| 474 | |||
| 475 | public function setBorderRightWidth(int $width) : self |
||
| 482 | |||
| 483 | public function setBorderBottomWidth(int $width) : self |
||
| 489 | |||
| 490 | public function setBorderLeftWidth(int $width) : self |
||
| 497 | |||
| 498 | public function setBorderColour(string $colour) : self |
||
| 504 | |||
| 505 | public function getBorderTopWidth() : int |
||
| 509 | |||
| 510 | public function getBorderRightWidth() : int |
||
| 514 | |||
| 515 | public function getBorderBottomWidth() : int |
||
| 519 | |||
| 520 | public function getBorderLeftWidth() : int |
||
| 524 | |||
| 525 | public function getBorderColour() : string |
||
| 529 | |||
| 530 | public function getBorderColourCode() : string |
||
| 534 | } |
||
| 535 |
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: