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 | * Bottom inset (in pixels) |
||
| 109 | * |
||
| 110 | * @var float |
||
| 111 | */ |
||
| 112 | private $bottomInset = 4.8; |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Left inset (in pixels) |
||
| 116 | * |
||
| 117 | * @var float |
||
| 118 | */ |
||
| 119 | private $leftInset = 9.6; |
||
| 120 | |||
| 121 | /** |
||
| 122 | * Right inset (in pixels) |
||
| 123 | * |
||
| 124 | * @var float |
||
| 125 | */ |
||
| 126 | private $rightInset = 9.6; |
||
| 127 | |||
| 128 | /** |
||
| 129 | * Top inset (in pixels) |
||
| 130 | * |
||
| 131 | * @var float |
||
| 132 | */ |
||
| 133 | private $topInset = 4.8; |
||
| 134 | |||
| 135 | /** |
||
| 136 | * Horizontal Auto Shrink |
||
| 137 | * @var boolean |
||
| 138 | */ |
||
| 139 | private $autoShrinkHorizontal; |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Vertical Auto Shrink |
||
| 143 | * @var boolean |
||
| 144 | */ |
||
| 145 | private $autoShrinkVertical; |
||
| 146 | |||
| 147 | /** |
||
| 148 | * The percentage of the original font size to which the text is scaled |
||
| 149 | * @var float |
||
| 150 | */ |
||
| 151 | private $fontScale; |
||
| 152 | |||
| 153 | /** |
||
| 154 | * The percentage of the reduction of the line spacing |
||
| 155 | * @var float |
||
| 156 | */ |
||
| 157 | private $lnSpcReduction; |
||
| 158 | |||
| 159 | /** |
||
| 160 | * Create a new \PhpOffice\PhpPresentation\Shape\RichText instance |
||
| 161 | */ |
||
| 162 | 86 | public function __construct() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Get active paragraph index |
||
| 176 | * |
||
| 177 | * @return int |
||
| 178 | */ |
||
| 179 | 3 | public function getActiveParagraphIndex() |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Get active paragraph |
||
| 186 | * |
||
| 187 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 188 | */ |
||
| 189 | 41 | public function getActiveParagraph() |
|
| 193 | |||
| 194 | /** |
||
| 195 | * Set active paragraph |
||
| 196 | * |
||
| 197 | * @param int $index |
||
| 198 | * @throws \Exception |
||
| 199 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 200 | */ |
||
| 201 | 8 | public function setActiveParagraph($index = 0) |
|
| 211 | |||
| 212 | /** |
||
| 213 | * Get paragraph |
||
| 214 | * |
||
| 215 | * @param int $index |
||
| 216 | * @throws \Exception |
||
| 217 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 218 | */ |
||
| 219 | 3 | public function getParagraph($index = 0) |
|
| 227 | |||
| 228 | /** |
||
| 229 | * Create paragraph |
||
| 230 | * |
||
| 231 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 232 | * @throws \Exception |
||
| 233 | */ |
||
| 234 | 12 | public function createParagraph() |
|
| 257 | |||
| 258 | /** |
||
| 259 | * Add text |
||
| 260 | * |
||
| 261 | * @param \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element |
||
| 262 | * @throws \Exception |
||
| 263 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 264 | */ |
||
| 265 | 1 | public function addText(TextElementInterface $pText = null) |
|
| 271 | |||
| 272 | /** |
||
| 273 | * Create text (can not be formatted !) |
||
| 274 | * |
||
| 275 | * @param string $pText Text |
||
| 276 | * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement |
||
| 277 | * @throws \Exception |
||
| 278 | */ |
||
| 279 | 2 | public function createText($pText = '') |
|
| 283 | |||
| 284 | /** |
||
| 285 | * Create break |
||
| 286 | * |
||
| 287 | * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement |
||
| 288 | * @throws \Exception |
||
| 289 | */ |
||
| 290 | 4 | public function createBreak() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Create text run (can be formatted) |
||
| 297 | * |
||
| 298 | * @param string $pText Text |
||
| 299 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Run |
||
| 300 | * @throws \Exception |
||
| 301 | */ |
||
| 302 | 33 | public function createTextRun($pText = '') |
|
| 306 | |||
| 307 | /** |
||
| 308 | * Get plain text |
||
| 309 | * |
||
| 310 | * @return string |
||
| 311 | */ |
||
| 312 | 1 | public function getPlainText() |
|
| 325 | |||
| 326 | /** |
||
| 327 | * Convert to string |
||
| 328 | * |
||
| 329 | * @return string |
||
| 330 | */ |
||
| 331 | 1 | public function __toString() |
|
| 335 | |||
| 336 | /** |
||
| 337 | * Get paragraphs |
||
| 338 | * |
||
| 339 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] |
||
| 340 | */ |
||
| 341 | 50 | public function getParagraphs() |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Set paragraphs |
||
| 348 | * |
||
| 349 | * @param \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs Array of paragraphs |
||
| 350 | * @throws \Exception |
||
| 351 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 352 | */ |
||
| 353 | 9 | public function setParagraphs($paragraphs = null) |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Get text wrapping |
||
| 366 | * |
||
| 367 | * @return string |
||
| 368 | */ |
||
| 369 | 25 | public function getWrap() |
|
| 373 | |||
| 374 | /** |
||
| 375 | * Set text wrapping |
||
| 376 | * |
||
| 377 | * @param $value string |
||
| 378 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 379 | */ |
||
| 380 | 2 | public function setWrap($value = self::WRAP_SQUARE) |
|
| 386 | |||
| 387 | /** |
||
| 388 | * Get autofit |
||
| 389 | * |
||
| 390 | * @return string |
||
| 391 | */ |
||
| 392 | 25 | public function getAutoFit() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Get pourcentage of fontScale |
||
| 399 | * |
||
| 400 | * @return float |
||
| 401 | */ |
||
| 402 | 1 | public function getFontScale() |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Get pourcentage of the line space reduction |
||
| 409 | * |
||
| 410 | * @return float |
||
| 411 | */ |
||
| 412 | 1 | public function getLineSpaceReduction() |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Set autofit |
||
| 419 | * |
||
| 420 | * @param $value string |
||
| 421 | * @param $fontScale float |
||
| 422 | * @param $lnSpcReduction float |
||
| 423 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 424 | */ |
||
| 425 | 2 | public function setAutoFit($value = self::AUTOFIT_DEFAULT, $fontScale = null, $lnSpcReduction = null) |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Get horizontal overflow |
||
| 442 | * |
||
| 443 | * @return string |
||
| 444 | */ |
||
| 445 | 25 | public function getHorizontalOverflow() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Set horizontal overflow |
||
| 452 | * |
||
| 453 | * @param $value string |
||
| 454 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 455 | */ |
||
| 456 | 1 | public function setHorizontalOverflow($value = self::OVERFLOW_OVERFLOW) |
|
| 462 | |||
| 463 | /** |
||
| 464 | * Get vertical overflow |
||
| 465 | * |
||
| 466 | * @return string |
||
| 467 | */ |
||
| 468 | 25 | public function getVerticalOverflow() |
|
| 472 | |||
| 473 | /** |
||
| 474 | * Set vertical overflow |
||
| 475 | * |
||
| 476 | * @param $value string |
||
| 477 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 478 | */ |
||
| 479 | 1 | public function setVerticalOverflow($value = self::OVERFLOW_OVERFLOW) |
|
| 485 | |||
| 486 | /** |
||
| 487 | * Get upright |
||
| 488 | * |
||
| 489 | * @return boolean |
||
| 490 | */ |
||
| 491 | 25 | public function isUpright() |
|
| 495 | |||
| 496 | /** |
||
| 497 | * Set vertical |
||
| 498 | * |
||
| 499 | * @param $value boolean |
||
| 500 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 501 | */ |
||
| 502 | 2 | public function setUpright($value = false) |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Get vertical |
||
| 511 | * |
||
| 512 | * @return boolean |
||
| 513 | */ |
||
| 514 | 25 | public function isVertical() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Set vertical |
||
| 521 | * |
||
| 522 | * @param $value boolean |
||
| 523 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 524 | */ |
||
| 525 | 2 | public function setVertical($value = false) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * Get columns |
||
| 534 | * |
||
| 535 | * @return int |
||
| 536 | */ |
||
| 537 | 25 | public function getColumns() |
|
| 541 | |||
| 542 | /** |
||
| 543 | * Set columns |
||
| 544 | * |
||
| 545 | * @param $value int |
||
| 546 | * @throws \Exception |
||
| 547 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 548 | */ |
||
| 549 | 2 | public function setColumns($value = 1) |
|
| 559 | |||
| 560 | /** |
||
| 561 | * Get bottom inset |
||
| 562 | * |
||
| 563 | * @return float |
||
| 564 | */ |
||
| 565 | 25 | public function getInsetBottom() |
|
| 569 | |||
| 570 | /** |
||
| 571 | * Set bottom inset |
||
| 572 | * |
||
| 573 | * @param $value float |
||
| 574 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 575 | */ |
||
| 576 | 5 | public function setInsetBottom($value = 4.8) |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Get left inset |
||
| 585 | * |
||
| 586 | * @return float |
||
| 587 | */ |
||
| 588 | 25 | public function getInsetLeft() |
|
| 592 | |||
| 593 | /** |
||
| 594 | * Set left inset |
||
| 595 | * |
||
| 596 | * @param $value float |
||
| 597 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 598 | */ |
||
| 599 | 5 | public function setInsetLeft($value = 9.6) |
|
| 605 | |||
| 606 | /** |
||
| 607 | * Get right inset |
||
| 608 | * |
||
| 609 | * @return float |
||
| 610 | */ |
||
| 611 | 25 | public function getInsetRight() |
|
| 615 | |||
| 616 | /** |
||
| 617 | * Set left inset |
||
| 618 | * |
||
| 619 | * @param $value float |
||
| 620 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 621 | */ |
||
| 622 | 5 | public function setInsetRight($value = 9.6) |
|
| 628 | |||
| 629 | /** |
||
| 630 | * Get top inset |
||
| 631 | * |
||
| 632 | * @return float |
||
| 633 | */ |
||
| 634 | 25 | public function getInsetTop() |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Set top inset |
||
| 641 | * |
||
| 642 | * @param $value float |
||
| 643 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 644 | */ |
||
| 645 | 5 | public function setInsetTop($value = 4.8) |
|
| 651 | |||
| 652 | /** |
||
| 653 | * Set horizontal auto shrink |
||
| 654 | * @param bool $value |
||
| 655 | * @return RichText |
||
| 656 | */ |
||
| 657 | 2 | public function setAutoShrinkHorizontal($value = null) |
|
| 664 | |||
| 665 | /** |
||
| 666 | * Get horizontal auto shrink |
||
| 667 | * @return bool |
||
| 668 | */ |
||
| 669 | 15 | public function hasAutoShrinkHorizontal() |
|
| 673 | |||
| 674 | /** |
||
| 675 | * Set vertical auto shrink |
||
| 676 | * @param bool $value |
||
| 677 | * @return RichText |
||
| 678 | */ |
||
| 679 | 2 | public function setAutoShrinkVertical($value = null) |
|
| 686 | |||
| 687 | /** |
||
| 688 | * Set vertical auto shrink |
||
| 689 | * @return bool |
||
| 690 | */ |
||
| 691 | 15 | public function hasAutoShrinkVertical() |
|
| 695 | |||
| 696 | /** |
||
| 697 | * Get hash code |
||
| 698 | * |
||
| 699 | * @return string Hash code |
||
| 700 | */ |
||
| 701 | 3 | public function getHashCode() |
|
| 710 | } |
||
| 711 |