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 | 57 | public function __construct() |
|
| 163 | { |
||
| 164 | // Initialise variables |
||
| 165 | 57 | $this->richTextParagraphs = array( |
|
| 166 | 57 | new Paragraph() |
|
| 167 | ); |
||
| 168 | 57 | $this->activeParagraph = 0; |
|
| 169 | |||
| 170 | // Initialize parent |
||
| 171 | 57 | parent::__construct(); |
|
| 172 | 57 | } |
|
| 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 | 16 | 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 | */ |
||
| 233 | 10 | public function createParagraph() |
|
| 234 | { |
||
| 235 | 10 | $numParagraphs = count($this->richTextParagraphs); |
|
| 236 | 10 | if ($numParagraphs > 0) { |
|
| 237 | 9 | $alignment = clone $this->getActiveParagraph()->getAlignment(); |
|
| 238 | 9 | $font = clone $this->getActiveParagraph()->getFont(); |
|
| 239 | 9 | $bulletStyle = clone $this->getActiveParagraph()->getBulletStyle(); |
|
| 240 | } |
||
| 241 | |||
| 242 | 10 | $this->richTextParagraphs[] = new Paragraph(); |
|
| 243 | 10 | $this->activeParagraph = count($this->richTextParagraphs) - 1; |
|
| 244 | |||
| 245 | 10 | if (isset($alignment)) { |
|
| 246 | 9 | $this->getActiveParagraph()->setAlignment($alignment); |
|
| 247 | } |
||
| 248 | 10 | if (isset($font)) { |
|
| 249 | 9 | $this->getActiveParagraph()->setFont($font); |
|
| 250 | } |
||
| 251 | 10 | if (isset($bulletStyle)) { |
|
| 252 | 9 | $this->getActiveParagraph()->setBulletStyle($bulletStyle); |
|
| 253 | } |
||
| 254 | 10 | return $this->getActiveParagraph(); |
|
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Add text |
||
| 259 | * |
||
| 260 | * @param \PhpOffice\PhpPresentation\Shape\RichText\TextElementInterface $pText Rich text element |
||
| 261 | * @throws \Exception |
||
| 262 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 263 | */ |
||
| 264 | 1 | public function addText(TextElementInterface $pText = null) |
|
| 270 | |||
| 271 | /** |
||
| 272 | * Create text (can not be formatted !) |
||
| 273 | * |
||
| 274 | * @param string $pText Text |
||
| 275 | * @return \PhpOffice\PhpPresentation\Shape\RichText\TextElement |
||
| 276 | * @throws \Exception |
||
| 277 | */ |
||
| 278 | 2 | public function createText($pText = '') |
|
| 282 | |||
| 283 | /** |
||
| 284 | * Create break |
||
| 285 | * |
||
| 286 | * @return \PhpOffice\PhpPresentation\Shape\RichText\BreakElement |
||
| 287 | * @throws \Exception |
||
| 288 | */ |
||
| 289 | 3 | public function createBreak() |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Create text run (can be formatted) |
||
| 296 | * |
||
| 297 | * @param string $pText Text |
||
| 298 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Run |
||
| 299 | * @throws \Exception |
||
| 300 | */ |
||
| 301 | 13 | public function createTextRun($pText = '') |
|
| 305 | |||
| 306 | /** |
||
| 307 | * Get plain text |
||
| 308 | * |
||
| 309 | * @return string |
||
| 310 | */ |
||
| 311 | 1 | public function getPlainText() |
|
| 312 | { |
||
| 313 | // Return value |
||
| 314 | 1 | $returnValue = ''; |
|
| 315 | |||
| 316 | // Loop trough all \PhpOffice\PhpPresentation\Shape\RichText\Paragraph |
||
| 317 | 1 | foreach ($this->richTextParagraphs as $p) { |
|
| 318 | 1 | $returnValue .= $p->getPlainText(); |
|
| 319 | } |
||
| 320 | |||
| 321 | // Return |
||
| 322 | 1 | return $returnValue; |
|
| 323 | } |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Convert to string |
||
| 327 | * |
||
| 328 | * @return string |
||
| 329 | */ |
||
| 330 | 1 | public function __toString() |
|
| 334 | |||
| 335 | /** |
||
| 336 | * Get paragraphs |
||
| 337 | * |
||
| 338 | * @return \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] |
||
| 339 | */ |
||
| 340 | 25 | public function getParagraphs() |
|
| 344 | |||
| 345 | /** |
||
| 346 | * Set paragraphs |
||
| 347 | * |
||
| 348 | * @param \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] $paragraphs Array of paragraphs |
||
| 349 | * @throws \Exception |
||
| 350 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 351 | */ |
||
| 352 | 9 | public function setParagraphs($paragraphs = null) |
|
| 353 | { |
||
| 354 | 9 | if (!is_array($paragraphs)) { |
|
| 355 | 1 | throw new \Exception("Invalid \PhpOffice\PhpPresentation\Shape\RichText\Paragraph[] array passed."); |
|
| 356 | } |
||
| 357 | |||
| 358 | 8 | $this->richTextParagraphs = $paragraphs; |
|
| 359 | 8 | $this->activeParagraph = count($this->richTextParagraphs) - 1; |
|
| 360 | 8 | return $this; |
|
| 361 | } |
||
| 362 | |||
| 363 | /** |
||
| 364 | * Get text wrapping |
||
| 365 | * |
||
| 366 | * @return string |
||
| 367 | */ |
||
| 368 | 1 | public function getWrap() |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Set text wrapping |
||
| 375 | * |
||
| 376 | * @param $value string |
||
| 377 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 378 | */ |
||
| 379 | 1 | public function setWrap($value = self::WRAP_SQUARE) |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Get autofit |
||
| 388 | * |
||
| 389 | * @return string |
||
| 390 | */ |
||
| 391 | 1 | public function getAutoFit() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Get pourcentage of fontScale |
||
| 398 | * |
||
| 399 | * @return float |
||
| 400 | */ |
||
| 401 | public function getFontScale() |
||
| 402 | { |
||
| 403 | return $this->fontScale; |
||
| 404 | } |
||
| 405 | |||
| 406 | /** |
||
| 407 | * Get pourcentage of the line space reduction |
||
| 408 | * |
||
| 409 | * @return float |
||
| 410 | */ |
||
| 411 | public function getLineSpaceReduction() |
||
| 412 | { |
||
| 413 | return $this->lnSpcReduction; |
||
| 414 | } |
||
| 415 | |||
| 416 | /** |
||
| 417 | * Set autofit |
||
| 418 | * |
||
| 419 | * @param $value string |
||
| 420 | * @param $fontScale float |
||
| 421 | * @param $lnSpcReduction float |
||
| 422 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 423 | */ |
||
| 424 | 1 | public function setAutoFit($value = self::AUTOFIT_DEFAULT, $fontScale = null, $lnSpcReduction = null) |
|
| 425 | { |
||
| 426 | 1 | $this->autoFit = $value; |
|
| 427 | |||
| 428 | 1 | if (!is_null($fontScale)) { |
|
| 429 | $this->fontScale = $fontScale; |
||
| 430 | } |
||
| 431 | |||
| 432 | 1 | if (!is_null($lnSpcReduction)) { |
|
| 433 | $this->lnSpcReduction = $lnSpcReduction; |
||
| 434 | } |
||
| 435 | |||
| 436 | 1 | return $this; |
|
| 437 | } |
||
| 438 | |||
| 439 | /** |
||
| 440 | * Get horizontal overflow |
||
| 441 | * |
||
| 442 | * @return string |
||
| 443 | */ |
||
| 444 | 1 | public function getHorizontalOverflow() |
|
| 448 | |||
| 449 | /** |
||
| 450 | * Set horizontal overflow |
||
| 451 | * |
||
| 452 | * @param $value string |
||
| 453 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 454 | */ |
||
| 455 | 1 | public function setHorizontalOverflow($value = self::OVERFLOW_OVERFLOW) |
|
| 461 | |||
| 462 | /** |
||
| 463 | * Get vertical overflow |
||
| 464 | * |
||
| 465 | * @return string |
||
| 466 | */ |
||
| 467 | 1 | public function getVerticalOverflow() |
|
| 471 | |||
| 472 | /** |
||
| 473 | * Set vertical overflow |
||
| 474 | * |
||
| 475 | * @param $value string |
||
| 476 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 477 | */ |
||
| 478 | 1 | public function setVerticalOverflow($value = self::OVERFLOW_OVERFLOW) |
|
| 484 | |||
| 485 | /** |
||
| 486 | * Get upright |
||
| 487 | * |
||
| 488 | * @return boolean |
||
| 489 | */ |
||
| 490 | 1 | public function isUpright() |
|
| 494 | |||
| 495 | /** |
||
| 496 | * Set vertical |
||
| 497 | * |
||
| 498 | * @param $value boolean |
||
| 499 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 500 | */ |
||
| 501 | 1 | public function setUpright($value = false) |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Get vertical |
||
| 510 | * |
||
| 511 | * @return boolean |
||
| 512 | */ |
||
| 513 | 1 | public function isVertical() |
|
| 517 | |||
| 518 | /** |
||
| 519 | * Set vertical |
||
| 520 | * |
||
| 521 | * @param $value boolean |
||
| 522 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 523 | */ |
||
| 524 | 1 | public function setVertical($value = false) |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Get columns |
||
| 533 | * |
||
| 534 | * @return int |
||
| 535 | */ |
||
| 536 | 1 | public function getColumns() |
|
| 540 | |||
| 541 | /** |
||
| 542 | * Set columns |
||
| 543 | * |
||
| 544 | * @param $value int |
||
| 545 | * @throws \Exception |
||
| 546 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 547 | */ |
||
| 548 | 2 | public function setColumns($value = 1) |
|
| 558 | |||
| 559 | /** |
||
| 560 | * Get bottom inset |
||
| 561 | * |
||
| 562 | * @return float |
||
| 563 | */ |
||
| 564 | 1 | public function getInsetBottom() |
|
| 568 | |||
| 569 | /** |
||
| 570 | * Set bottom inset |
||
| 571 | * |
||
| 572 | * @param $value float |
||
| 573 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 574 | */ |
||
| 575 | 4 | public function setInsetBottom($value = 4.8) |
|
| 581 | |||
| 582 | /** |
||
| 583 | * Get left inset |
||
| 584 | * |
||
| 585 | * @return float |
||
| 586 | */ |
||
| 587 | 1 | public function getInsetLeft() |
|
| 591 | |||
| 592 | /** |
||
| 593 | * Set left inset |
||
| 594 | * |
||
| 595 | * @param $value float |
||
| 596 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 597 | */ |
||
| 598 | 4 | public function setInsetLeft($value = 9.6) |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Get right inset |
||
| 607 | * |
||
| 608 | * @return float |
||
| 609 | */ |
||
| 610 | 1 | public function getInsetRight() |
|
| 614 | |||
| 615 | /** |
||
| 616 | * Set left inset |
||
| 617 | * |
||
| 618 | * @param $value float |
||
| 619 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 620 | */ |
||
| 621 | 4 | public function setInsetRight($value = 9.6) |
|
| 627 | |||
| 628 | /** |
||
| 629 | * Get top inset |
||
| 630 | * |
||
| 631 | * @return float |
||
| 632 | */ |
||
| 633 | 1 | public function getInsetTop() |
|
| 637 | |||
| 638 | /** |
||
| 639 | * Set top inset |
||
| 640 | * |
||
| 641 | * @param $value float |
||
| 642 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 643 | */ |
||
| 644 | 4 | public function setInsetTop($value = 4.8) |
|
| 650 | |||
| 651 | /** |
||
| 652 | * Set horizontal auto shrink |
||
| 653 | * @param bool $value |
||
| 654 | */ |
||
| 655 | 2 | public function setAutoShrinkHorizontal($value = null) |
|
| 656 | { |
||
| 657 | 2 | if (is_bool($value)) { |
|
| 658 | 2 | $this->autoShrinkHorizontal = $value; |
|
| 659 | } |
||
| 660 | 2 | return $this; |
|
| 661 | } |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Get horizontal auto shrink |
||
| 665 | * @return bool |
||
| 666 | */ |
||
| 667 | 15 | public function hasAutoShrinkHorizontal() |
|
| 671 | |||
| 672 | /** |
||
| 673 | * Set vertical auto shrink |
||
| 674 | * @param bool $value |
||
| 675 | */ |
||
| 676 | 2 | public function setAutoShrinkVertical($value = null) |
|
| 677 | { |
||
| 678 | 2 | if (is_bool($value)) { |
|
| 679 | 2 | $this->autoShrinkVertical = $value; |
|
| 680 | } |
||
| 681 | 2 | return $this; |
|
| 682 | } |
||
| 683 | |||
| 684 | /** |
||
| 685 | * Set vertical auto shrink |
||
| 686 | * @return bool |
||
| 687 | */ |
||
| 688 | 15 | public function hasAutoShrinkVertical() |
|
| 692 | |||
| 693 | /** |
||
| 694 | * Get hash code |
||
| 695 | * |
||
| 696 | * @return string Hash code |
||
| 697 | */ |
||
| 698 | 1 | public function getHashCode() |
|
| 707 | } |
||
| 708 |