Complex classes like Style 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 Style, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 9 | class Style |
||
| 10 | { |
||
| 11 | /** Default values */ |
||
| 12 | const DEFAULT_FONT_SIZE = 11; |
||
| 13 | const DEFAULT_FONT_COLOR = Color::BLACK; |
||
| 14 | const DEFAULT_FONT_NAME = 'Arial'; |
||
| 15 | |||
| 16 | /** @var int|null Style ID */ |
||
| 17 | private $id; |
||
| 18 | |||
| 19 | /** @var bool Whether the font should be bold */ |
||
| 20 | private $fontBold = false; |
||
| 21 | /** @var bool Whether the bold property was set */ |
||
| 22 | private $hasSetFontBold = false; |
||
| 23 | |||
| 24 | /** @var bool Whether the font should be italic */ |
||
| 25 | private $fontItalic = false; |
||
| 26 | /** @var bool Whether the italic property was set */ |
||
| 27 | private $hasSetFontItalic = false; |
||
| 28 | |||
| 29 | /** @var bool Whether the font should be underlined */ |
||
| 30 | private $fontUnderline = false; |
||
| 31 | /** @var bool Whether the underline property was set */ |
||
| 32 | private $hasSetFontUnderline = false; |
||
| 33 | |||
| 34 | /** @var bool Whether the font should be struck through */ |
||
| 35 | private $fontStrikethrough = false; |
||
| 36 | /** @var bool Whether the strikethrough property was set */ |
||
| 37 | private $hasSetFontStrikethrough = false; |
||
| 38 | |||
| 39 | /** @var int Font size */ |
||
| 40 | private $fontSize = self::DEFAULT_FONT_SIZE; |
||
| 41 | /** @var bool Whether the font size property was set */ |
||
| 42 | private $hasSetFontSize = false; |
||
| 43 | |||
| 44 | /** @var string Font color */ |
||
| 45 | private $fontColor = self::DEFAULT_FONT_COLOR; |
||
| 46 | /** @var bool Whether the font color property was set */ |
||
| 47 | private $hasSetFontColor = false; |
||
| 48 | |||
| 49 | /** @var string Font name */ |
||
| 50 | private $fontName = self::DEFAULT_FONT_NAME; |
||
| 51 | /** @var bool Whether the font name property was set */ |
||
| 52 | private $hasSetFontName = false; |
||
| 53 | |||
| 54 | /** @var bool Whether specific font properties should be applied */ |
||
| 55 | private $shouldApplyFont = false; |
||
| 56 | |||
| 57 | /** @var bool Whether specific cell alignment should be applied */ |
||
| 58 | private $shouldApplyCellAlignment = false; |
||
| 59 | /** @var string Cell alignment */ |
||
| 60 | private $cellAlignment; |
||
| 61 | /** @var bool Whether the cell alignment property was set */ |
||
| 62 | private $hasSetCellAlignment = false; |
||
| 63 | |||
| 64 | /** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */ |
||
| 65 | private $shouldWrapText = false; |
||
| 66 | /** @var bool Whether the wrap text property was set */ |
||
| 67 | private $hasSetWrapText = false; |
||
| 68 | |||
| 69 | /** @var Border */ |
||
| 70 | private $border; |
||
| 71 | |||
| 72 | /** @var bool Whether border properties should be applied */ |
||
| 73 | private $shouldApplyBorder = false; |
||
| 74 | |||
| 75 | /** @var string Background color */ |
||
| 76 | private $backgroundColor; |
||
| 77 | |||
| 78 | /** @var bool */ |
||
| 79 | private $hasSetBackgroundColor = false; |
||
| 80 | |||
| 81 | /** @var string Format */ |
||
| 82 | private $format; |
||
| 83 | |||
| 84 | /** @var bool */ |
||
| 85 | private $hasSetFormat = false; |
||
| 86 | |||
| 87 | /** @var bool */ |
||
| 88 | private $isRegistered = false; |
||
| 89 | |||
| 90 | 91 | /** @var bool */ |
|
| 91 | private $isEmpty = true; |
||
| 92 | 91 | ||
| 93 | /** |
||
| 94 | * @return int|null |
||
| 95 | */ |
||
| 96 | public function getId() |
||
| 100 | |||
| 101 | 91 | /** |
|
| 102 | * @param int $id |
||
| 103 | 91 | * @return Style |
|
| 104 | */ |
||
| 105 | public function setId($id) |
||
| 111 | 78 | ||
| 112 | /** |
||
| 113 | * @return Border |
||
| 114 | */ |
||
| 115 | public function getBorder() |
||
| 119 | |||
| 120 | 8 | /** |
|
| 121 | 8 | * @param Border $border |
|
| 122 | * @return Style |
||
| 123 | 8 | */ |
|
| 124 | public function setBorder(Border $border) |
||
| 132 | |||
| 133 | /** |
||
| 134 | * @return bool |
||
| 135 | */ |
||
| 136 | public function shouldApplyBorder() |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @return bool |
||
| 143 | */ |
||
| 144 | public function isFontBold() |
||
| 148 | 20 | ||
| 149 | 20 | /** |
|
| 150 | * @return Style |
||
| 151 | 20 | */ |
|
| 152 | public function setFontBold() |
||
| 161 | |||
| 162 | /** |
||
| 163 | * @return bool |
||
| 164 | */ |
||
| 165 | 80 | public function hasSetFontBold() |
|
| 169 | |||
| 170 | /** |
||
| 171 | * @return bool |
||
| 172 | */ |
||
| 173 | 7 | public function isFontItalic() |
|
| 177 | 7 | ||
| 178 | /** |
||
| 179 | 7 | * @return Style |
|
| 180 | */ |
||
| 181 | public function setFontItalic() |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return bool |
||
| 193 | 80 | */ |
|
| 194 | public function hasSetFontItalic() |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @return bool |
||
| 201 | 7 | */ |
|
| 202 | public function isFontUnderline() |
||
| 206 | |||
| 207 | 7 | /** |
|
| 208 | * @return Style |
||
| 209 | */ |
||
| 210 | public function setFontUnderline() |
||
| 219 | |||
| 220 | /** |
||
| 221 | 80 | * @return bool |
|
| 222 | */ |
||
| 223 | 80 | public function hasSetFontUnderline() |
|
| 227 | |||
| 228 | /** |
||
| 229 | 4 | * @return bool |
|
| 230 | */ |
||
| 231 | 4 | public function isFontStrikethrough() |
|
| 235 | 4 | ||
| 236 | /** |
||
| 237 | * @return Style |
||
| 238 | */ |
||
| 239 | public function setFontStrikethrough() |
||
| 248 | |||
| 249 | 82 | /** |
|
| 250 | * @return bool |
||
| 251 | 82 | */ |
|
| 252 | public function hasSetFontStrikethrough() |
||
| 256 | |||
| 257 | /** |
||
| 258 | 54 | * @return int |
|
| 259 | */ |
||
| 260 | 54 | public function getFontSize() |
|
| 264 | 54 | ||
| 265 | /** |
||
| 266 | * @param int $fontSize Font size, in pixels |
||
| 267 | * @return Style |
||
| 268 | */ |
||
| 269 | public function setFontSize($fontSize) |
||
| 278 | 82 | ||
| 279 | /** |
||
| 280 | 82 | * @return bool |
|
| 281 | */ |
||
| 282 | public function hasSetFontSize() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @return string |
||
| 289 | 3 | */ |
|
| 290 | public function getFontColor() |
||
| 294 | |||
| 295 | 3 | /** |
|
| 296 | * Sets the font color. |
||
| 297 | * |
||
| 298 | * @param string $fontColor ARGB color (@see Color) |
||
| 299 | * @return Style |
||
| 300 | */ |
||
| 301 | 77 | public function setFontColor($fontColor) |
|
| 310 | |||
| 311 | 86 | /** |
|
| 312 | * @return bool |
||
| 313 | */ |
||
| 314 | public function hasSetFontColor() |
||
| 318 | 52 | ||
| 319 | /** |
||
| 320 | 52 | * @return string |
|
| 321 | 52 | */ |
|
| 322 | 52 | public function getFontName() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * @param string $fontName Name of the font to use |
||
| 329 | * @return Style |
||
| 330 | 77 | */ |
|
| 331 | public function setFontName($fontName) |
||
| 340 | 2 | ||
| 341 | /** |
||
| 342 | * @return bool |
||
| 343 | */ |
||
| 344 | public function hasSetFontName() |
||
| 348 | 3 | ||
| 349 | /** |
||
| 350 | 3 | * @return string |
|
| 351 | 3 | */ |
|
| 352 | 3 | public function getCellAlignment() |
|
| 356 | |||
| 357 | /** |
||
| 358 | * @param string $cellAlignment The cell alignment |
||
| 359 | * |
||
| 360 | 77 | * @return Style |
|
| 361 | */ |
||
| 362 | 77 | public function setCellAlignment($cellAlignment) |
|
| 371 | |||
| 372 | /** |
||
| 373 | * @return bool |
||
| 374 | */ |
||
| 375 | public function hasSetCellAlignment() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return bool Whether specific cell alignment should be applied |
||
| 382 | */ |
||
| 383 | public function shouldApplyCellAlignment() |
||
| 387 | 8 | ||
| 388 | 8 | /** |
|
| 389 | * @return bool |
||
| 390 | 8 | */ |
|
| 391 | public function shouldWrapText() |
||
| 395 | |||
| 396 | 79 | /** |
|
| 397 | * @param bool $shouldWrap Should the text be wrapped |
||
| 398 | 79 | * @return Style |
|
| 399 | */ |
||
| 400 | public function setShouldWrapText($shouldWrap = true) |
||
| 408 | |||
| 409 | /** |
||
| 410 | * @return bool |
||
| 411 | */ |
||
| 412 | public function hasSetWrapText() |
||
| 416 | 7 | ||
| 417 | 7 | /** |
|
| 418 | * @return bool Whether specific font properties should be applied |
||
| 419 | 7 | */ |
|
| 420 | public function shouldApplyFont() |
||
| 424 | |||
| 425 | 48 | /** |
|
| 426 | * Sets the background color |
||
| 427 | 48 | * @param string $color ARGB color (@see Color) |
|
| 428 | * @return Style |
||
| 429 | */ |
||
| 430 | public function setBackgroundColor($color) |
||
| 438 | |||
| 439 | /** |
||
| 440 | * @return string |
||
| 441 | */ |
||
| 442 | public function getBackgroundColor() |
||
| 446 | 3 | ||
| 447 | /** |
||
| 448 | 3 | * @return bool Whether the background color should be applied |
|
| 449 | */ |
||
| 450 | public function shouldApplyBackgroundColor() |
||
| 454 | 87 | ||
| 455 | /** |
||
| 456 | 87 | * Sets format |
|
| 457 | * @param string $format |
||
| 458 | * @return Style |
||
| 459 | */ |
||
| 460 | public function setFormat($format) |
||
| 468 | |||
| 469 | /** |
||
| 470 | * @return string |
||
| 471 | */ |
||
| 472 | public function getFormat() |
||
| 476 | |||
| 477 | /** |
||
| 478 | * @return bool Whether format should be applied |
||
| 479 | */ |
||
| 480 | public function shouldApplyFormat() |
||
| 484 | |||
| 485 | /** |
||
| 486 | * @return bool |
||
| 487 | */ |
||
| 488 | public function isRegistered() : bool |
||
| 492 | |||
| 493 | public function markAsRegistered(?int $id) : void |
||
| 494 | { |
||
| 495 | $this->setId($id); |
||
| 496 | $this->isRegistered = true; |
||
| 497 | } |
||
| 498 | |||
| 499 | public function unmarkAsRegistered() : void |
||
| 500 | { |
||
| 501 | $this->setId(0); |
||
| 502 | $this->isRegistered = false; |
||
| 503 | } |
||
| 504 | |||
| 505 | public function isEmpty() : bool |
||
| 509 | } |
||
| 510 |