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 | /** @var bool */ |
||
91 | private $isEmpty = true; |
||
92 | |||
93 | /** |
||
94 | * @return int|null |
||
95 | */ |
||
96 | 94 | public function getId() |
|
97 | { |
||
98 | 94 | return $this->id; |
|
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param int $id |
||
103 | * @return Style |
||
104 | */ |
||
105 | 94 | public function setId($id) |
|
106 | { |
||
107 | 94 | $this->id = $id; |
|
108 | |||
109 | 94 | return $this; |
|
110 | } |
||
111 | |||
112 | /** |
||
113 | * @return Border |
||
114 | */ |
||
115 | 80 | public function getBorder() |
|
116 | { |
||
117 | 80 | return $this->border; |
|
118 | } |
||
119 | |||
120 | /** |
||
121 | * @param Border $border |
||
122 | * @return Style |
||
123 | */ |
||
124 | 8 | public function setBorder(Border $border) |
|
125 | { |
||
126 | 8 | $this->shouldApplyBorder = true; |
|
127 | 8 | $this->border = $border; |
|
128 | 8 | $this->isEmpty = false; |
|
129 | |||
130 | 8 | return $this; |
|
131 | } |
||
132 | |||
133 | /** |
||
134 | * @return bool |
||
135 | */ |
||
136 | 92 | public function shouldApplyBorder() |
|
140 | |||
141 | /** |
||
142 | * @return bool |
||
143 | */ |
||
144 | 82 | public function isFontBold() |
|
148 | |||
149 | /** |
||
150 | * @return Style |
||
151 | */ |
||
152 | 20 | public function setFontBold() |
|
153 | { |
||
154 | 20 | $this->fontBold = true; |
|
155 | 20 | $this->hasSetFontBold = true; |
|
156 | 20 | $this->shouldApplyFont = true; |
|
157 | 20 | $this->isEmpty = false; |
|
158 | |||
159 | 20 | return $this; |
|
160 | } |
||
161 | |||
162 | /** |
||
163 | * @return bool |
||
164 | */ |
||
165 | 79 | public function hasSetFontBold() |
|
169 | |||
170 | /** |
||
171 | * @return bool |
||
172 | */ |
||
173 | 82 | public function isFontItalic() |
|
174 | { |
||
175 | 82 | return $this->fontItalic; |
|
176 | } |
||
177 | |||
178 | /** |
||
179 | * @return Style |
||
180 | */ |
||
181 | 7 | public function setFontItalic() |
|
182 | { |
||
183 | 7 | $this->fontItalic = true; |
|
184 | 7 | $this->hasSetFontItalic = true; |
|
185 | 7 | $this->shouldApplyFont = true; |
|
186 | 7 | $this->isEmpty = false; |
|
187 | |||
188 | 7 | return $this; |
|
189 | } |
||
190 | |||
191 | /** |
||
192 | * @return bool |
||
193 | */ |
||
194 | 79 | public function hasSetFontItalic() |
|
195 | { |
||
196 | 79 | return $this->hasSetFontItalic; |
|
197 | } |
||
198 | |||
199 | /** |
||
200 | * @return bool |
||
201 | */ |
||
202 | 82 | public function isFontUnderline() |
|
203 | { |
||
204 | 82 | return $this->fontUnderline; |
|
205 | } |
||
206 | |||
207 | /** |
||
208 | * @return Style |
||
209 | */ |
||
210 | 7 | public function setFontUnderline() |
|
211 | { |
||
212 | 7 | $this->fontUnderline = true; |
|
213 | 7 | $this->hasSetFontUnderline = true; |
|
214 | 7 | $this->shouldApplyFont = true; |
|
215 | 7 | $this->isEmpty = false; |
|
216 | |||
217 | 7 | return $this; |
|
218 | } |
||
219 | |||
220 | /** |
||
221 | * @return bool |
||
222 | */ |
||
223 | 79 | public function hasSetFontUnderline() |
|
224 | { |
||
225 | 79 | return $this->hasSetFontUnderline; |
|
226 | } |
||
227 | |||
228 | /** |
||
229 | * @return bool |
||
230 | */ |
||
231 | 82 | public function isFontStrikethrough() |
|
232 | { |
||
233 | 82 | return $this->fontStrikethrough; |
|
234 | } |
||
235 | |||
236 | /** |
||
237 | * @return Style |
||
238 | */ |
||
239 | 4 | public function setFontStrikethrough() |
|
240 | { |
||
241 | 4 | $this->fontStrikethrough = true; |
|
242 | 4 | $this->hasSetFontStrikethrough = true; |
|
243 | 4 | $this->shouldApplyFont = true; |
|
244 | 4 | $this->isEmpty = false; |
|
245 | |||
246 | 4 | return $this; |
|
247 | } |
||
248 | |||
249 | /** |
||
250 | * @return bool |
||
251 | */ |
||
252 | 79 | public function hasSetFontStrikethrough() |
|
253 | { |
||
254 | 79 | return $this->hasSetFontStrikethrough; |
|
255 | } |
||
256 | |||
257 | /** |
||
258 | * @return int |
||
259 | */ |
||
260 | 84 | public function getFontSize() |
|
261 | { |
||
262 | 84 | return $this->fontSize; |
|
263 | } |
||
264 | |||
265 | /** |
||
266 | * @param int $fontSize Font size, in pixels |
||
267 | * @return Style |
||
268 | */ |
||
269 | 55 | public function setFontSize($fontSize) |
|
270 | { |
||
271 | 55 | $this->fontSize = $fontSize; |
|
272 | 55 | $this->hasSetFontSize = true; |
|
273 | 55 | $this->shouldApplyFont = true; |
|
274 | 55 | $this->isEmpty = false; |
|
275 | |||
276 | 55 | return $this; |
|
277 | } |
||
278 | |||
279 | /** |
||
280 | * @return bool |
||
281 | */ |
||
282 | 79 | public function hasSetFontSize() |
|
283 | { |
||
284 | 79 | return $this->hasSetFontSize; |
|
285 | } |
||
286 | |||
287 | /** |
||
288 | * @return string |
||
289 | */ |
||
290 | 84 | public function getFontColor() |
|
291 | { |
||
292 | 84 | return $this->fontColor; |
|
293 | } |
||
294 | |||
295 | /** |
||
296 | * Sets the font color. |
||
297 | * |
||
298 | * @param string $fontColor ARGB color (@see Color) |
||
299 | * @return Style |
||
300 | */ |
||
301 | 3 | public function setFontColor($fontColor) |
|
302 | { |
||
303 | 3 | $this->fontColor = $fontColor; |
|
304 | 3 | $this->hasSetFontColor = true; |
|
305 | 3 | $this->shouldApplyFont = true; |
|
306 | 3 | $this->isEmpty = false; |
|
307 | |||
308 | 3 | return $this; |
|
309 | } |
||
310 | |||
311 | /** |
||
312 | * @return bool |
||
313 | */ |
||
314 | 79 | public function hasSetFontColor() |
|
315 | { |
||
316 | 79 | return $this->hasSetFontColor; |
|
317 | } |
||
318 | |||
319 | /** |
||
320 | * @return string |
||
321 | */ |
||
322 | 88 | public function getFontName() |
|
326 | |||
327 | /** |
||
328 | * @param string $fontName Name of the font to use |
||
329 | * @return Style |
||
330 | */ |
||
331 | 53 | public function setFontName($fontName) |
|
332 | { |
||
333 | 53 | $this->fontName = $fontName; |
|
334 | 53 | $this->hasSetFontName = true; |
|
335 | 53 | $this->shouldApplyFont = true; |
|
336 | 53 | $this->isEmpty = false; |
|
337 | |||
338 | 53 | return $this; |
|
339 | } |
||
340 | |||
341 | /** |
||
342 | * @return bool |
||
343 | */ |
||
344 | 79 | public function hasSetFontName() |
|
345 | { |
||
346 | 79 | return $this->hasSetFontName; |
|
347 | } |
||
348 | |||
349 | /** |
||
350 | * @return string |
||
351 | */ |
||
352 | 2 | public function getCellAlignment() |
|
356 | |||
357 | /** |
||
358 | * @param string $cellAlignment The cell alignment |
||
359 | * |
||
360 | * @return Style |
||
361 | */ |
||
362 | 3 | public function setCellAlignment($cellAlignment) |
|
363 | { |
||
364 | 3 | $this->cellAlignment = $cellAlignment; |
|
365 | 3 | $this->hasSetCellAlignment = true; |
|
366 | 3 | $this->shouldApplyCellAlignment = true; |
|
367 | 3 | $this->isEmpty = false; |
|
368 | |||
369 | 3 | return $this; |
|
370 | } |
||
371 | |||
372 | /** |
||
373 | * @return bool |
||
374 | */ |
||
375 | 79 | public function hasSetCellAlignment() |
|
379 | |||
380 | /** |
||
381 | * @return bool Whether specific cell alignment should be applied |
||
382 | */ |
||
383 | 85 | public function shouldApplyCellAlignment() |
|
384 | { |
||
385 | 85 | return $this->shouldApplyCellAlignment; |
|
386 | } |
||
387 | |||
388 | /** |
||
389 | * @return bool |
||
390 | */ |
||
391 | 87 | public function shouldWrapText() |
|
392 | { |
||
393 | 87 | return $this->shouldWrapText; |
|
394 | } |
||
395 | |||
396 | /** |
||
397 | * @param bool $shouldWrap Should the text be wrapped |
||
398 | * @return Style |
||
399 | */ |
||
400 | 8 | public function setShouldWrapText($shouldWrap = true) |
|
401 | { |
||
402 | 8 | $this->shouldWrapText = $shouldWrap; |
|
403 | 8 | $this->hasSetWrapText = true; |
|
404 | 8 | $this->isEmpty = false; |
|
405 | |||
406 | 8 | return $this; |
|
407 | } |
||
408 | |||
409 | /** |
||
410 | * @return bool |
||
411 | */ |
||
412 | 82 | public function hasSetWrapText() |
|
413 | { |
||
414 | 82 | return $this->hasSetWrapText; |
|
415 | } |
||
416 | |||
417 | /** |
||
418 | * @return bool Whether specific font properties should be applied |
||
419 | */ |
||
420 | 75 | public function shouldApplyFont() |
|
421 | { |
||
422 | 75 | return $this->shouldApplyFont; |
|
423 | } |
||
424 | |||
425 | /** |
||
426 | * Sets the background color |
||
427 | * @param string $color ARGB color (@see Color) |
||
428 | * @return Style |
||
429 | */ |
||
430 | 7 | public function setBackgroundColor($color) |
|
431 | { |
||
432 | 7 | $this->hasSetBackgroundColor = true; |
|
433 | 7 | $this->backgroundColor = $color; |
|
434 | 7 | $this->isEmpty = false; |
|
435 | |||
436 | 7 | return $this; |
|
437 | } |
||
438 | |||
439 | /** |
||
440 | * @return string |
||
441 | */ |
||
442 | 49 | public function getBackgroundColor() |
|
446 | |||
447 | /** |
||
448 | * @return bool Whether the background color should be applied |
||
449 | */ |
||
450 | 81 | public function shouldApplyBackgroundColor() |
|
451 | { |
||
452 | 81 | return $this->hasSetBackgroundColor; |
|
453 | } |
||
454 | |||
455 | /** |
||
456 | * Sets format |
||
457 | * @param string $format |
||
458 | * @return Style |
||
459 | */ |
||
460 | 3 | public function setFormat($format) |
|
461 | { |
||
462 | 3 | $this->hasSetFormat = true; |
|
463 | 3 | $this->format = $format; |
|
464 | 3 | $this->isEmpty = false; |
|
465 | |||
466 | 3 | return $this; |
|
467 | } |
||
468 | |||
469 | /** |
||
470 | * @return string |
||
471 | */ |
||
472 | 89 | public function getFormat() |
|
473 | { |
||
474 | 89 | return $this->format; |
|
475 | } |
||
476 | |||
477 | /** |
||
478 | * @return bool Whether format should be applied |
||
479 | */ |
||
480 | 78 | public function shouldApplyFormat() |
|
484 | |||
485 | /** |
||
486 | * @return bool |
||
487 | */ |
||
488 | 86 | public function isRegistered() : bool |
|
492 | |||
493 | 94 | public function markAsRegistered(?int $id) : void |
|
494 | { |
||
495 | 94 | $this->setId($id); |
|
496 | 94 | $this->isRegistered = true; |
|
497 | 94 | } |
|
498 | |||
499 | 94 | public function unmarkAsRegistered() : void |
|
500 | { |
||
501 | 94 | $this->setId(0); |
|
502 | 94 | $this->isRegistered = false; |
|
503 | 94 | } |
|
504 | |||
505 | 70 | public function isEmpty() : bool |
|
509 | } |
||
510 |