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 | /** @var bool */ |
||
88 | private $isRegistered = false; |
||
89 | |||
90 | /** |
||
91 | * @return int|null |
||
92 | */ |
||
93 | 92 | public function getId() |
|
97 | |||
98 | /** |
||
99 | * @param int $id |
||
100 | * @return Style |
||
101 | */ |
||
102 | 92 | public function setId($id) |
|
108 | |||
109 | /** |
||
110 | * @return Border |
||
111 | */ |
||
112 | 78 | public function getBorder() |
|
116 | |||
117 | /** |
||
118 | * @param Border $border |
||
119 | * @return Style |
||
120 | */ |
||
121 | 8 | public function setBorder(Border $border) |
|
128 | |||
129 | /** |
||
130 | * @return bool |
||
131 | */ |
||
132 | 90 | public function shouldApplyBorder() |
|
136 | |||
137 | /** |
||
138 | * @return bool |
||
139 | */ |
||
140 | 80 | public function isFontBold() |
|
144 | |||
145 | /** |
||
146 | * @return Style |
||
147 | */ |
||
148 | 20 | public function setFontBold() |
|
156 | |||
157 | /** |
||
158 | * @return bool |
||
159 | */ |
||
160 | 77 | public function hasSetFontBold() |
|
164 | |||
165 | /** |
||
166 | * @return bool |
||
167 | */ |
||
168 | 80 | public function isFontItalic() |
|
172 | |||
173 | /** |
||
174 | * @return Style |
||
175 | */ |
||
176 | 7 | public function setFontItalic() |
|
184 | |||
185 | /** |
||
186 | * @return bool |
||
187 | */ |
||
188 | 77 | public function hasSetFontItalic() |
|
192 | |||
193 | /** |
||
194 | * @return bool |
||
195 | */ |
||
196 | 80 | public function isFontUnderline() |
|
200 | |||
201 | /** |
||
202 | * @return Style |
||
203 | */ |
||
204 | 7 | public function setFontUnderline() |
|
212 | |||
213 | /** |
||
214 | * @return bool |
||
215 | */ |
||
216 | 77 | public function hasSetFontUnderline() |
|
220 | |||
221 | /** |
||
222 | * @return bool |
||
223 | */ |
||
224 | 80 | public function isFontStrikethrough() |
|
228 | |||
229 | /** |
||
230 | * @return Style |
||
231 | */ |
||
232 | 4 | public function setFontStrikethrough() |
|
240 | |||
241 | /** |
||
242 | * @return bool |
||
243 | */ |
||
244 | 77 | public function hasSetFontStrikethrough() |
|
248 | |||
249 | /** |
||
250 | * @return int |
||
251 | */ |
||
252 | 82 | public function getFontSize() |
|
256 | |||
257 | /** |
||
258 | * @param int $fontSize Font size, in pixels |
||
259 | * @return Style |
||
260 | */ |
||
261 | 54 | public function setFontSize($fontSize) |
|
269 | |||
270 | /** |
||
271 | * @return bool |
||
272 | */ |
||
273 | 77 | public function hasSetFontSize() |
|
277 | |||
278 | /** |
||
279 | * @return string |
||
280 | */ |
||
281 | 82 | public function getFontColor() |
|
285 | |||
286 | /** |
||
287 | * Sets the font color. |
||
288 | * |
||
289 | * @param string $fontColor ARGB color (@see Color) |
||
290 | * @return Style |
||
291 | */ |
||
292 | 3 | public function setFontColor($fontColor) |
|
300 | |||
301 | /** |
||
302 | * @return bool |
||
303 | */ |
||
304 | 77 | public function hasSetFontColor() |
|
308 | |||
309 | /** |
||
310 | * @return string |
||
311 | */ |
||
312 | 86 | public function getFontName() |
|
316 | |||
317 | /** |
||
318 | * @param string $fontName Name of the font to use |
||
319 | * @return Style |
||
320 | */ |
||
321 | 52 | public function setFontName($fontName) |
|
329 | |||
330 | /** |
||
331 | * @return bool |
||
332 | */ |
||
333 | 77 | public function hasSetFontName() |
|
337 | |||
338 | /** |
||
339 | * @return string |
||
340 | */ |
||
341 | 2 | public function getCellAlignment() |
|
345 | |||
346 | /** |
||
347 | * @param string $cellAlignment The cell alignment |
||
348 | * |
||
349 | * @return Style |
||
350 | */ |
||
351 | 3 | public function setCellAlignment($cellAlignment) |
|
359 | |||
360 | /** |
||
361 | * @return bool |
||
362 | */ |
||
363 | 77 | public function hasSetCellAlignment() |
|
367 | |||
368 | /** |
||
369 | * @return bool Whether specific cell alignment should be applied |
||
370 | */ |
||
371 | 83 | public function shouldApplyCellAlignment() |
|
375 | |||
376 | /** |
||
377 | * @return bool |
||
378 | */ |
||
379 | 85 | public function shouldWrapText() |
|
383 | |||
384 | /** |
||
385 | * @param bool $shouldWrap Should the text be wrapped |
||
386 | * @return Style |
||
387 | */ |
||
388 | 8 | public function setShouldWrapText($shouldWrap = true) |
|
395 | |||
396 | /** |
||
397 | * @return bool |
||
398 | */ |
||
399 | 80 | public function hasSetWrapText() |
|
403 | |||
404 | /** |
||
405 | * @return bool Whether specific font properties should be applied |
||
406 | */ |
||
407 | 73 | public function shouldApplyFont() |
|
411 | |||
412 | /** |
||
413 | * Sets the background color |
||
414 | * @param string $color ARGB color (@see Color) |
||
415 | * @return Style |
||
416 | */ |
||
417 | 7 | public function setBackgroundColor($color) |
|
424 | |||
425 | /** |
||
426 | * @return string |
||
427 | */ |
||
428 | 48 | public function getBackgroundColor() |
|
432 | |||
433 | /** |
||
434 | * @return bool Whether the background color should be applied |
||
435 | */ |
||
436 | 79 | public function shouldApplyBackgroundColor() |
|
440 | |||
441 | /** |
||
442 | * Sets format |
||
443 | * @param string $format |
||
444 | * @return Style |
||
445 | */ |
||
446 | 3 | public function setFormat($format) |
|
453 | |||
454 | /** |
||
455 | * @return string |
||
456 | */ |
||
457 | 87 | public function getFormat() |
|
461 | |||
462 | /** |
||
463 | * @return bool Whether format should be applied |
||
464 | */ |
||
465 | 76 | public function shouldApplyFormat() |
|
469 | |||
470 | 92 | public function setRegistered(bool $isRegistered = true) : void |
|
474 | |||
475 | /** |
||
476 | * @return bool |
||
477 | */ |
||
478 | 84 | public function isRegistered() : bool |
|
482 | |||
483 | 92 | public function register(int $id) : void |
|
488 | } |
||
489 |