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 | /** @var string Custom number format */ |
||
| 65 | protected $numberFormat = ''; |
||
| 66 | /** @var boolean Whether specific number format has been set */ |
||
| 67 | protected $hasSetNumberFormat = false; |
||
| 68 | /** @var integer Holds the number format id */ |
||
| 69 | protected $numberFormatId = 0; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @return int|null |
||
| 73 | */ |
||
| 74 | 153 | public function getId() |
|
| 75 | { |
||
| 76 | 153 | return $this->id; |
|
| 77 | } |
||
| 78 | |||
| 79 | /** |
||
| 80 | * @param int $id |
||
| 81 | * @return Style |
||
| 82 | */ |
||
| 83 | 231 | public function setId($id) |
|
| 84 | { |
||
| 85 | 231 | $this->id = $id; |
|
| 86 | 231 | return $this; |
|
| 87 | } |
||
| 88 | |||
| 89 | /** |
||
| 90 | * @return boolean |
||
| 91 | */ |
||
| 92 | 114 | public function isFontBold() |
|
| 96 | |||
| 97 | /** |
||
| 98 | * @return Style |
||
| 99 | */ |
||
| 100 | 36 | public function setFontBold() |
|
| 101 | { |
||
| 102 | 36 | $this->fontBold = true; |
|
| 103 | 36 | $this->hasSetFontBold = true; |
|
| 104 | 36 | $this->shouldApplyFont = true; |
|
| 105 | 36 | return $this; |
|
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return boolean |
||
| 110 | */ |
||
| 111 | 114 | public function isFontItalic() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * @return Style |
||
| 118 | */ |
||
| 119 | 12 | public function setFontItalic() |
|
| 120 | { |
||
| 121 | 12 | $this->fontItalic = true; |
|
| 122 | 12 | $this->hasSetFontItalic = true; |
|
| 123 | 12 | $this->shouldApplyFont = true; |
|
| 124 | 12 | return $this; |
|
| 125 | } |
||
| 126 | |||
| 127 | /** |
||
| 128 | * @return boolean |
||
| 129 | */ |
||
| 130 | 114 | public function isFontUnderline() |
|
| 134 | |||
| 135 | /** |
||
| 136 | * @return Style |
||
| 137 | */ |
||
| 138 | 18 | public function setFontUnderline() |
|
| 139 | { |
||
| 140 | 18 | $this->fontUnderline = true; |
|
| 141 | 18 | $this->hasSetFontUnderline = true; |
|
| 142 | 18 | $this->shouldApplyFont = true; |
|
| 143 | 18 | return $this; |
|
| 144 | } |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @return boolean |
||
| 148 | */ |
||
| 149 | 114 | public function isFontStrikethrough() |
|
| 153 | |||
| 154 | /** |
||
| 155 | * @return Style |
||
| 156 | */ |
||
| 157 | 12 | public function setFontStrikethrough() |
|
| 158 | { |
||
| 159 | 12 | $this->fontStrikethrough = true; |
|
| 160 | 12 | $this->hasSetFontStrikethrough = true; |
|
| 161 | 12 | $this->shouldApplyFont = true; |
|
| 162 | 12 | return $this; |
|
| 163 | } |
||
| 164 | |||
| 165 | /** |
||
| 166 | * @return int |
||
| 167 | */ |
||
| 168 | 168 | public function getFontSize() |
|
| 169 | { |
||
| 170 | 168 | return $this->fontSize; |
|
| 171 | } |
||
| 172 | |||
| 173 | /** |
||
| 174 | * @param int $fontSize Font size, in pixels |
||
| 175 | * @return Style |
||
| 176 | */ |
||
| 177 | 126 | public function setFontSize($fontSize) |
|
| 178 | { |
||
| 179 | 126 | $this->fontSize = $fontSize; |
|
| 180 | 126 | $this->hasSetFontSize = true; |
|
| 181 | 126 | $this->shouldApplyFont = true; |
|
| 182 | 126 | return $this; |
|
| 183 | } |
||
| 184 | |||
| 185 | /** |
||
| 186 | * @return string |
||
| 187 | */ |
||
| 188 | 168 | public function getFontColor() |
|
| 189 | { |
||
| 190 | 168 | return $this->fontColor; |
|
| 191 | } |
||
| 192 | |||
| 193 | /** |
||
| 194 | * Sets the font color. |
||
| 195 | * |
||
| 196 | * @param string $fontColor ARGB color (@see Color) |
||
| 197 | * @return Style |
||
| 198 | */ |
||
| 199 | 6 | public function setFontColor($fontColor) |
|
| 200 | { |
||
| 201 | 6 | $this->fontColor = $fontColor; |
|
| 202 | 6 | $this->hasSetFontColor = true; |
|
| 203 | 6 | $this->shouldApplyFont = true; |
|
| 204 | 6 | return $this; |
|
| 205 | } |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @return string |
||
| 209 | */ |
||
| 210 | 210 | public function getFontName() |
|
| 211 | { |
||
| 212 | 210 | return $this->fontName; |
|
| 213 | } |
||
| 214 | |||
| 215 | /** |
||
| 216 | * @param string $fontName Name of the font to use |
||
| 217 | * @return Style |
||
| 218 | */ |
||
| 219 | 120 | public function setFontName($fontName) |
|
| 220 | { |
||
| 221 | 120 | $this->fontName = $fontName; |
|
| 222 | 120 | $this->hasSetFontName = true; |
|
| 223 | 120 | $this->shouldApplyFont = true; |
|
| 224 | 120 | return $this; |
|
| 225 | } |
||
| 226 | |||
| 227 | /** |
||
| 228 | * @return boolean |
||
| 229 | */ |
||
| 230 | 186 | public function shouldWrapText() |
|
| 234 | |||
| 235 | /** |
||
| 236 | * @return Style |
||
| 237 | */ |
||
| 238 | 30 | public function setShouldWrapText() |
|
| 239 | { |
||
| 240 | 30 | $this->shouldWrapText = true; |
|
| 241 | 30 | $this->hasSetWrapText = true; |
|
| 242 | 30 | return $this; |
|
| 243 | } |
||
| 244 | |||
| 245 | /** |
||
| 246 | * @return bool Whether specific font properties should be applied |
||
| 247 | */ |
||
| 248 | 135 | public function shouldApplyFont() |
|
| 252 | |||
| 253 | public function setNumberFormat($format) |
||
| 259 | |||
| 260 | public function setNumberFormatId($id) |
||
| 264 | |||
| 265 | |||
| 266 | 63 | public function shouldApplyNumberFormat() |
|
| 270 | |||
| 271 | 63 | public function getNumberFormatId() |
|
| 275 | |||
| 276 | public function getNumberFormat() |
||
| 280 | |||
| 281 | |||
| 282 | /** |
||
| 283 | * Serializes the style for future comparison with other styles. |
||
| 284 | * The ID is excluded from the comparison, as we only care about |
||
| 285 | * actual style properties. |
||
| 286 | * |
||
| 287 | * @return string The serialized style |
||
| 288 | */ |
||
| 289 | 231 | public function serialize() |
|
| 301 | |||
| 302 | /** |
||
| 303 | * Merges the current style with the given style, using the given style as a base. This means that: |
||
| 304 | * - 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 | * - 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 | 66 | public function mergeWith($baseStyle) |
|
| 344 | } |
||
| 345 |