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 | const DEFAULT_BACKGROUND_COLOR = Color::WHITE; |
||
| 18 | |||
| 19 | /** @var int|null Style ID */ |
||
| 20 | protected $id = null; |
||
| 21 | |||
| 22 | /** @var bool Whether the font should be bold */ |
||
| 23 | protected $fontBold = false; |
||
| 24 | /** @var bool Whether the bold property was set */ |
||
| 25 | protected $hasSetFontBold = false; |
||
| 26 | |||
| 27 | /** @var bool Whether the font should be italic */ |
||
| 28 | protected $fontItalic = false; |
||
| 29 | /** @var bool Whether the italic property was set */ |
||
| 30 | protected $hasSetFontItalic = false; |
||
| 31 | |||
| 32 | /** @var bool Whether the font should be underlined */ |
||
| 33 | protected $fontUnderline = false; |
||
| 34 | /** @var bool Whether the underline property was set */ |
||
| 35 | protected $hasSetFontUnderline = false; |
||
| 36 | |||
| 37 | /** @var bool Whether the font should be struck through */ |
||
| 38 | protected $fontStrikethrough = false; |
||
| 39 | /** @var bool Whether the strikethrough property was set */ |
||
| 40 | protected $hasSetFontStrikethrough = false; |
||
| 41 | |||
| 42 | /** @var int Font size */ |
||
| 43 | protected $fontSize = self::DEFAULT_FONT_SIZE; |
||
| 44 | /** @var bool Whether the font size property was set */ |
||
| 45 | protected $hasSetFontSize = false; |
||
| 46 | |||
| 47 | /** @var string Font color */ |
||
| 48 | protected $fontColor = self::DEFAULT_FONT_COLOR; |
||
| 49 | /** @var bool Whether the font color property was set */ |
||
| 50 | protected $hasSetFontColor = false; |
||
| 51 | |||
| 52 | /** @var string Font name */ |
||
| 53 | protected $fontName = self::DEFAULT_FONT_NAME; |
||
| 54 | /** @var bool Whether the font name property was set */ |
||
| 55 | protected $hasSetFontName = false; |
||
| 56 | |||
| 57 | /** @var bool Whether specific font properties should be applied */ |
||
| 58 | protected $shouldApplyFont = false; |
||
| 59 | |||
| 60 | /** @var bool Whether the text should wrap in the cell (useful for long or multi-lines text) */ |
||
| 61 | protected $shouldWrapText = false; |
||
| 62 | /** @var bool Whether the wrap text property was set */ |
||
| 63 | protected $hasSetWrapText = false; |
||
| 64 | |||
| 65 | /** @var string Background color */ |
||
| 66 | protected $backgroundColor = null; |
||
| 67 | 153 | ||
| 68 | /** @var bool */ |
||
| 69 | 153 | protected $hasSetBackgroundColor = false; |
|
| 70 | |||
| 71 | |||
| 72 | /** |
||
| 73 | * @return int|null |
||
| 74 | */ |
||
| 75 | public function getId() |
||
| 76 | 231 | { |
|
| 77 | return $this->id; |
||
| 78 | 231 | } |
|
| 79 | 231 | ||
| 80 | /** |
||
| 81 | * @param int $id |
||
| 82 | * @return Style |
||
| 83 | */ |
||
| 84 | public function setId($id) |
||
| 85 | 114 | { |
|
| 86 | $this->id = $id; |
||
| 87 | 114 | return $this; |
|
| 88 | } |
||
| 89 | |||
| 90 | /** |
||
| 91 | * @return boolean |
||
| 92 | */ |
||
| 93 | 36 | public function isFontBold() |
|
| 94 | { |
||
| 95 | 36 | return $this->fontBold; |
|
| 96 | 36 | } |
|
| 97 | 36 | ||
| 98 | 36 | /** |
|
| 99 | * @return Style |
||
| 100 | */ |
||
| 101 | public function setFontBold() |
||
| 102 | { |
||
| 103 | $this->fontBold = true; |
||
| 104 | 114 | $this->hasSetFontBold = true; |
|
| 105 | $this->shouldApplyFont = true; |
||
| 106 | 114 | return $this; |
|
| 107 | } |
||
| 108 | |||
| 109 | /** |
||
| 110 | * @return boolean |
||
| 111 | */ |
||
| 112 | 12 | public function isFontItalic() |
|
| 113 | { |
||
| 114 | 12 | return $this->fontItalic; |
|
| 115 | 12 | } |
|
| 116 | 12 | ||
| 117 | 12 | /** |
|
| 118 | * @return Style |
||
| 119 | */ |
||
| 120 | public function setFontItalic() |
||
| 121 | { |
||
| 122 | $this->fontItalic = true; |
||
| 123 | 114 | $this->hasSetFontItalic = true; |
|
| 124 | $this->shouldApplyFont = true; |
||
| 125 | 114 | return $this; |
|
| 126 | } |
||
| 127 | |||
| 128 | /** |
||
| 129 | * @return boolean |
||
| 130 | */ |
||
| 131 | 18 | public function isFontUnderline() |
|
| 132 | { |
||
| 133 | 18 | return $this->fontUnderline; |
|
| 134 | 18 | } |
|
| 135 | 18 | ||
| 136 | 18 | /** |
|
| 137 | * @return Style |
||
| 138 | */ |
||
| 139 | public function setFontUnderline() |
||
| 140 | { |
||
| 141 | $this->fontUnderline = true; |
||
| 142 | 114 | $this->hasSetFontUnderline = true; |
|
| 143 | $this->shouldApplyFont = true; |
||
| 144 | 114 | return $this; |
|
| 145 | } |
||
| 146 | |||
| 147 | /** |
||
| 148 | * @return boolean |
||
| 149 | */ |
||
| 150 | 12 | public function isFontStrikethrough() |
|
| 151 | { |
||
| 152 | 12 | return $this->fontStrikethrough; |
|
| 153 | 12 | } |
|
| 154 | 12 | ||
| 155 | 12 | /** |
|
| 156 | * @return Style |
||
| 157 | */ |
||
| 158 | public function setFontStrikethrough() |
||
| 159 | { |
||
| 160 | $this->fontStrikethrough = true; |
||
| 161 | 168 | $this->hasSetFontStrikethrough = true; |
|
| 162 | $this->shouldApplyFont = true; |
||
| 163 | 168 | return $this; |
|
| 164 | } |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @return int |
||
| 168 | */ |
||
| 169 | public function getFontSize() |
||
| 170 | 126 | { |
|
| 171 | return $this->fontSize; |
||
| 172 | 126 | } |
|
| 173 | 126 | ||
| 174 | 126 | /** |
|
| 175 | 126 | * @param int $fontSize Font size, in pixels |
|
| 176 | * @return Style |
||
| 177 | */ |
||
| 178 | public function setFontSize($fontSize) |
||
| 179 | { |
||
| 180 | $this->fontSize = $fontSize; |
||
| 181 | 168 | $this->hasSetFontSize = true; |
|
| 182 | $this->shouldApplyFont = true; |
||
| 183 | 168 | return $this; |
|
| 184 | } |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | public function getFontColor() |
||
| 190 | { |
||
| 191 | return $this->fontColor; |
||
| 192 | 6 | } |
|
| 193 | |||
| 194 | 6 | /** |
|
| 195 | 6 | * Sets the font color. |
|
| 196 | 6 | * |
|
| 197 | 6 | * @param string $fontColor ARGB color (@see Color) |
|
| 198 | * @return Style |
||
| 199 | */ |
||
| 200 | public function setFontColor($fontColor) |
||
| 201 | { |
||
| 202 | $this->fontColor = $fontColor; |
||
| 203 | 210 | $this->hasSetFontColor = true; |
|
| 204 | $this->shouldApplyFont = true; |
||
| 205 | 210 | return $this; |
|
| 206 | } |
||
| 207 | |||
| 208 | /** |
||
| 209 | * @return string |
||
| 210 | */ |
||
| 211 | public function getFontName() |
||
| 212 | 120 | { |
|
| 213 | return $this->fontName; |
||
| 214 | 120 | } |
|
| 215 | 120 | ||
| 216 | 120 | /** |
|
| 217 | 120 | * @param string $fontName Name of the font to use |
|
| 218 | * @return Style |
||
| 219 | */ |
||
| 220 | public function setFontName($fontName) |
||
| 221 | { |
||
| 222 | $this->fontName = $fontName; |
||
| 223 | 186 | $this->hasSetFontName = true; |
|
| 224 | $this->shouldApplyFont = true; |
||
| 225 | 186 | return $this; |
|
| 226 | } |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @return boolean |
||
| 230 | */ |
||
| 231 | 30 | public function shouldWrapText() |
|
| 232 | { |
||
| 233 | 30 | return $this->shouldWrapText; |
|
| 234 | 30 | } |
|
| 235 | 30 | ||
| 236 | /** |
||
| 237 | * @return Style |
||
| 238 | */ |
||
| 239 | public function setShouldWrapText() |
||
| 240 | { |
||
| 241 | 135 | $this->shouldWrapText = true; |
|
| 242 | $this->hasSetWrapText = true; |
||
| 243 | 135 | return $this; |
|
| 244 | } |
||
| 245 | |||
| 246 | /** |
||
| 247 | * @return bool Whether specific font properties should be applied |
||
| 248 | */ |
||
| 249 | public function shouldApplyFont() |
||
| 250 | { |
||
| 251 | return $this->shouldApplyFont; |
||
| 252 | } |
||
| 253 | 231 | ||
| 254 | /** |
||
| 255 | * Sets the background color |
||
| 256 | 231 | * @param $color |
|
| 257 | 231 | */ |
|
| 258 | public function setBackgroundColor($color = self::DEFAULT_BACKGROUND_COLOR) |
||
| 259 | 231 | { |
|
| 260 | $this->hasSetBackgroundColor = true; |
||
| 261 | 231 | $this->backgroundColor = $color; |
|
| 262 | return $this; |
||
| 263 | 231 | } |
|
| 264 | |||
| 265 | /** |
||
| 266 | * @return string |
||
| 267 | */ |
||
| 268 | public function getBackgroundColor() |
||
| 269 | { |
||
| 270 | return $this->backgroundColor; |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * |
||
| 275 | * @return bool Whether the background color should be applied |
||
| 276 | */ |
||
| 277 | 66 | public function shouldApplyBackgroundColor() |
|
| 278 | { |
||
| 279 | 66 | return $this->hasSetBackgroundColor; |
|
| 280 | } |
||
| 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) |
||
| 314 | { |
||
| 315 | $mergedStyle = clone $this; |
||
| 316 | |||
| 317 | if (!$this->hasSetFontBold && $baseStyle->isFontBold()) { |
||
| 318 | $mergedStyle->setFontBold(); |
||
| 319 | } |
||
| 320 | if (!$this->hasSetFontItalic && $baseStyle->isFontItalic()) { |
||
| 321 | $mergedStyle->setFontItalic(); |
||
| 322 | } |
||
| 323 | if (!$this->hasSetFontUnderline && $baseStyle->isFontUnderline()) { |
||
| 347 | } |
||
| 348 |