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 bool Whether the cell should shrink to fit to content */ |
||
| 70 | private $shouldShrinkToFit = false; |
||
| 71 | /** @var bool Whether the shouldShrinkToFit text property was set */ |
||
| 72 | private $hasSetShrinkToFit = false; |
||
| 73 | |||
| 74 | /** @var Border */ |
||
| 75 | private $border; |
||
| 76 | |||
| 77 | /** @var bool Whether border properties should be applied */ |
||
| 78 | private $shouldApplyBorder = false; |
||
| 79 | |||
| 80 | /** @var string Background color */ |
||
| 81 | private $backgroundColor; |
||
| 82 | |||
| 83 | /** @var bool */ |
||
| 84 | private $hasSetBackgroundColor = false; |
||
| 85 | |||
| 86 | /** @var string Format */ |
||
| 87 | private $format; |
||
| 88 | |||
| 89 | /** @var bool */ |
||
| 90 | private $hasSetFormat = false; |
||
| 91 | |||
| 92 | /** |
||
| 93 | * @return int|null |
||
| 94 | */ |
||
| 95 | 96 | public function getId() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param int $id |
||
| 102 | * @return Style |
||
| 103 | */ |
||
| 104 | 96 | public function setId($id) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * @return Border |
||
| 113 | */ |
||
| 114 | 81 | public function getBorder() |
|
| 118 | |||
| 119 | /** |
||
| 120 | * @param Border $border |
||
| 121 | * @return Style |
||
| 122 | */ |
||
| 123 | 8 | public function setBorder(Border $border) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * @return bool |
||
| 133 | */ |
||
| 134 | 95 | public function shouldApplyBorder() |
|
| 138 | |||
| 139 | /** |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | 85 | public function isFontBold() |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @return Style |
||
| 149 | */ |
||
| 150 | 20 | public function setFontBold() |
|
| 158 | |||
| 159 | /** |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | 80 | public function hasSetFontBold() |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @return bool |
||
| 169 | */ |
||
| 170 | 85 | public function isFontItalic() |
|
| 174 | |||
| 175 | /** |
||
| 176 | * @return Style |
||
| 177 | */ |
||
| 178 | 7 | public function setFontItalic() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 80 | public function hasSetFontItalic() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * @return bool |
||
| 197 | */ |
||
| 198 | 85 | public function isFontUnderline() |
|
| 202 | |||
| 203 | /** |
||
| 204 | * @return Style |
||
| 205 | */ |
||
| 206 | 7 | public function setFontUnderline() |
|
| 214 | |||
| 215 | /** |
||
| 216 | * @return bool |
||
| 217 | */ |
||
| 218 | 80 | public function hasSetFontUnderline() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * @return bool |
||
| 225 | */ |
||
| 226 | 85 | public function isFontStrikethrough() |
|
| 230 | |||
| 231 | /** |
||
| 232 | * @return Style |
||
| 233 | */ |
||
| 234 | 4 | public function setFontStrikethrough() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @return bool |
||
| 245 | */ |
||
| 246 | 80 | public function hasSetFontStrikethrough() |
|
| 250 | |||
| 251 | /** |
||
| 252 | * @return int |
||
| 253 | */ |
||
| 254 | 87 | public function getFontSize() |
|
| 258 | |||
| 259 | /** |
||
| 260 | * @param int $fontSize Font size, in pixels |
||
| 261 | * @return Style |
||
| 262 | */ |
||
| 263 | 59 | public function setFontSize($fontSize) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * @return bool |
||
| 274 | */ |
||
| 275 | 80 | public function hasSetFontSize() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * @return string |
||
| 282 | */ |
||
| 283 | 87 | public function getFontColor() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Sets the font color. |
||
| 290 | * |
||
| 291 | * @param string $fontColor ARGB color (@see Color) |
||
| 292 | * @return Style |
||
| 293 | */ |
||
| 294 | 3 | public function setFontColor($fontColor) |
|
| 302 | |||
| 303 | /** |
||
| 304 | * @return bool |
||
| 305 | */ |
||
| 306 | 80 | public function hasSetFontColor() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 91 | public function getFontName() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * @param string $fontName Name of the font to use |
||
| 321 | * @return Style |
||
| 322 | */ |
||
| 323 | 57 | public function setFontName($fontName) |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @return bool |
||
| 334 | */ |
||
| 335 | 80 | public function hasSetFontName() |
|
| 339 | |||
| 340 | /** |
||
| 341 | * @return string |
||
| 342 | */ |
||
| 343 | 2 | public function getCellAlignment() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * @param string $cellAlignment The cell alignment |
||
| 350 | * |
||
| 351 | * @return Style |
||
| 352 | */ |
||
| 353 | 3 | public function setCellAlignment($cellAlignment) |
|
| 361 | |||
| 362 | /** |
||
| 363 | * @return bool |
||
| 364 | */ |
||
| 365 | 80 | public function hasSetCellAlignment() |
|
| 369 | |||
| 370 | /** |
||
| 371 | * @return bool Whether specific cell alignment should be applied |
||
| 372 | */ |
||
| 373 | 88 | public function shouldApplyCellAlignment() |
|
| 377 | |||
| 378 | /** |
||
| 379 | * @return bool |
||
| 380 | */ |
||
| 381 | 89 | public function shouldWrapText() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * @param bool $shouldWrap Should the text be wrapped |
||
| 388 | * @return Style |
||
| 389 | */ |
||
| 390 | 8 | public function setShouldWrapText($shouldWrap = true) |
|
| 397 | |||
| 398 | /** |
||
| 399 | * @return bool |
||
| 400 | */ |
||
| 401 | 82 | public function hasSetWrapText() |
|
| 405 | |||
| 406 | /** |
||
| 407 | * @return bool Whether specific font properties should be applied |
||
| 408 | */ |
||
| 409 | 78 | public function shouldApplyFont() |
|
| 413 | |||
| 414 | /** |
||
| 415 | * Sets the background color |
||
| 416 | * @param string $color ARGB color (@see Color) |
||
| 417 | * @return Style |
||
| 418 | */ |
||
| 419 | 7 | public function setBackgroundColor($color) |
|
| 426 | |||
| 427 | /** |
||
| 428 | * @return string |
||
| 429 | */ |
||
| 430 | 53 | public function getBackgroundColor() |
|
| 434 | |||
| 435 | /** |
||
| 436 | * @return bool Whether the background color should be applied |
||
| 437 | */ |
||
| 438 | 82 | public function shouldApplyBackgroundColor() |
|
| 442 | |||
| 443 | /** |
||
| 444 | * Sets format |
||
| 445 | * @param string $format |
||
| 446 | * @return Style |
||
| 447 | */ |
||
| 448 | 3 | public function setFormat($format) |
|
| 455 | |||
| 456 | /** |
||
| 457 | * @return string |
||
| 458 | */ |
||
| 459 | 92 | public function getFormat() |
|
| 463 | |||
| 464 | /** |
||
| 465 | * @return bool Whether format should be applied |
||
| 466 | */ |
||
| 467 | 80 | public function shouldApplyFormat() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Sets should shrink to fit |
||
| 474 | * @param bool $shrinkToFit |
||
| 475 | * @return Style |
||
| 476 | */ |
||
| 477 | 1 | public function setShouldShrinkToFit($shrinkToFit = true) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @return bool Whether format should be applied |
||
| 487 | */ |
||
| 488 | 85 | public function shouldShrinkToFit() |
|
| 492 | |||
| 493 | /** |
||
| 494 | * @return bool |
||
| 495 | */ |
||
| 496 | 80 | public function hasSetShrinkToFit() |
|
| 500 | } |
||
| 501 |