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 |
||
| 11 | class Style |
||
| 12 | { |
||
| 13 | /** Default font values */ |
||
| 14 | const DEFAULT_FONT_SIZE = 11; |
||
| 15 | const DEFAULT_FONT_COLOR = Color::BLACK; |
||
| 16 | const DEFAULT_FONT_NAME = 'Arial'; |
||
| 17 | |||
| 18 | /** @var int|null Style ID */ |
||
| 19 | protected $id = null; |
||
| 20 | |||
| 21 | /** @var bool Whether the font should be bold */ |
||
| 22 | protected $fontBold = false; |
||
| 23 | /** @var bool Whether the bold property was set */ |
||
| 24 | protected $hasSetFontBold = false; |
||
| 25 | |||
| 26 | /** @var bool Whether the font should be italic */ |
||
| 27 | protected $fontItalic = false; |
||
| 28 | /** @var bool Whether the italic property was set */ |
||
| 29 | protected $hasSetFontItalic = false; |
||
| 30 | |||
| 31 | /** @var bool Whether the font should be underlined */ |
||
| 32 | protected $fontUnderline = false; |
||
| 33 | /** @var bool Whether the underline property was set */ |
||
| 34 | protected $hasSetFontUnderline = false; |
||
| 35 | |||
| 36 | /** @var bool Whether the font should be struck through */ |
||
| 37 | protected $fontStrikethrough = false; |
||
| 38 | /** @var bool Whether the strikethrough property was set */ |
||
| 39 | protected $hasSetFontStrikethrough = false; |
||
| 40 | |||
| 41 | /** @var int Font size */ |
||
| 42 | protected $fontSize = self::DEFAULT_FONT_SIZE; |
||
| 43 | /** @var bool Whether the font size property was set */ |
||
| 44 | protected $hasSetFontSize = false; |
||
| 45 | |||
| 46 | /** @var string Font color */ |
||
| 47 | protected $fontColor = self::DEFAULT_FONT_COLOR; |
||
| 48 | /** @var bool Whether the font color property was set */ |
||
| 49 | protected $hasSetFontColor = false; |
||
| 50 | |||
| 51 | /** @var string Font name */ |
||
| 52 | protected $fontName = self::DEFAULT_FONT_NAME; |
||
| 53 | /** @var bool Whether the font name property was set */ |
||
| 54 | protected $hasSetFontName = false; |
||
| 55 | |||
| 56 | /** @var bool Whether specific font properties should be applied */ |
||
| 57 | protected $shouldApplyFont = false; |
||
| 58 | |||
| 59 | /** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */ |
||
| 60 | protected $shouldWrapText = false; |
||
| 61 | /** @var bool Whether the wrap text property was set */ |
||
| 62 | protected $hasSetWrapText = false; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * @var Border |
||
| 66 | */ |
||
| 67 | 153 | protected $border = null; |
|
| 68 | |||
| 69 | 153 | /** |
|
| 70 | * @var bool Whether border properties should be applied |
||
| 71 | */ |
||
| 72 | protected $shouldApplyBorder = false; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * @return int|null |
||
| 76 | 231 | */ |
|
| 77 | public function getId() |
||
| 81 | |||
| 82 | /** |
||
| 83 | * @param int $id |
||
| 84 | * @return Style |
||
| 85 | 114 | */ |
|
| 86 | public function setId($id) |
||
| 91 | |||
| 92 | /** |
||
| 93 | 36 | * @return Border |
|
| 94 | */ |
||
| 95 | 36 | public function getBorder() |
|
| 99 | |||
| 100 | /** |
||
| 101 | * @param Border $border |
||
| 102 | */ |
||
| 103 | public function setBorder(Border $border) |
||
| 109 | |||
| 110 | /** |
||
| 111 | * @return boolean |
||
| 112 | 12 | */ |
|
| 113 | public function shouldApplyBorder() |
||
| 117 | 12 | ||
| 118 | /** |
||
| 119 | * @return boolean |
||
| 120 | */ |
||
| 121 | public function isFontBold() |
||
| 125 | 114 | ||
| 126 | /** |
||
| 127 | * @return Style |
||
| 128 | */ |
||
| 129 | public function setFontBold() |
||
| 136 | 18 | ||
| 137 | /** |
||
| 138 | * @return boolean |
||
| 139 | */ |
||
| 140 | public function isFontItalic() |
||
| 144 | 114 | ||
| 145 | /** |
||
| 146 | * @return Style |
||
| 147 | */ |
||
| 148 | public function setFontItalic() |
||
| 155 | 12 | ||
| 156 | /** |
||
| 157 | * @return boolean |
||
| 158 | */ |
||
| 159 | public function isFontUnderline() |
||
| 163 | 168 | ||
| 164 | /** |
||
| 165 | * @return Style |
||
| 166 | */ |
||
| 167 | public function setFontUnderline() |
||
| 174 | 126 | ||
| 175 | 126 | /** |
|
| 176 | * @return boolean |
||
| 177 | */ |
||
| 178 | public function isFontStrikethrough() |
||
| 182 | |||
| 183 | 168 | /** |
|
| 184 | * @return Style |
||
| 185 | */ |
||
| 186 | public function setFontStrikethrough() |
||
| 193 | |||
| 194 | 6 | /** |
|
| 195 | 6 | * @return int |
|
| 196 | 6 | */ |
|
| 197 | 6 | public function getFontSize() |
|
| 201 | |||
| 202 | /** |
||
| 203 | 210 | * @param int $fontSize Font size, in pixels |
|
| 204 | * @return Style |
||
| 205 | 210 | */ |
|
| 206 | public function setFontSize($fontSize) |
||
| 213 | |||
| 214 | 120 | /** |
|
| 215 | 120 | * @return string |
|
| 216 | 120 | */ |
|
| 217 | 120 | public function getFontColor() |
|
| 221 | |||
| 222 | /** |
||
| 223 | 186 | * Sets the font color. |
|
| 224 | * |
||
| 225 | 186 | * @param string $fontColor ARGB color (@see Color) |
|
| 226 | * @return Style |
||
| 227 | */ |
||
| 228 | public function setFontColor($fontColor) |
||
| 235 | 30 | ||
| 236 | /** |
||
| 237 | * @return string |
||
| 238 | */ |
||
| 239 | public function getFontName() |
||
| 243 | 135 | ||
| 244 | /** |
||
| 245 | * @param string $fontName Name of the font to use |
||
| 246 | * @return Style |
||
| 247 | */ |
||
| 248 | public function setFontName($fontName) |
||
| 255 | |||
| 256 | 231 | /** |
|
| 257 | 231 | * @return boolean |
|
| 258 | */ |
||
| 259 | 231 | public function shouldWrapText() |
|
| 263 | 231 | ||
| 264 | /** |
||
| 265 | * @return Style |
||
| 266 | */ |
||
| 267 | public function setShouldWrapText() |
||
| 273 | |||
| 274 | /** |
||
| 275 | * @return bool Whether specific font properties should be applied |
||
| 276 | */ |
||
| 277 | 66 | public function shouldApplyFont() |
|
| 281 | 66 | ||
| 282 | 3 | /** |
|
| 283 | 3 | * Serializes the style for future comparison with other styles. |
|
| 284 | 66 | * The ID is excluded from the comparison, as we only care about |
|
| 285 | 3 | * actual style properties. |
|
| 286 | 3 | * |
|
| 287 | 66 | * @return string The serialized style |
|
| 288 | 3 | */ |
|
| 289 | 3 | public function serialize() |
|
| 301 | 21 | ||
| 302 | 66 | /** |
|
| 303 | 3 | * Merges the current style with the given style, using the given style as a base. This means that: |
|
| 304 | 3 | * - if current style and base style both have property A set, use current style property's value |
|
| 305 | * - if current style has property A set but base style does not, use current style property's value |
||
| 306 | 66 | * - if base style has property A set but current style does not, use base style property's value |
|
| 307 | * |
||
| 308 | * @NOTE: This function returns a new style. |
||
| 309 | * |
||
| 310 | * @param Style $baseStyle |
||
| 311 | * @return Style New style corresponding to the merge of the 2 styles |
||
| 312 | */ |
||
| 313 | public function mergeWith($baseStyle) |
||
| 347 | } |
||
| 348 |