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 Border */ |
||
70 | private $border; |
||
71 | |||
72 | /** @var bool Whether border properties should be applied */ |
||
73 | private $shouldApplyBorder = false; |
||
74 | |||
75 | /** @var string Background color */ |
||
76 | private $backgroundColor; |
||
77 | |||
78 | /** @var bool */ |
||
79 | private $hasSetBackgroundColor = false; |
||
80 | |||
81 | /** @var string Format */ |
||
82 | private $format; |
||
83 | |||
84 | /** @var bool */ |
||
85 | private $hasSetFormat = false; |
||
86 | |||
87 | /** |
||
88 | * @return int|null |
||
89 | */ |
||
90 | 89 | public function getId() |
|
94 | |||
95 | /** |
||
96 | * @param int $id |
||
97 | * @return Style |
||
98 | */ |
||
99 | 89 | public function setId($id) |
|
105 | |||
106 | /** |
||
107 | * @return Border |
||
108 | */ |
||
109 | 76 | public function getBorder() |
|
113 | |||
114 | /** |
||
115 | * @param Border $border |
||
116 | * @return Style |
||
117 | */ |
||
118 | 8 | public function setBorder(Border $border) |
|
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | 88 | public function shouldApplyBorder() |
|
133 | |||
134 | /** |
||
135 | * @return bool |
||
136 | */ |
||
137 | 78 | public function isFontBold() |
|
141 | |||
142 | /** |
||
143 | * @return Style |
||
144 | */ |
||
145 | 20 | public function setFontBold() |
|
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | */ |
||
157 | 75 | public function hasSetFontBold() |
|
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | 78 | public function isFontItalic() |
|
169 | |||
170 | /** |
||
171 | * @return Style |
||
172 | */ |
||
173 | 7 | public function setFontItalic() |
|
181 | |||
182 | /** |
||
183 | * @return bool |
||
184 | */ |
||
185 | 75 | public function hasSetFontItalic() |
|
189 | |||
190 | /** |
||
191 | * @return bool |
||
192 | */ |
||
193 | 78 | public function isFontUnderline() |
|
197 | |||
198 | /** |
||
199 | * @return Style |
||
200 | */ |
||
201 | 7 | public function setFontUnderline() |
|
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | 75 | public function hasSetFontUnderline() |
|
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | 78 | public function isFontStrikethrough() |
|
225 | |||
226 | /** |
||
227 | * @return Style |
||
228 | */ |
||
229 | 4 | public function setFontStrikethrough() |
|
237 | |||
238 | /** |
||
239 | * @return bool |
||
240 | */ |
||
241 | 75 | public function hasSetFontStrikethrough() |
|
245 | |||
246 | /** |
||
247 | * @return int |
||
248 | */ |
||
249 | 80 | public function getFontSize() |
|
253 | |||
254 | /** |
||
255 | * @param int $fontSize Font size, in pixels |
||
256 | * @return Style |
||
257 | */ |
||
258 | 53 | public function setFontSize($fontSize) |
|
266 | |||
267 | /** |
||
268 | * @return bool |
||
269 | */ |
||
270 | 75 | public function hasSetFontSize() |
|
274 | |||
275 | /** |
||
276 | * @return string |
||
277 | */ |
||
278 | 80 | public function getFontColor() |
|
282 | |||
283 | /** |
||
284 | * Sets the font color. |
||
285 | * |
||
286 | * @param string $fontColor ARGB color (@see Color) |
||
287 | * @return Style |
||
288 | */ |
||
289 | 3 | public function setFontColor($fontColor) |
|
297 | |||
298 | /** |
||
299 | * @return bool |
||
300 | */ |
||
301 | 75 | public function hasSetFontColor() |
|
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | 84 | public function getFontName() |
|
313 | |||
314 | /** |
||
315 | * @param string $fontName Name of the font to use |
||
316 | * @return Style |
||
317 | */ |
||
318 | 51 | public function setFontName($fontName) |
|
326 | |||
327 | /** |
||
328 | * @return bool |
||
329 | */ |
||
330 | 75 | public function hasSetFontName() |
|
334 | |||
335 | /** |
||
336 | * @return string |
||
337 | */ |
||
338 | 2 | public function getCellAlignment() |
|
342 | |||
343 | /** |
||
344 | * @param string $cellAlignment The cell alignment |
||
345 | * |
||
346 | * @return Style |
||
347 | */ |
||
348 | 3 | public function setCellAlignment($cellAlignment) |
|
356 | |||
357 | /** |
||
358 | * @return bool |
||
359 | */ |
||
360 | 75 | public function hasSetCellAlignment() |
|
364 | |||
365 | /** |
||
366 | * @return bool Whether specific cell alignment should be applied |
||
367 | */ |
||
368 | 81 | public function shouldApplyCellAlignment() |
|
372 | |||
373 | /** |
||
374 | * @return bool |
||
375 | */ |
||
376 | 82 | public function shouldWrapText() |
|
380 | |||
381 | /** |
||
382 | * @param bool $shouldWrap Should the text be wrapped |
||
383 | * @return Style |
||
384 | */ |
||
385 | 8 | public function setShouldWrapText($shouldWrap = true) |
|
392 | |||
393 | /** |
||
394 | * @return bool |
||
395 | */ |
||
396 | 77 | public function hasSetWrapText() |
|
400 | |||
401 | /** |
||
402 | * @return bool Whether specific font properties should be applied |
||
403 | */ |
||
404 | 71 | public function shouldApplyFont() |
|
408 | |||
409 | /** |
||
410 | * Sets the background color |
||
411 | * @param string $color ARGB color (@see Color) |
||
412 | * @return Style |
||
413 | */ |
||
414 | 7 | public function setBackgroundColor($color) |
|
421 | |||
422 | /** |
||
423 | * @return string |
||
424 | */ |
||
425 | 47 | public function getBackgroundColor() |
|
429 | |||
430 | /** |
||
431 | * @return bool Whether the background color should be applied |
||
432 | */ |
||
433 | 77 | public function shouldApplyBackgroundColor() |
|
437 | |||
438 | /** |
||
439 | * Sets format |
||
440 | * @param string $format |
||
441 | * @return Style |
||
442 | */ |
||
443 | 3 | public function setFormat($format) |
|
450 | |||
451 | /** |
||
452 | * @return string |
||
453 | */ |
||
454 | 85 | public function getFormat() |
|
458 | |||
459 | /** |
||
460 | * @return bool Whether format should be applied |
||
461 | */ |
||
462 | 75 | public function shouldApplyFormat() |
|
466 | } |
||
467 |