Complex classes like RichText 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 RichText, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
28 | class RichText extends AbstractShape implements ComparableInterface |
||
29 | { |
||
30 | /** Wrapping */ |
||
31 | const WRAP_NONE = 'none'; |
||
32 | const WRAP_SQUARE = 'square'; |
||
33 | |||
34 | /** Autofit */ |
||
35 | const AUTOFIT_DEFAULT = 'spAutoFit'; |
||
36 | const AUTOFIT_SHAPE = 'spAutoFit'; |
||
37 | const AUTOFIT_NOAUTOFIT = 'noAutofit'; |
||
38 | const AUTOFIT_NORMAL = 'normAutofit'; |
||
39 | |||
40 | /** Overflow */ |
||
41 | const OVERFLOW_CLIP = 'clip'; |
||
42 | const OVERFLOW_OVERFLOW = 'overflow'; |
||
43 | |||
44 | /** |
||
45 | * Rich text paragraphs |
||
46 | * |
||
47 | * @var \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] |
||
48 | */ |
||
49 | private $richTextParagraphs; |
||
50 | |||
51 | /** |
||
52 | * Active paragraph |
||
53 | * |
||
54 | * @var int |
||
55 | */ |
||
56 | private $activeParagraph = 0; |
||
57 | |||
58 | /** |
||
59 | * Text wrapping |
||
60 | * |
||
61 | * @var string |
||
62 | */ |
||
63 | private $wrap = self::WRAP_SQUARE; |
||
64 | |||
65 | /** |
||
66 | * Autofit |
||
67 | * |
||
68 | * @var string |
||
69 | */ |
||
70 | private $autoFit = self::AUTOFIT_DEFAULT; |
||
71 | |||
72 | /** |
||
73 | * Horizontal overflow |
||
74 | * |
||
75 | * @var string |
||
76 | */ |
||
77 | private $horizontalOverflow = self::OVERFLOW_OVERFLOW; |
||
78 | |||
79 | /** |
||
80 | * Vertical overflow |
||
81 | * |
||
82 | * @var string |
||
83 | */ |
||
84 | private $verticalOverflow = self::OVERFLOW_OVERFLOW; |
||
85 | |||
86 | /** |
||
87 | * Text upright? |
||
88 | * |
||
89 | * @var boolean |
||
90 | */ |
||
91 | private $upright = false; |
||
92 | |||
93 | /** |
||
94 | * Vertical text? |
||
95 | * |
||
96 | * @var boolean |
||
97 | */ |
||
98 | private $vertical = false; |
||
99 | |||
100 | /** |
||
101 | * Number of columns (1 - 16) |
||
102 | * |
||
103 | * @var int |
||
104 | */ |
||
105 | private $columns = 1; |
||
106 | |||
107 | /** |
||
108 | * The spacing between columns |
||
109 | * |
||
110 | * @var float |
||
111 | */ |
||
112 | private $columnSpacing = 0; |
||
113 | |||
114 | /** |
||
115 | * Bottom inset (in pixels) |
||
116 | * |
||
117 | * @var float |
||
118 | */ |
||
119 | private $bottomInset = 4.8; |
||
120 | |||
121 | /** |
||
122 | * Left inset (in pixels) |
||
123 | * |
||
124 | * @var float |
||
125 | */ |
||
126 | private $leftInset = 9.6; |
||
127 | |||
128 | /** |
||
129 | * Right inset (in pixels) |
||
130 | * |
||
131 | * @var float |
||
132 | */ |
||
133 | private $rightInset = 9.6; |
||
134 | |||
135 | /** |
||
136 | * Top inset (in pixels) |
||
137 | * |
||
138 | * @var float |
||
139 | */ |
||
140 | private $topInset = 4.8; |
||
141 | |||
142 | /** |
||
143 | * Horizontal Auto Shrink |
||
144 | * @var boolean |
||
145 | */ |
||
146 | private $autoShrinkHorizontal; |
||
147 | |||
148 | /** |
||
149 | * Vertical Auto Shrink |
||
150 | * @var boolean |
||
151 | */ |
||
152 | private $autoShrinkVertical; |
||
153 | |||
154 | /** |
||
155 | * The percentage of the original font size to which the text is scaled |
||
156 | * @var float |
||
157 | */ |
||
158 | private $fontScale; |
||
159 | |||
160 | /** |
||
161 | * The percentage of the reduction of the line spacing |
||
162 | 86 | * @var float |
|
163 | */ |
||
164 | private $lnSpcReduction; |
||
165 | 86 | ||
166 | 86 | /** |
|
167 | * Create a new \PhpOffice\PhpPresentation\Shape\RichText instance |
||
168 | 86 | */ |
|
169 | public function __construct() |
||
180 | |||
181 | 3 | /** |
|
182 | * Get active paragraph index |
||
183 | * |
||
184 | * @return int |
||
185 | */ |
||
186 | public function getActiveParagraphIndex() |
||
190 | |||
191 | 41 | /** |
|
192 | * Get active paragraph |
||
193 | * |
||
194 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
195 | */ |
||
196 | public function getActiveParagraph() |
||
200 | |||
201 | 8 | /** |
|
202 | * Set active paragraph |
||
203 | 8 | * |
|
204 | 1 | * @param int $index |
|
205 | * @throws \Exception |
||
206 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
207 | 7 | */ |
|
208 | public function setActiveParagraph($index = 0) |
||
218 | |||
219 | 3 | /** |
|
220 | * Get paragraph |
||
221 | 3 | * |
|
222 | 1 | * @param int $index |
|
223 | * @throws \Exception |
||
224 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
225 | 2 | */ |
|
226 | public function getParagraph($index = 0) |
||
234 | 12 | ||
235 | /** |
||
236 | 12 | * Create paragraph |
|
237 | 12 | * |
|
238 | 11 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
|
239 | 11 | * @throws \Exception |
|
240 | 11 | */ |
|
241 | public function createParagraph() |
||
264 | |||
265 | 1 | /** |
|
266 | * Add text |
||
267 | 1 | * |
|
268 | * @param \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element |
||
269 | 1 | * @throws \Exception |
|
270 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
271 | */ |
||
272 | public function addText(TextElementInterface $pText = null) |
||
278 | |||
279 | 2 | /** |
|
280 | * Create text (can not be formatted !) |
||
281 | 2 | * |
|
282 | * @param string $pText Text |
||
283 | * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement |
||
284 | * @throws \Exception |
||
285 | */ |
||
286 | public function createText($pText = '') |
||
290 | 4 | ||
291 | /** |
||
292 | 4 | * Create break |
|
293 | * |
||
294 | * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement |
||
295 | * @throws \Exception |
||
296 | */ |
||
297 | public function createBreak() |
||
301 | |||
302 | 33 | /** |
|
303 | * Create text run (can be formatted) |
||
304 | 33 | * |
|
305 | * @param string $pText Text |
||
306 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Run |
||
307 | * @throws \Exception |
||
308 | */ |
||
309 | public function createTextRun($pText = '') |
||
313 | |||
314 | /** |
||
315 | 1 | * Get plain text |
|
316 | * |
||
317 | * @return string |
||
318 | 1 | */ |
|
319 | 1 | public function getPlainText() |
|
332 | |||
333 | 1 | /** |
|
334 | * Convert to string |
||
335 | * |
||
336 | * @return string |
||
337 | */ |
||
338 | public function __toString() |
||
342 | |||
343 | 50 | /** |
|
344 | * Get paragraphs |
||
345 | * |
||
346 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] |
||
347 | */ |
||
348 | public function getParagraphs() |
||
352 | |||
353 | 9 | /** |
|
354 | * Set paragraphs |
||
355 | 9 | * |
|
356 | 1 | * @param \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs Array of paragraphs |
|
357 | * @throws \Exception |
||
358 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
359 | 8 | */ |
|
360 | 8 | public function setParagraphs($paragraphs = null) |
|
370 | |||
371 | 25 | /** |
|
372 | * Get text wrapping |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getWrap() |
||
380 | 2 | ||
381 | /** |
||
382 | 2 | * Set text wrapping |
|
383 | * |
||
384 | 2 | * @param $value string |
|
385 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
386 | */ |
||
387 | public function setWrap($value = self::WRAP_SQUARE) |
||
393 | |||
394 | 25 | /** |
|
395 | * Get autofit |
||
396 | * |
||
397 | * @return string |
||
398 | */ |
||
399 | public function getAutoFit() |
||
403 | |||
404 | 1 | /** |
|
405 | * Get pourcentage of fontScale |
||
406 | * |
||
407 | * @return float |
||
408 | */ |
||
409 | public function getFontScale() |
||
413 | |||
414 | 1 | /** |
|
415 | * Get pourcentage of the line space reduction |
||
416 | * |
||
417 | * @return float |
||
418 | */ |
||
419 | public function getLineSpaceReduction() |
||
423 | |||
424 | /** |
||
425 | 2 | * Set autofit |
|
426 | * |
||
427 | 2 | * @param $value string |
|
428 | * @param $fontScale float |
||
429 | 2 | * @param $lnSpcReduction float |
|
430 | 1 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
|
431 | */ |
||
432 | public function setAutoFit($value = self::AUTOFIT_DEFAULT, $fontScale = null, $lnSpcReduction = null) |
||
446 | |||
447 | 25 | /** |
|
448 | * Get horizontal overflow |
||
449 | * |
||
450 | * @return string |
||
451 | */ |
||
452 | public function getHorizontalOverflow() |
||
456 | 1 | ||
457 | /** |
||
458 | 1 | * Set horizontal overflow |
|
459 | * |
||
460 | 1 | * @param $value string |
|
461 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
462 | */ |
||
463 | public function setHorizontalOverflow($value = self::OVERFLOW_OVERFLOW) |
||
469 | |||
470 | 25 | /** |
|
471 | * Get vertical overflow |
||
472 | * |
||
473 | * @return string |
||
474 | */ |
||
475 | public function getVerticalOverflow() |
||
479 | 1 | ||
480 | /** |
||
481 | 1 | * Set vertical overflow |
|
482 | * |
||
483 | 1 | * @param $value string |
|
484 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
485 | */ |
||
486 | public function setVerticalOverflow($value = self::OVERFLOW_OVERFLOW) |
||
492 | |||
493 | 25 | /** |
|
494 | * Get upright |
||
495 | * |
||
496 | * @return boolean |
||
497 | */ |
||
498 | public function isUpright() |
||
502 | 2 | ||
503 | /** |
||
504 | 2 | * Set vertical |
|
505 | * |
||
506 | 2 | * @param $value boolean |
|
507 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
508 | */ |
||
509 | public function setUpright($value = false) |
||
515 | |||
516 | 25 | /** |
|
517 | * Get vertical |
||
518 | * |
||
519 | * @return boolean |
||
520 | */ |
||
521 | public function isVertical() |
||
525 | 2 | ||
526 | /** |
||
527 | 2 | * Set vertical |
|
528 | * |
||
529 | 2 | * @param $value boolean |
|
530 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
531 | */ |
||
532 | public function setVertical($value = false) |
||
538 | |||
539 | 25 | /** |
|
540 | * Get columns |
||
541 | * |
||
542 | * @return int |
||
543 | */ |
||
544 | public function getColumns() |
||
548 | |||
549 | 2 | /** |
|
550 | * Set columns |
||
551 | 2 | * |
|
552 | 1 | * @param $value int |
|
553 | * @throws \Exception |
||
554 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
555 | 1 | */ |
|
556 | public function setColumns($value = 1) |
||
566 | |||
567 | 25 | /** |
|
568 | * Get spacing between columns |
||
569 | * |
||
570 | * @return int |
||
571 | */ |
||
572 | public function getColumnSpacing() |
||
576 | 5 | ||
577 | /** |
||
578 | 5 | * Set spacing between columns |
|
579 | * |
||
580 | 5 | * @param $value float |
|
581 | * @throws \Exception |
||
582 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
583 | */ |
||
584 | public function setColumnSpacing($value = 0) |
||
594 | |||
595 | /** |
||
596 | * Get bottom inset |
||
597 | * |
||
598 | * @return float |
||
599 | 5 | */ |
|
600 | public function getInsetBottom() |
||
604 | |||
605 | /** |
||
606 | * Set bottom inset |
||
607 | * |
||
608 | * @param $value float |
||
609 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
610 | */ |
||
611 | 25 | public function setInsetBottom($value = 4.8) |
|
617 | |||
618 | /** |
||
619 | * Get left inset |
||
620 | * |
||
621 | * @return float |
||
622 | 5 | */ |
|
623 | public function getInsetLeft() |
||
627 | |||
628 | /** |
||
629 | * Set left inset |
||
630 | * |
||
631 | * @param $value float |
||
632 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
633 | */ |
||
634 | 25 | public function setInsetLeft($value = 9.6) |
|
640 | |||
641 | /** |
||
642 | * Get right inset |
||
643 | * |
||
644 | * @return float |
||
645 | 5 | */ |
|
646 | public function getInsetRight() |
||
650 | |||
651 | /** |
||
652 | * Set left inset |
||
653 | * |
||
654 | * @param $value float |
||
655 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
656 | */ |
||
657 | 2 | public function setInsetRight($value = 9.6) |
|
663 | |||
664 | /** |
||
665 | * Get top inset |
||
666 | * |
||
667 | * @return float |
||
668 | */ |
||
669 | 15 | public function getInsetTop() |
|
673 | |||
674 | /** |
||
675 | * Set top inset |
||
676 | * |
||
677 | * @param $value float |
||
678 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
679 | 2 | */ |
|
680 | public function setInsetTop($value = 4.8) |
||
686 | |||
687 | /** |
||
688 | * Set horizontal auto shrink |
||
689 | * @param bool $value |
||
690 | * @return RichText |
||
691 | 15 | */ |
|
692 | public function setAutoShrinkHorizontal($value = null) |
||
699 | |||
700 | /** |
||
701 | 3 | * Get horizontal auto shrink |
|
702 | * @return bool |
||
703 | 3 | */ |
|
704 | 3 | public function hasAutoShrinkHorizontal() |
|
708 | 3 | ||
709 | /** |
||
710 | * Set vertical auto shrink |
||
711 | * @param bool $value |
||
712 | * @return RichText |
||
713 | */ |
||
714 | public function setAutoShrinkVertical($value = null) |
||
721 | |||
722 | /** |
||
723 | * Set vertical auto shrink |
||
724 | * @return bool |
||
725 | */ |
||
726 | public function hasAutoShrinkVertical() |
||
730 | |||
731 | /** |
||
732 | * Get hash code |
||
733 | * |
||
734 | * @return string Hash code |
||
735 | */ |
||
736 | public function getHashCode() |
||
745 | } |
||
746 |
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.