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 | protected $border = null; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @var bool Whether border properties should be applied |
||
| 71 | */ |
||
| 72 | protected $shouldApplyBorder = false; |
||
| 73 | |||
| 74 | /** @var string Background color */ |
||
| 75 | protected $backgroundColor = null; |
||
| 76 | |||
| 77 | 168 | /** @var bool */ |
|
| 78 | protected $hasSetBackgroundColor = false; |
||
| 79 | 168 | ||
| 80 | |||
| 81 | /** |
||
| 82 | * @return int|null |
||
| 83 | */ |
||
| 84 | public function getId() |
||
| 85 | { |
||
| 86 | 246 | return $this->id; |
|
| 87 | } |
||
| 88 | 246 | ||
| 89 | 246 | /** |
|
| 90 | * @param int $id |
||
| 91 | * @return Style |
||
| 92 | */ |
||
| 93 | public function setId($id) |
||
| 94 | { |
||
| 95 | 129 | $this->id = $id; |
|
| 96 | return $this; |
||
| 97 | 129 | } |
|
| 98 | |||
| 99 | /** |
||
| 100 | * @return Border |
||
| 101 | */ |
||
| 102 | public function getBorder() |
||
| 106 | 15 | ||
| 107 | 15 | /** |
|
| 108 | * @param Border $border |
||
| 109 | */ |
||
| 110 | public function setBorder(Border $border) |
||
| 111 | { |
||
| 112 | $this->shouldApplyBorder = true; |
||
| 113 | 189 | $this->border = $border; |
|
| 114 | return $this; |
||
| 115 | 189 | } |
|
| 116 | |||
| 117 | /** |
||
| 118 | * @return boolean |
||
| 119 | */ |
||
| 120 | public function shouldApplyBorder() |
||
| 124 | |||
| 125 | /** |
||
| 126 | * @return boolean |
||
| 127 | */ |
||
| 128 | public function isFontBold() |
||
| 132 | 36 | ||
| 133 | 36 | /** |
|
| 134 | 36 | * @return Style |
|
| 135 | */ |
||
| 136 | public function setFontBold() |
||
| 137 | { |
||
| 138 | $this->fontBold = true; |
||
| 139 | $this->hasSetFontBold = true; |
||
| 140 | 129 | $this->shouldApplyFont = true; |
|
| 141 | return $this; |
||
| 142 | 129 | } |
|
| 143 | |||
| 144 | /** |
||
| 145 | * @return boolean |
||
| 146 | */ |
||
| 147 | public function isFontItalic() |
||
| 151 | 12 | ||
| 152 | 12 | /** |
|
| 153 | 12 | * @return Style |
|
| 154 | */ |
||
| 155 | public function setFontItalic() |
||
| 156 | { |
||
| 157 | $this->fontItalic = true; |
||
| 158 | $this->hasSetFontItalic = true; |
||
| 159 | 129 | $this->shouldApplyFont = true; |
|
| 160 | return $this; |
||
| 161 | 129 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * @return boolean |
||
| 165 | */ |
||
| 166 | public function isFontUnderline() |
||
| 170 | 18 | ||
| 171 | 18 | /** |
|
| 172 | 18 | * @return Style |
|
| 173 | */ |
||
| 174 | public function setFontUnderline() |
||
| 175 | { |
||
| 176 | $this->fontUnderline = true; |
||
| 177 | $this->hasSetFontUnderline = true; |
||
| 178 | 129 | $this->shouldApplyFont = true; |
|
| 179 | return $this; |
||
| 180 | 129 | } |
|
| 181 | |||
| 182 | /** |
||
| 183 | * @return boolean |
||
| 184 | */ |
||
| 185 | public function isFontStrikethrough() |
||
| 189 | 12 | ||
| 190 | 12 | /** |
|
| 191 | 12 | * @return Style |
|
| 192 | */ |
||
| 193 | public function setFontStrikethrough() |
||
| 194 | { |
||
| 195 | $this->fontStrikethrough = true; |
||
| 196 | $this->hasSetFontStrikethrough = true; |
||
| 197 | 186 | $this->shouldApplyFont = true; |
|
| 198 | return $this; |
||
| 199 | 186 | } |
|
| 200 | |||
| 201 | /** |
||
| 202 | * @return int |
||
| 203 | */ |
||
| 204 | public function getFontSize() |
||
| 205 | { |
||
| 206 | 135 | return $this->fontSize; |
|
| 207 | } |
||
| 208 | 135 | ||
| 209 | 135 | /** |
|
| 210 | 135 | * @param int $fontSize Font size, in pixels |
|
| 211 | 135 | * @return Style |
|
| 212 | */ |
||
| 213 | public function setFontSize($fontSize) |
||
| 214 | { |
||
| 215 | $this->fontSize = $fontSize; |
||
| 216 | $this->hasSetFontSize = true; |
||
| 217 | 186 | $this->shouldApplyFont = true; |
|
| 218 | return $this; |
||
| 219 | 186 | } |
|
| 220 | |||
| 221 | /** |
||
| 222 | * @return string |
||
| 223 | */ |
||
| 224 | public function getFontColor() |
||
| 225 | { |
||
| 226 | return $this->fontColor; |
||
| 227 | } |
||
| 228 | 6 | ||
| 229 | /** |
||
| 230 | 6 | * Sets the font color. |
|
| 231 | 6 | * |
|
| 232 | 6 | * @param string $fontColor ARGB color (@see Color) |
|
| 233 | 6 | * @return Style |
|
| 234 | */ |
||
| 235 | public function setFontColor($fontColor) |
||
| 236 | { |
||
| 237 | $this->fontColor = $fontColor; |
||
| 238 | $this->hasSetFontColor = true; |
||
| 239 | 228 | $this->shouldApplyFont = true; |
|
| 240 | return $this; |
||
| 241 | 228 | } |
|
| 242 | |||
| 243 | /** |
||
| 244 | * @return string |
||
| 245 | */ |
||
| 246 | public function getFontName() |
||
| 247 | { |
||
| 248 | 129 | return $this->fontName; |
|
| 249 | } |
||
| 250 | 129 | ||
| 251 | 129 | /** |
|
| 252 | 129 | * @param string $fontName Name of the font to use |
|
| 253 | 129 | * @return Style |
|
| 254 | */ |
||
| 255 | public function setFontName($fontName) |
||
| 256 | { |
||
| 257 | $this->fontName = $fontName; |
||
| 258 | $this->hasSetFontName = true; |
||
| 259 | 204 | $this->shouldApplyFont = true; |
|
| 260 | return $this; |
||
| 261 | 204 | } |
|
| 262 | |||
| 263 | /** |
||
| 264 | * @return boolean |
||
| 265 | */ |
||
| 266 | public function shouldWrapText() |
||
| 270 | 30 | ||
| 271 | 30 | /** |
|
| 272 | * @return Style |
||
| 273 | */ |
||
| 274 | public function setShouldWrapText() |
||
| 275 | { |
||
| 276 | $this->shouldWrapText = true; |
||
| 277 | 150 | $this->hasSetWrapText = true; |
|
| 278 | return $this; |
||
| 279 | 150 | } |
|
| 280 | |||
| 281 | /** |
||
| 282 | * @return bool Whether specific font properties should be applied |
||
| 283 | */ |
||
| 284 | public function shouldApplyFont() |
||
| 285 | { |
||
| 286 | return $this->shouldApplyFont; |
||
| 287 | } |
||
| 288 | |||
| 289 | 246 | /** |
|
| 290 | * Sets the background color |
||
| 291 | * @param $color ARGB color (@see Color) |
||
| 292 | 246 | * @return Style |
|
| 293 | 246 | */ |
|
| 294 | public function setBackgroundColor($color) |
||
| 295 | 246 | { |
|
| 296 | $this->hasSetBackgroundColor = true; |
||
| 297 | 246 | $this->backgroundColor = $color; |
|
| 298 | return $this; |
||
| 299 | 246 | } |
|
| 300 | |||
| 301 | /** |
||
| 302 | * @return string |
||
| 303 | */ |
||
| 304 | public function getBackgroundColor() |
||
| 305 | { |
||
| 306 | return $this->backgroundColor; |
||
| 307 | } |
||
| 308 | |||
| 309 | /** |
||
| 310 | * |
||
| 311 | * @return bool Whether the background color should be applied |
||
| 312 | */ |
||
| 313 | 78 | public function shouldApplyBackgroundColor() |
|
| 317 | 78 | ||
| 318 | 3 | /** |
|
| 319 | 3 | * Serializes the style for future comparison with other styles. |
|
| 320 | 78 | * The ID is excluded from the comparison, as we only care about |
|
| 321 | 3 | * actual style properties. |
|
| 322 | 3 | * |
|
| 323 | 78 | * @return string The serialized style |
|
| 324 | 3 | */ |
|
| 325 | 3 | public function serialize() |
|
| 326 | 78 | { |
|
| 327 | 3 | // In order to be able to properly compare style, set static ID value |
|
| 328 | 3 | $currentId = $this->id; |
|
| 329 | 78 | $this->setId(0); |
|
| 330 | 30 | ||
| 331 | 30 | $serializedStyle = serialize($this); |
|
| 332 | 78 | ||
| 333 | $this->setId($currentId); |
||
| 334 | |||
| 337 | 27 | ||
| 338 | 78 | /** |
|
| 339 | 3 | * Merges the current style with the given style, using the given style as a base. This means that: |
|
| 340 | 3 | * - if current style and base style both have property A set, use current style property's value |
|
| 341 | 78 | * - if current style has property A set but base style does not, use current style property's value |
|
| 342 | 3 | * - if base style has property A set but current style does not, use base style property's value |
|
| 343 | 3 | * |
|
| 344 | * @NOTE: This function returns a new style. |
||
| 345 | 78 | * |
|
| 346 | * @param Style $baseStyle |
||
| 347 | * @return Style New style corresponding to the merge of the 2 styles |
||
| 348 | */ |
||
| 349 | public function mergeWith($baseStyle) |
||
| 386 | } |
||
| 387 |