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 | 87 | ||
84 | /** @var bool */ |
||
85 | 87 | private $hasSetFormat = false; |
|
86 | |||
87 | /** |
||
88 | * @return int|null |
||
89 | */ |
||
90 | public function getId() |
||
91 | { |
||
92 | 87 | return $this->id; |
|
93 | } |
||
94 | 87 | ||
95 | /** |
||
96 | 87 | * @param int $id |
|
97 | * @return Style |
||
98 | */ |
||
99 | public function setId($id) |
||
100 | { |
||
101 | $this->id = $id; |
||
102 | 75 | ||
103 | return $this; |
||
104 | 75 | } |
|
105 | |||
106 | /** |
||
107 | * @return Border |
||
108 | */ |
||
109 | public function getBorder() |
||
110 | { |
||
111 | 8 | return $this->border; |
|
112 | } |
||
113 | 8 | ||
114 | 8 | /** |
|
115 | * @param Border $border |
||
116 | 8 | * @return Style |
|
117 | */ |
||
118 | public function setBorder(Border $border) |
||
119 | { |
||
120 | $this->shouldApplyBorder = true; |
||
121 | $this->border = $border; |
||
122 | 87 | ||
123 | return $this; |
||
124 | 87 | } |
|
125 | |||
126 | /** |
||
127 | * @return bool |
||
128 | */ |
||
129 | public function shouldApplyBorder() |
||
133 | |||
134 | /** |
||
135 | * @return bool |
||
136 | */ |
||
137 | public function isFontBold() |
||
141 | 21 | ||
142 | 21 | /** |
|
143 | * @return Style |
||
144 | 21 | */ |
|
145 | public function setFontBold() |
||
146 | { |
||
147 | $this->fontBold = true; |
||
148 | $this->hasSetFontBold = true; |
||
149 | $this->shouldApplyFont = true; |
||
150 | 74 | ||
151 | return $this; |
||
152 | 74 | } |
|
153 | |||
154 | /** |
||
155 | * @return bool |
||
156 | */ |
||
157 | public function hasSetFontBold() |
||
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | public function isFontItalic() |
||
169 | 7 | ||
170 | 7 | /** |
|
171 | * @return Style |
||
172 | 7 | */ |
|
173 | public function setFontItalic() |
||
174 | { |
||
175 | $this->fontItalic = true; |
||
176 | $this->hasSetFontItalic = true; |
||
177 | $this->shouldApplyFont = true; |
||
178 | 74 | ||
179 | return $this; |
||
180 | 74 | } |
|
181 | |||
182 | /** |
||
183 | * @return bool |
||
184 | */ |
||
185 | public function hasSetFontItalic() |
||
189 | |||
190 | /** |
||
191 | * @return bool |
||
192 | */ |
||
193 | public function isFontUnderline() |
||
197 | 7 | ||
198 | 7 | /** |
|
199 | * @return Style |
||
200 | 7 | */ |
|
201 | public function setFontUnderline() |
||
202 | { |
||
203 | $this->fontUnderline = true; |
||
204 | $this->hasSetFontUnderline = true; |
||
205 | $this->shouldApplyFont = true; |
||
206 | 74 | ||
207 | return $this; |
||
208 | 74 | } |
|
209 | |||
210 | /** |
||
211 | * @return bool |
||
212 | */ |
||
213 | public function hasSetFontUnderline() |
||
217 | |||
218 | /** |
||
219 | * @return bool |
||
220 | */ |
||
221 | public function isFontStrikethrough() |
||
225 | 4 | ||
226 | 4 | /** |
|
227 | * @return Style |
||
228 | 4 | */ |
|
229 | public function setFontStrikethrough() |
||
230 | { |
||
231 | $this->fontStrikethrough = true; |
||
232 | $this->hasSetFontStrikethrough = true; |
||
233 | $this->shouldApplyFont = true; |
||
234 | 74 | ||
235 | return $this; |
||
236 | 74 | } |
|
237 | |||
238 | /** |
||
239 | * @return bool |
||
240 | */ |
||
241 | public function hasSetFontStrikethrough() |
||
245 | |||
246 | /** |
||
247 | * @return int |
||
248 | */ |
||
249 | public function getFontSize() |
||
250 | { |
||
251 | 52 | return $this->fontSize; |
|
252 | } |
||
253 | 52 | ||
254 | 52 | /** |
|
255 | 52 | * @param int $fontSize Font size, in pixels |
|
256 | * @return Style |
||
257 | 52 | */ |
|
258 | public function setFontSize($fontSize) |
||
259 | { |
||
260 | $this->fontSize = $fontSize; |
||
261 | $this->hasSetFontSize = true; |
||
262 | $this->shouldApplyFont = true; |
||
263 | 74 | ||
264 | return $this; |
||
265 | 74 | } |
|
266 | |||
267 | /** |
||
268 | * @return bool |
||
269 | */ |
||
270 | public function hasSetFontSize() |
||
274 | |||
275 | /** |
||
276 | * @return string |
||
277 | */ |
||
278 | public function getFontColor() |
||
279 | { |
||
280 | return $this->fontColor; |
||
281 | } |
||
282 | 3 | ||
283 | /** |
||
284 | 3 | * Sets the font color. |
|
285 | 3 | * |
|
286 | 3 | * @param string $fontColor ARGB color (@see Color) |
|
287 | * @return Style |
||
288 | 3 | */ |
|
289 | public function setFontColor($fontColor) |
||
290 | { |
||
291 | $this->fontColor = $fontColor; |
||
292 | $this->hasSetFontColor = true; |
||
293 | $this->shouldApplyFont = true; |
||
294 | 74 | ||
295 | return $this; |
||
296 | 74 | } |
|
297 | |||
298 | /** |
||
299 | * @return bool |
||
300 | */ |
||
301 | public function hasSetFontColor() |
||
305 | |||
306 | /** |
||
307 | * @return string |
||
308 | */ |
||
309 | public function getFontName() |
||
310 | { |
||
311 | 50 | return $this->fontName; |
|
312 | } |
||
313 | 50 | ||
314 | 50 | /** |
|
315 | 50 | * @param string $fontName Name of the font to use |
|
316 | * @return Style |
||
317 | 50 | */ |
|
318 | public function setFontName($fontName) |
||
319 | { |
||
320 | $this->fontName = $fontName; |
||
321 | $this->hasSetFontName = true; |
||
322 | $this->shouldApplyFont = true; |
||
323 | 74 | ||
324 | return $this; |
||
325 | 74 | } |
|
326 | |||
327 | /** |
||
328 | * @return bool |
||
329 | */ |
||
330 | public function hasSetFontName() |
||
334 | |||
335 | /** |
||
336 | * @return string |
||
337 | */ |
||
338 | public function getCellAlignment() |
||
339 | { |
||
340 | 8 | return $this->cellAlignment; |
|
341 | } |
||
342 | 8 | ||
343 | 8 | /** |
|
344 | * @param string $cellAlignment The cell alignment |
||
345 | 8 | * |
|
346 | * @return Style |
||
347 | */ |
||
348 | public function setCellAlignment($cellAlignment) |
||
349 | { |
||
350 | $this->cellAlignment = $cellAlignment; |
||
351 | 76 | $this->hasSetCellAlignment = true; |
|
352 | $this->shouldApplyCellAlignment = true; |
||
353 | 76 | ||
354 | return $this; |
||
355 | } |
||
356 | |||
357 | /** |
||
358 | * @return bool |
||
359 | 69 | */ |
|
360 | public function hasSetCellAlignment() |
||
361 | 69 | { |
|
362 | return $this->hasSetCellAlignment; |
||
363 | } |
||
364 | |||
365 | /** |
||
366 | * @return bool Whether specific cell alignment should be applied |
||
367 | */ |
||
368 | public function shouldApplyCellAlignment() |
||
372 | 7 | ||
373 | /** |
||
374 | 7 | * @return bool |
|
375 | */ |
||
376 | public function shouldWrapText() |
||
377 | { |
||
378 | return $this->shouldWrapText; |
||
379 | } |
||
380 | 46 | ||
381 | /** |
||
382 | 46 | * @param bool $shouldWrap Should the text be wrapped |
|
383 | * @return Style |
||
384 | */ |
||
385 | public function setShouldWrapText($shouldWrap = true) |
||
386 | { |
||
387 | $this->shouldWrapText = $shouldWrap; |
||
388 | 76 | $this->hasSetWrapText = true; |
|
389 | |||
390 | 76 | return $this; |
|
392 | |||
393 | /** |
||
394 | * @return bool |
||
395 | */ |
||
396 | public function hasSetWrapText() |
||
400 | 3 | ||
401 | 3 | /** |
|
402 | * @return bool Whether specific font properties should be applied |
||
403 | 3 | */ |
|
404 | public function shouldApplyFont() |
||
408 | |||
409 | 84 | /** |
|
410 | * Sets the background color |
||
411 | 84 | * @param string $color ARGB color (@see Color) |
|
412 | * @return Style |
||
413 | */ |
||
414 | public function setBackgroundColor($color) |
||
421 | |||
422 | /** |
||
423 | * @return string |
||
424 | */ |
||
425 | public function getBackgroundColor() |
||
429 | |||
430 | /** |
||
431 | * @return bool Whether the background color should be applied |
||
432 | */ |
||
433 | public function shouldApplyBackgroundColor() |
||
437 | |||
438 | /** |
||
439 | * Sets format |
||
440 | * @param string $format |
||
441 | * @return Style |
||
442 | */ |
||
443 | public function setFormat($format) |
||
450 | |||
451 | /** |
||
452 | * @return string |
||
453 | */ |
||
454 | public function getFormat() |
||
458 | |||
459 | /** |
||
460 | * @return bool Whether format should be applied |
||
461 | */ |
||
462 | public function shouldApplyFormat() |
||
466 | } |
||
467 |