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 | private $bodyPr; |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Create a new \PhpOffice\PhpPresentation\Shape\RichText instance |
||
| 164 | */ |
||
| 165 | 82 | public function __construct() |
|
| 176 | |||
| 177 | /** |
||
| 178 | * Get active paragraph index |
||
| 179 | * |
||
| 180 | * @return int |
||
| 181 | */ |
||
| 182 | 3 | public function getActiveParagraphIndex() |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Get active paragraph |
||
| 189 | * |
||
| 190 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 191 | */ |
||
| 192 | 39 | public function getActiveParagraph() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Set active paragraph |
||
| 199 | * |
||
| 200 | * @param int $index |
||
| 201 | * @throws \Exception |
||
| 202 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 203 | */ |
||
| 204 | 4 | public function setActiveParagraph($index = 0) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Get paragraph |
||
| 217 | * |
||
| 218 | * @param int $index |
||
| 219 | * @throws \Exception |
||
| 220 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 221 | */ |
||
| 222 | 3 | public function getParagraph($index = 0) |
|
| 230 | |||
| 231 | /** |
||
| 232 | * Create paragraph |
||
| 233 | * |
||
| 234 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 235 | */ |
||
| 236 | 8 | public function createParagraph() |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Add text |
||
| 262 | * |
||
| 263 | * @param \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element |
||
| 264 | * @throws \Exception |
||
| 265 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 266 | */ |
||
| 267 | 1 | public function addText(TextElementInterface $pText = null) |
|
| 273 | |||
| 274 | /** |
||
| 275 | * Create text (can not be formatted !) |
||
| 276 | * |
||
| 277 | * @param string $pText Text |
||
| 278 | * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement |
||
| 279 | * @throws \Exception |
||
| 280 | */ |
||
| 281 | 2 | public function createText($pText = '') |
|
| 285 | |||
| 286 | /** |
||
| 287 | * Create break |
||
| 288 | * |
||
| 289 | * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement |
||
| 290 | * @throws \Exception |
||
| 291 | */ |
||
| 292 | 4 | public function createBreak() |
|
| 296 | |||
| 297 | /** |
||
| 298 | * Create text run (can be formatted) |
||
| 299 | * |
||
| 300 | * @param string $pText Text |
||
| 301 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Run |
||
| 302 | * @throws \Exception |
||
| 303 | */ |
||
| 304 | 33 | public function createTextRun($pText = '') |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Get plain text |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 1 | public function getPlainText() |
|
| 327 | |||
| 328 | /** |
||
| 329 | * Convert to string |
||
| 330 | * |
||
| 331 | * @return string |
||
| 332 | */ |
||
| 333 | 1 | public function __toString() |
|
| 337 | |||
| 338 | /** |
||
| 339 | * Get paragraphs |
||
| 340 | * |
||
| 341 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] |
||
| 342 | */ |
||
| 343 | 47 | public function getParagraphs() |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Set paragraphs |
||
| 350 | * |
||
| 351 | * @param \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs Array of paragraphs |
||
| 352 | * @throws \Exception |
||
| 353 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 354 | */ |
||
| 355 | 5 | public function setParagraphs($paragraphs = null) |
|
| 365 | |||
| 366 | /** |
||
| 367 | * Get text wrapping |
||
| 368 | * |
||
| 369 | * @return string |
||
| 370 | */ |
||
| 371 | 27 | public function getWrap() |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Set text wrapping |
||
| 378 | * |
||
| 379 | * @param $value string |
||
| 380 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 381 | */ |
||
| 382 | 2 | public function setWrap($value = self::WRAP_SQUARE) |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Get autofit |
||
| 391 | * |
||
| 392 | * @return string |
||
| 393 | */ |
||
| 394 | 27 | public function getAutoFit() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Get pourcentage of fontScale |
||
| 401 | * |
||
| 402 | * @return float |
||
| 403 | */ |
||
| 404 | 1 | public function getFontScale() |
|
| 408 | |||
| 409 | /** |
||
| 410 | * Get pourcentage of the line space reduction |
||
| 411 | * |
||
| 412 | * @return float |
||
| 413 | */ |
||
| 414 | 1 | public function getLineSpaceReduction() |
|
| 418 | |||
| 419 | /** |
||
| 420 | * Set autofit |
||
| 421 | * |
||
| 422 | * @param $value string |
||
| 423 | * @param $fontScale float |
||
| 424 | * @param $lnSpcReduction float |
||
| 425 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 426 | */ |
||
| 427 | 2 | public function setAutoFit($value = self::AUTOFIT_DEFAULT, $fontScale = null, $lnSpcReduction = null) |
|
| 441 | |||
| 442 | /** |
||
| 443 | * Get horizontal overflow |
||
| 444 | * |
||
| 445 | * @return string |
||
| 446 | */ |
||
| 447 | 27 | public function getHorizontalOverflow() |
|
| 451 | |||
| 452 | /** |
||
| 453 | * Set horizontal overflow |
||
| 454 | * |
||
| 455 | * @param $value string |
||
| 456 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 457 | */ |
||
| 458 | 1 | public function setHorizontalOverflow($value = self::OVERFLOW_OVERFLOW) |
|
| 464 | |||
| 465 | /** |
||
| 466 | * Get vertical overflow |
||
| 467 | * |
||
| 468 | * @return string |
||
| 469 | */ |
||
| 470 | 27 | public function getVerticalOverflow() |
|
| 474 | |||
| 475 | /** |
||
| 476 | * Set vertical overflow |
||
| 477 | * |
||
| 478 | * @param $value string |
||
| 479 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 480 | */ |
||
| 481 | 1 | public function setVerticalOverflow($value = self::OVERFLOW_OVERFLOW) |
|
| 487 | |||
| 488 | /** |
||
| 489 | * Get upright |
||
| 490 | * |
||
| 491 | * @return boolean |
||
| 492 | */ |
||
| 493 | 27 | public function isUpright() |
|
| 497 | |||
| 498 | /** |
||
| 499 | * Set vertical |
||
| 500 | * |
||
| 501 | * @param $value boolean |
||
| 502 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 503 | */ |
||
| 504 | 2 | public function setUpright($value = false) |
|
| 510 | |||
| 511 | /** |
||
| 512 | * Get vertical |
||
| 513 | * |
||
| 514 | * @return boolean |
||
| 515 | */ |
||
| 516 | 27 | public function isVertical() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Set vertical |
||
| 523 | * |
||
| 524 | * @param $value boolean |
||
| 525 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 526 | */ |
||
| 527 | 2 | public function setVertical($value = false) |
|
| 533 | |||
| 534 | /** |
||
| 535 | * Get columns |
||
| 536 | * |
||
| 537 | * @return int |
||
| 538 | */ |
||
| 539 | 27 | public function getColumns() |
|
| 543 | |||
| 544 | /** |
||
| 545 | * Set columns |
||
| 546 | * |
||
| 547 | * @param $value int |
||
| 548 | * @throws \Exception |
||
| 549 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 550 | */ |
||
| 551 | 2 | public function setColumns($value = 1) |
|
| 561 | |||
| 562 | /** |
||
| 563 | * Get bottom inset |
||
| 564 | * |
||
| 565 | * @return float |
||
| 566 | */ |
||
| 567 | 27 | public function getInsetBottom() |
|
| 571 | |||
| 572 | /** |
||
| 573 | * Set bottom inset |
||
| 574 | * |
||
| 575 | * @param $value float |
||
| 576 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 577 | */ |
||
| 578 | 5 | public function setInsetBottom($value = 4.8) |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Get left inset |
||
| 587 | * |
||
| 588 | * @return float |
||
| 589 | */ |
||
| 590 | 27 | public function getInsetLeft() |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Set left inset |
||
| 597 | * |
||
| 598 | * @param $value float |
||
| 599 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 600 | */ |
||
| 601 | 5 | public function setInsetLeft($value = 9.6) |
|
| 607 | |||
| 608 | /** |
||
| 609 | * Get right inset |
||
| 610 | * |
||
| 611 | * @return float |
||
| 612 | */ |
||
| 613 | 27 | public function getInsetRight() |
|
| 617 | |||
| 618 | /** |
||
| 619 | * Set left inset |
||
| 620 | * |
||
| 621 | * @param $value float |
||
| 622 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 623 | */ |
||
| 624 | 5 | public function setInsetRight($value = 9.6) |
|
| 630 | |||
| 631 | /** |
||
| 632 | * Get top inset |
||
| 633 | * |
||
| 634 | * @return float |
||
| 635 | */ |
||
| 636 | 27 | public function getInsetTop() |
|
| 640 | |||
| 641 | /** |
||
| 642 | * Set top inset |
||
| 643 | * |
||
| 644 | * @param $value float |
||
| 645 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 646 | */ |
||
| 647 | 5 | public function setInsetTop($value = 4.8) |
|
| 653 | |||
| 654 | /** |
||
| 655 | * Set horizontal auto shrink |
||
| 656 | * @param bool $value |
||
| 657 | */ |
||
| 658 | 2 | public function setAutoShrinkHorizontal($value = null) |
|
| 665 | |||
| 666 | /** |
||
| 667 | * Get horizontal auto shrink |
||
| 668 | * @return bool |
||
| 669 | */ |
||
| 670 | 15 | public function hasAutoShrinkHorizontal() |
|
| 674 | |||
| 675 | /** |
||
| 676 | * Set vertical auto shrink |
||
| 677 | * @param bool $value |
||
| 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 | 2 | public function getHashCode() |
|
| 710 | |||
| 711 | public function setBodyPr(\DOMElement $element) |
||
| 715 | |||
| 716 | public function getBodyPr() |
||
| 720 | |||
| 721 | public function setListStyle(\DOMElement $element) |
||
| 725 | |||
| 726 | 26 | public function getListStyle() |
|
| 730 | } |
||
| 731 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: