Complex classes like Slide 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 Slide, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 35 | class Slide implements ComparableInterface, ShapeContainerInterface |
||
| 36 | { |
||
| 37 | /** |
||
| 38 | * The slide is shown in presentation |
||
| 39 | * @var bool |
||
| 40 | */ |
||
| 41 | protected $isVisible = true; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Parent presentation |
||
| 45 | * |
||
| 46 | * @var PhpPresentation |
||
| 47 | */ |
||
| 48 | private $parent; |
||
| 49 | |||
| 50 | /** |
||
| 51 | * Collection of shapes |
||
| 52 | * |
||
| 53 | * @var \ArrayObject|\PhpOffice\PhpPresentation\AbstractShape[] |
||
| 54 | */ |
||
| 55 | private $shapeCollection = null; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Slide identifier |
||
| 59 | * |
||
| 60 | * @var string |
||
| 61 | */ |
||
| 62 | private $identifier; |
||
| 63 | |||
| 64 | /** |
||
| 65 | * Slide layout |
||
| 66 | * |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | private $slideLayout; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * Slide master id |
||
| 73 | * |
||
| 74 | * @var integer |
||
| 75 | */ |
||
| 76 | private $slideMasterId = 1; |
||
| 77 | |||
| 78 | /** |
||
| 79 | * |
||
| 80 | * @var \PhpOffice\PhpPresentation\Slide\Note |
||
| 81 | */ |
||
| 82 | private $slideNote; |
||
| 83 | |||
| 84 | /** |
||
| 85 | * |
||
| 86 | * @var \PhpOffice\PhpPresentation\Slide\Transition |
||
| 87 | */ |
||
| 88 | private $slideTransition; |
||
| 89 | |||
| 90 | /** |
||
| 91 | * |
||
| 92 | * @var \PhpOffice\PhpPresentation\Slide\Animation |
||
| 93 | */ |
||
| 94 | private $animations = array(); |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Hash index |
||
| 98 | * |
||
| 99 | * @var string |
||
| 100 | */ |
||
| 101 | private $hashIndex; |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Offset X |
||
| 105 | * |
||
| 106 | * @var int |
||
| 107 | */ |
||
| 108 | protected $offsetX; |
||
| 109 | |||
| 110 | /** |
||
| 111 | * Offset Y |
||
| 112 | * |
||
| 113 | * @var int |
||
| 114 | */ |
||
| 115 | protected $offsetY; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Extent X |
||
| 119 | * |
||
| 120 | * @var int |
||
| 121 | */ |
||
| 122 | protected $extentX; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Extent Y |
||
| 126 | * |
||
| 127 | * @var int |
||
| 128 | */ |
||
| 129 | protected $extentY; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Name of the title |
||
| 133 | * |
||
| 134 | * @var string |
||
| 135 | */ |
||
| 136 | protected $name; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Background of the slide |
||
| 140 | * |
||
| 141 | * @var AbstractBackground |
||
| 142 | */ |
||
| 143 | protected $background; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Create a new slide |
||
| 147 | * |
||
| 148 | * @param PhpPresentation $pParent |
||
| 149 | */ |
||
| 150 | 204 | public function __construct(PhpPresentation $pParent = null) |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Get collection of shapes |
||
| 166 | * |
||
| 167 | * @return \ArrayObject|\PhpOffice\PhpPresentation\AbstractShape[] |
||
| 168 | */ |
||
| 169 | 173 | public function getShapeCollection() |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Add shape to slide |
||
| 176 | * |
||
| 177 | * @param \PhpOffice\PhpPresentation\AbstractShape $shape |
||
| 178 | * @return \PhpOffice\PhpPresentation\AbstractShape |
||
| 179 | */ |
||
| 180 | 139 | public function addShape(AbstractShape $shape) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Create rich text shape |
||
| 189 | * |
||
| 190 | * @return \PhpOffice\PhpPresentation\Shape\RichText |
||
| 191 | */ |
||
| 192 | 41 | public function createRichTextShape() |
|
| 199 | |||
| 200 | /** |
||
| 201 | * Create line shape |
||
| 202 | * |
||
| 203 | * @param int $fromX Starting point x offset |
||
| 204 | * @param int $fromY Starting point y offset |
||
| 205 | * @param int $toX Ending point x offset |
||
| 206 | * @param int $toY Ending point y offset |
||
| 207 | * @return \PhpOffice\PhpPresentation\Shape\Line |
||
| 208 | */ |
||
| 209 | 2 | public function createLineShape($fromX, $fromY, $toX, $toY) |
|
| 216 | |||
| 217 | /** |
||
| 218 | * Create chart shape |
||
| 219 | * |
||
| 220 | * @return \PhpOffice\PhpPresentation\Shape\Chart |
||
| 221 | */ |
||
| 222 | 46 | public function createChartShape() |
|
| 229 | |||
| 230 | /** |
||
| 231 | * Create drawing shape |
||
| 232 | * |
||
| 233 | * @return \PhpOffice\PhpPresentation\Shape\Drawing\File |
||
| 234 | */ |
||
| 235 | 13 | public function createDrawingShape() |
|
| 242 | |||
| 243 | /** |
||
| 244 | * Create table shape |
||
| 245 | * |
||
| 246 | * @param int $columns Number of columns |
||
| 247 | * @return \PhpOffice\PhpPresentation\Shape\Table |
||
| 248 | */ |
||
| 249 | 16 | public function createTableShape($columns = 1) |
|
| 256 | |||
| 257 | /** |
||
| 258 | * Creates a group within this slide |
||
| 259 | * |
||
| 260 | * @return \PhpOffice\PhpPresentation\Shape\Group |
||
| 261 | */ |
||
| 262 | 2 | public function createGroup() |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Get parent |
||
| 272 | * |
||
| 273 | * @return PhpPresentation |
||
| 274 | */ |
||
| 275 | 95 | public function getParent() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Re-bind parent |
||
| 282 | * |
||
| 283 | * @param \PhpOffice\PhpPresentation\PhpPresentation $parent |
||
| 284 | * @return \PhpOffice\PhpPresentation\Slide |
||
| 285 | */ |
||
| 286 | 2 | public function rebindParent(PhpPresentation $parent) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Get slide layout |
||
| 296 | * |
||
| 297 | * @return string |
||
| 298 | */ |
||
| 299 | 95 | public function getSlideLayout() |
|
| 303 | |||
| 304 | /** |
||
| 305 | * Set slide layout |
||
| 306 | * |
||
| 307 | * @param string $layout |
||
| 308 | * @return \PhpOffice\PhpPresentation\Slide |
||
| 309 | */ |
||
| 310 | 4 | public function setSlideLayout($layout = Layout::BLANK) |
|
| 316 | |||
| 317 | /** |
||
| 318 | * Get slide master id |
||
| 319 | * |
||
| 320 | * @return int |
||
| 321 | */ |
||
| 322 | 98 | public function getSlideMasterId() |
|
| 326 | |||
| 327 | /** |
||
| 328 | * Set slide master id |
||
| 329 | * |
||
| 330 | * @param int $masterId |
||
| 331 | * @return \PhpOffice\PhpPresentation\Slide |
||
| 332 | */ |
||
| 333 | 1 | public function setSlideMasterId($masterId = 1) |
|
| 339 | |||
| 340 | /** |
||
| 341 | * Get hash code |
||
| 342 | * |
||
| 343 | * @return string Hash code |
||
| 344 | */ |
||
| 345 | 134 | public function getHashCode() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Get hash index |
||
| 352 | * |
||
| 353 | * Note that this index may vary during script execution! Only reliable moment is |
||
| 354 | * while doing a write of a workbook and when changes are not allowed. |
||
| 355 | * |
||
| 356 | * @return string Hash index |
||
| 357 | */ |
||
| 358 | 4 | public function getHashIndex() |
|
| 362 | |||
| 363 | /** |
||
| 364 | * Set hash index |
||
| 365 | * |
||
| 366 | * Note that this index may vary during script execution! Only reliable moment is |
||
| 367 | * while doing a write of a workbook and when changes are not allowed. |
||
| 368 | * |
||
| 369 | * @param string $value Hash index |
||
| 370 | */ |
||
| 371 | 4 | public function setHashIndex($value) |
|
| 375 | |||
| 376 | /** |
||
| 377 | * Copy slide (!= clone!) |
||
| 378 | * |
||
| 379 | * @return \PhpOffice\PhpPresentation\Slide |
||
| 380 | */ |
||
| 381 | 1 | public function copy() |
|
| 387 | |||
| 388 | /** |
||
| 389 | * Get X Offset |
||
| 390 | * |
||
| 391 | * @return int |
||
| 392 | */ |
||
| 393 | 95 | public function getOffsetX() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Get Y Offset |
||
| 405 | * |
||
| 406 | * @return int |
||
| 407 | */ |
||
| 408 | 95 | public function getOffsetY() |
|
| 417 | |||
| 418 | /** |
||
| 419 | * Get X Extent |
||
| 420 | * |
||
| 421 | * @return int |
||
| 422 | */ |
||
| 423 | 95 | public function getExtentX() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Get Y Extent |
||
| 435 | * |
||
| 436 | * @return int |
||
| 437 | */ |
||
| 438 | 95 | public function getExtentY() |
|
| 447 | |||
| 448 | /** |
||
| 449 | * |
||
| 450 | * @return \PhpOffice\PhpPresentation\Slide\Note |
||
| 451 | */ |
||
| 452 | 156 | public function getNote() |
|
| 459 | |||
| 460 | /** |
||
| 461 | * |
||
| 462 | * @param \PhpOffice\PhpPresentation\Slide\Note $note |
||
| 463 | * @return \PhpOffice\PhpPresentation\Slide |
||
| 464 | */ |
||
| 465 | 157 | public function setNote(Note $note = null) |
|
| 472 | |||
| 473 | /** |
||
| 474 | * |
||
| 475 | * @return \PhpOffice\PhpPresentation\Slide\Transition |
||
| 476 | */ |
||
| 477 | 154 | public function getTransition() |
|
| 481 | |||
| 482 | /** |
||
| 483 | * |
||
| 484 | * @param \PhpOffice\PhpPresentation\Slide\Transition $transition |
||
| 485 | * @return \PhpOffice\PhpPresentation\Slide |
||
| 486 | */ |
||
| 487 | 3 | public function setTransition(Transition $transition = null) |
|
| 493 | |||
| 494 | /** |
||
| 495 | * Get the name of the slide |
||
| 496 | * @return string |
||
| 497 | */ |
||
| 498 | 61 | public function getName() |
|
| 502 | |||
| 503 | /** |
||
| 504 | * Set the name of the slide |
||
| 505 | * @param string $name |
||
| 506 | * @return $this |
||
| 507 | */ |
||
| 508 | 5 | public function setName($name = null) |
|
| 513 | |||
| 514 | /** |
||
| 515 | * @return AbstractBackground |
||
| 516 | */ |
||
| 517 | 154 | public function getBackground() |
|
| 521 | |||
| 522 | /** |
||
| 523 | * @param AbstractBackground $background |
||
| 524 | * @return $this |
||
| 525 | */ |
||
| 526 | 5 | public function setBackground(AbstractBackground $background = null) |
|
| 531 | |||
| 532 | /** |
||
| 533 | * @return boolean |
||
| 534 | */ |
||
| 535 | 154 | public function isVisible() |
|
| 539 | |||
| 540 | /** |
||
| 541 | * @param boolean $value |
||
| 542 | * @return Slide |
||
| 543 | */ |
||
| 544 | 3 | public function setIsVisible($value = true) |
|
| 549 | |||
| 550 | /** |
||
| 551 | * Add an animation to the slide |
||
| 552 | * |
||
| 553 | * @param \PhpOffice\PhpPresentation\Slide\Animation |
||
| 554 | * @return \PhpOffice\PhpPresentation\Slide\Animation |
||
| 555 | */ |
||
| 556 | public function addAnimation($animation) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Get collection of animations |
||
| 564 | * |
||
| 565 | * @return \PhpOffice\PhpPresentation\Slide\Animation |
||
| 566 | */ |
||
| 567 | |||
| 568 | 94 | public function getAnimations() |
|
| 572 | } |
||
| 573 |