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 |
||
| 15 | class MenuStyle |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var Terminal |
||
| 19 | */ |
||
| 20 | protected $terminal; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var int|string |
||
| 24 | */ |
||
| 25 | protected $fg; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var int|string |
||
| 29 | */ |
||
| 30 | protected $bg; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var int |
||
| 34 | */ |
||
| 35 | protected $width; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var int |
||
| 39 | */ |
||
| 40 | protected $padding; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var int |
||
| 44 | */ |
||
| 45 | protected $margin; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var int |
||
| 49 | */ |
||
| 50 | protected $contentWidth; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var string |
||
| 54 | */ |
||
| 55 | private $selectedMarker; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var string |
||
| 59 | */ |
||
| 60 | private $unselectedMarker; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var string |
||
| 64 | */ |
||
| 65 | private $itemExtra; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var bool |
||
| 69 | */ |
||
| 70 | private $displaysExtra; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var string |
||
| 74 | */ |
||
| 75 | private $titleSeparator; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * @var string |
||
| 79 | */ |
||
| 80 | private $coloursSetCode; |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @var string |
||
| 84 | */ |
||
| 85 | private $invertedColoursSetCode = "\033[7m"; |
||
| 86 | |||
| 87 | /** |
||
| 88 | * @var string |
||
| 89 | */ |
||
| 90 | private $invertedColoursUnsetCode = "\033[27m"; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @var string |
||
| 94 | */ |
||
| 95 | private $coloursResetCode = "\033[0m"; |
||
| 96 | |||
| 97 | /** |
||
| 98 | * @var int |
||
| 99 | */ |
||
| 100 | private $borderTopWidth; |
||
| 101 | |||
| 102 | /** |
||
| 103 | * @var int |
||
| 104 | */ |
||
| 105 | private $borderRightWidth; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | private $borderBottomWidth; |
||
| 111 | |||
| 112 | /** |
||
| 113 | * @var int |
||
| 114 | */ |
||
| 115 | private $borderLeftWidth; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * @var string |
||
| 119 | */ |
||
| 120 | private $borderColour; |
||
| 121 | |||
| 122 | /** |
||
| 123 | * @var bool |
||
| 124 | */ |
||
| 125 | private $marginAuto = false; |
||
| 126 | |||
| 127 | /** |
||
| 128 | * Default Values |
||
| 129 | * |
||
| 130 | * @var array |
||
| 131 | */ |
||
| 132 | private static $defaultStyleValues = [ |
||
| 133 | 'fg' => 'white', |
||
| 134 | 'bg' => 'blue', |
||
| 135 | 'width' => 100, |
||
| 136 | 'padding' => 2, |
||
| 137 | 'margin' => 2, |
||
| 138 | 'selectedMarker' => '●', |
||
| 139 | 'unselectedMarker' => '○', |
||
| 140 | 'itemExtra' => '✔', |
||
| 141 | 'displaysExtra' => false, |
||
| 142 | 'titleSeparator' => '=', |
||
| 143 | 'borderTopWidth' => 0, |
||
| 144 | 'borderRightWidth' => 0, |
||
| 145 | 'borderBottomWidth' => 0, |
||
| 146 | 'borderLeftWidth' => 0, |
||
| 147 | 'borderColour' => 'white', |
||
| 148 | 'marginAuto' => false, |
||
| 149 | ]; |
||
| 150 | |||
| 151 | public static function getDefaultStyleValues() : array |
||
| 152 | { |
||
| 153 | return static::$defaultStyleValues; |
||
|
|
|||
| 154 | } |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var array |
||
| 158 | */ |
||
| 159 | private static $availableForegroundColors = array( |
||
| 160 | 'black' => 30, |
||
| 161 | 'red' => 31, |
||
| 162 | 'green' => 32, |
||
| 163 | 'yellow' => 33, |
||
| 164 | 'blue' => 34, |
||
| 165 | 'magenta' => 35, |
||
| 166 | 'cyan' => 36, |
||
| 167 | 'white' => 37, |
||
| 168 | 'default' => 39, |
||
| 169 | ); |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var array |
||
| 173 | */ |
||
| 174 | private static $availableBackgroundColors = array( |
||
| 175 | 'black' => 40, |
||
| 176 | 'red' => 41, |
||
| 177 | 'green' => 42, |
||
| 178 | 'yellow' => 43, |
||
| 179 | 'blue' => 44, |
||
| 180 | 'magenta' => 45, |
||
| 181 | 'cyan' => 46, |
||
| 182 | 'white' => 47, |
||
| 183 | 'default' => 49, |
||
| 184 | ); |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var array |
||
| 188 | */ |
||
| 189 | private static $availableOptions = array( |
||
| 190 | 'bold' => array('set' => 1, 'unset' => 22), |
||
| 191 | 'dim' => array('set' => 2, 'unset' => 22), |
||
| 192 | 'underscore' => array('set' => 4, 'unset' => 24), |
||
| 193 | 'blink' => array('set' => 5, 'unset' => 25), |
||
| 194 | 'reverse' => array('set' => 7, 'unset' => 27), |
||
| 195 | 'conceal' => array('set' => 8, 'unset' => 28) |
||
| 196 | ); |
||
| 197 | |||
| 198 | /** |
||
| 199 | * Initialise style |
||
| 200 | */ |
||
| 201 | public function __construct(Terminal $terminal = null) |
||
| 202 | { |
||
| 203 | $this->terminal = $terminal ?: TerminalFactory::fromSystem(); |
||
| 204 | |||
| 205 | $this->setFg(static::$defaultStyleValues['fg']); |
||
| 206 | $this->setBg(static::$defaultStyleValues['bg']); |
||
| 207 | $this->setWidth(static::$defaultStyleValues['width']); |
||
| 208 | $this->setPadding(static::$defaultStyleValues['padding']); |
||
| 209 | $this->setMargin(static::$defaultStyleValues['margin']); |
||
| 210 | $this->setSelectedMarker(static::$defaultStyleValues['selectedMarker']); |
||
| 211 | $this->setUnselectedMarker(static::$defaultStyleValues['unselectedMarker']); |
||
| 212 | $this->setItemExtra(static::$defaultStyleValues['itemExtra']); |
||
| 213 | $this->setDisplaysExtra(static::$defaultStyleValues['displaysExtra']); |
||
| 214 | $this->setTitleSeparator(static::$defaultStyleValues['titleSeparator']); |
||
| 215 | $this->setBorderTopWidth(static::$defaultStyleValues['borderTopWidth']); |
||
| 216 | $this->setBorderRightWidth(static::$defaultStyleValues['borderRightWidth']); |
||
| 217 | $this->setBorderBottomWidth(static::$defaultStyleValues['borderBottomWidth']); |
||
| 218 | $this->setBorderLeftWidth(static::$defaultStyleValues['borderLeftWidth']); |
||
| 219 | $this->setBorderColour(static::$defaultStyleValues['borderColour']); |
||
| 220 | } |
||
| 221 | |||
| 222 | public function getDisabledItemText(string $text) : string |
||
| 223 | { |
||
| 224 | return sprintf( |
||
| 225 | "\033[%sm%s\033[%sm", |
||
| 226 | self::$availableOptions['dim']['set'], |
||
| 227 | $text, |
||
| 228 | self::$availableOptions['dim']['unset'] |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Generates the ansi escape sequence to set the colours |
||
| 234 | */ |
||
| 235 | private function generateColoursSetCode() : void |
||
| 236 | { |
||
| 237 | if (is_string($this->fg)) { |
||
| 238 | $fgCode = self::$availableForegroundColors[$this->fg]; |
||
| 239 | } else { |
||
| 240 | $fgCode = sprintf("38;5;%s", $this->fg); |
||
| 241 | } |
||
| 242 | |||
| 243 | if (is_string($this->bg)) { |
||
| 244 | $bgCode = self::$availableBackgroundColors[$this->bg]; |
||
| 245 | } else { |
||
| 246 | $bgCode = sprintf("48;5;%s", $this->bg); |
||
| 247 | } |
||
| 248 | |||
| 249 | $this->coloursSetCode = sprintf("\033[%s;%sm", $fgCode, $bgCode); |
||
| 250 | } |
||
| 251 | |||
| 252 | /** |
||
| 253 | * Get the colour code for Bg and Fg |
||
| 254 | */ |
||
| 255 | public function getColoursSetCode() : string |
||
| 256 | { |
||
| 257 | return $this->coloursSetCode; |
||
| 258 | } |
||
| 259 | |||
| 260 | /** |
||
| 261 | * Get the inverted escape sequence (used for selected elements) |
||
| 262 | */ |
||
| 263 | public function getInvertedColoursSetCode() : string |
||
| 264 | { |
||
| 265 | return $this->invertedColoursSetCode; |
||
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Get the inverted escape sequence (used for selected elements) |
||
| 270 | */ |
||
| 271 | public function getInvertedColoursUnsetCode() : string |
||
| 272 | { |
||
| 273 | return $this->invertedColoursUnsetCode; |
||
| 274 | } |
||
| 275 | |||
| 276 | /** |
||
| 277 | * Get the escape sequence used to reset colours to default |
||
| 278 | */ |
||
| 279 | public function getColoursResetCode() : string |
||
| 280 | { |
||
| 281 | return $this->coloursResetCode; |
||
| 282 | } |
||
| 283 | |||
| 284 | /** |
||
| 285 | * Calculate the contents width |
||
| 286 | */ |
||
| 287 | protected function calculateContentWidth() : void |
||
| 288 | { |
||
| 289 | $this->contentWidth = $this->width |
||
| 290 | - ($this->padding * 2) |
||
| 291 | - ($this->borderRightWidth + $this->borderLeftWidth); |
||
| 292 | } |
||
| 293 | |||
| 294 | public function getFg() |
||
| 295 | { |
||
| 296 | return $this->fg; |
||
| 297 | } |
||
| 298 | |||
| 299 | View Code Duplication | public function setFg($fg, string $fallback = null) : self |
|
| 300 | { |
||
| 301 | $this->fg = ColourUtil::validateColour( |
||
| 302 | $this->terminal, |
||
| 303 | $fg, |
||
| 304 | $fallback |
||
| 305 | ); |
||
| 306 | $this->generateColoursSetCode(); |
||
| 307 | |||
| 308 | return $this; |
||
| 309 | } |
||
| 310 | |||
| 311 | public function getBg() |
||
| 312 | { |
||
| 313 | return $this->bg; |
||
| 314 | } |
||
| 315 | |||
| 316 | View Code Duplication | public function setBg($bg, string $fallback = null) : self |
|
| 317 | { |
||
| 318 | $this->bg = ColourUtil::validateColour( |
||
| 319 | $this->terminal, |
||
| 320 | $bg, |
||
| 321 | $fallback |
||
| 322 | ); |
||
| 323 | $this->generateColoursSetCode(); |
||
| 324 | |||
| 325 | return $this; |
||
| 326 | } |
||
| 327 | |||
| 328 | public function getWidth() : int |
||
| 329 | { |
||
| 330 | return $this->width; |
||
| 331 | } |
||
| 332 | |||
| 333 | public function setWidth(int $width) : self |
||
| 334 | { |
||
| 335 | if ($width >= $this->terminal->getWidth()) { |
||
| 336 | $width = $this->terminal->getWidth(); |
||
| 337 | } |
||
| 338 | |||
| 339 | $this->width = $width; |
||
| 340 | if ($this->marginAuto) { |
||
| 341 | $this->setMarginAuto(); |
||
| 342 | } |
||
| 343 | $this->calculateContentWidth(); |
||
| 344 | |||
| 345 | return $this; |
||
| 346 | } |
||
| 347 | |||
| 348 | public function getPadding() : int |
||
| 349 | { |
||
| 350 | return $this->padding; |
||
| 351 | } |
||
| 352 | |||
| 353 | public function setPadding(int $padding) : self |
||
| 354 | { |
||
| 355 | $this->padding = $padding; |
||
| 356 | |||
| 357 | $this->calculateContentWidth(); |
||
| 358 | |||
| 359 | return $this; |
||
| 360 | } |
||
| 361 | |||
| 362 | public function getMargin() : int |
||
| 363 | { |
||
| 364 | return $this->margin; |
||
| 365 | } |
||
| 366 | |||
| 367 | public function setMarginAuto() : self |
||
| 368 | { |
||
| 369 | $this->marginAuto = true; |
||
| 370 | $this->margin = floor(($this->terminal->getWidth() - $this->width) / 2); |
||
| 371 | |||
| 372 | return $this; |
||
| 373 | } |
||
| 374 | |||
| 375 | public function setMargin(int $margin) : self |
||
| 376 | { |
||
| 377 | $this->marginAuto = false; |
||
| 378 | $this->margin = $margin; |
||
| 379 | |||
| 380 | return $this; |
||
| 381 | } |
||
| 382 | |||
| 383 | public function getContentWidth() : int |
||
| 384 | { |
||
| 385 | return $this->contentWidth; |
||
| 386 | } |
||
| 387 | |||
| 388 | /** |
||
| 389 | * Get padding for right had side of content |
||
| 390 | */ |
||
| 391 | public function getRightHandPadding(int $contentLength) : int |
||
| 395 | |||
| 396 | public function getSelectedMarker() : string |
||
| 397 | { |
||
| 398 | return $this->selectedMarker; |
||
| 399 | } |
||
| 400 | |||
| 401 | public function setSelectedMarker(string $marker) : self |
||
| 402 | { |
||
| 403 | $this->selectedMarker = mb_substr($marker, 0, 1); |
||
| 404 | |||
| 405 | return $this; |
||
| 406 | } |
||
| 407 | |||
| 408 | public function getUnselectedMarker() : string |
||
| 409 | { |
||
| 410 | return $this->unselectedMarker; |
||
| 411 | } |
||
| 412 | |||
| 413 | public function setUnselectedMarker(string $marker) : self |
||
| 414 | { |
||
| 415 | $this->unselectedMarker = mb_substr($marker, 0, 1); |
||
| 416 | |||
| 417 | return $this; |
||
| 418 | } |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Get the correct marker for the item |
||
| 422 | */ |
||
| 423 | public function getMarker(bool $selected) : string |
||
| 427 | |||
| 428 | public function setItemExtra(string $itemExtra) : self |
||
| 434 | |||
| 435 | public function getItemExtra() : string |
||
| 439 | |||
| 440 | public function getDisplaysExtra() : bool |
||
| 444 | |||
| 445 | public function setDisplaysExtra(bool $displaysExtra) : self |
||
| 451 | |||
| 452 | public function getTitleSeparator() : string |
||
| 456 | |||
| 457 | public function setTitleSeparator(string $actionSeparator) : self |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Shorthand function to set all borders values at once |
||
| 466 | */ |
||
| 467 | public function setBorder( |
||
| 499 | |||
| 500 | public function setBorderTopWidth(int $width) : self |
||
| 506 | |||
| 507 | public function setBorderRightWidth(int $width) : self |
||
| 514 | |||
| 515 | public function setBorderBottomWidth(int $width) : self |
||
| 521 | |||
| 522 | public function setBorderLeftWidth(int $width) : self |
||
| 529 | |||
| 530 | public function setBorderColour(string $colour) : self |
||
| 536 | |||
| 537 | public function getBorderTopWidth() : int |
||
| 541 | |||
| 542 | public function getBorderRightWidth() : int |
||
| 546 | |||
| 547 | public function getBorderBottomWidth() : int |
||
| 551 | |||
| 552 | public function getBorderLeftWidth() : int |
||
| 556 | |||
| 557 | public function getBorderColour() : string |
||
| 561 | |||
| 562 | public function getBorderColourCode() : string |
||
| 566 | } |
||
| 567 |
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: