Complex classes like Media 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 Media, and based on these observations, apply Extract Interface, too.
| 1 | <?php declare(strict_types=1); |
||
| 49 | class Media implements MediaInterface |
||
| 50 | { |
||
| 51 | /** |
||
| 52 | * @var string |
||
| 53 | */ |
||
| 54 | protected $nodeName; |
||
| 55 | |||
| 56 | /** |
||
| 57 | * @var string |
||
| 58 | */ |
||
| 59 | protected $type; |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @var string |
||
| 63 | */ |
||
| 64 | protected $url; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * @var string |
||
| 68 | */ |
||
| 69 | protected $length; |
||
| 70 | |||
| 71 | /** |
||
| 72 | * @var string |
||
| 73 | */ |
||
| 74 | protected $title; |
||
| 75 | |||
| 76 | /** |
||
| 77 | * @var string |
||
| 78 | */ |
||
| 79 | protected $description; |
||
| 80 | |||
| 81 | /** |
||
| 82 | * @var string |
||
| 83 | */ |
||
| 84 | protected $rights; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * @var int |
||
| 88 | */ |
||
| 89 | protected $titleType; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * @var int |
||
| 93 | */ |
||
| 94 | protected $descriptionType; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * @var array |
||
| 98 | */ |
||
| 99 | protected $keywords = array(); |
||
| 100 | |||
| 101 | /** |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | protected $comments = array(); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * @var array |
||
| 108 | */ |
||
| 109 | protected $responses = array(); |
||
| 110 | |||
| 111 | /** |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | protected $backlinks = array(); |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @var array |
||
| 118 | */ |
||
| 119 | protected $credits = array(); |
||
| 120 | |||
| 121 | /** |
||
| 122 | * @var array |
||
| 123 | */ |
||
| 124 | protected $texts = array(); |
||
| 125 | |||
| 126 | /** |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | protected $prices = array(); |
||
| 130 | |||
| 131 | /** |
||
| 132 | * @var array |
||
| 133 | */ |
||
| 134 | protected $subTitles = array(); |
||
| 135 | |||
| 136 | /** |
||
| 137 | * @var array |
||
| 138 | */ |
||
| 139 | protected $scenes = array(); |
||
| 140 | |||
| 141 | /** |
||
| 142 | * @var MediaContentInterface |
||
| 143 | */ |
||
| 144 | protected $content; |
||
| 145 | |||
| 146 | /** |
||
| 147 | * @var MediaThumbnailInterface |
||
| 148 | */ |
||
| 149 | protected $thumbnail; |
||
| 150 | |||
| 151 | /** |
||
| 152 | * @var MediaCategoryInterface |
||
| 153 | */ |
||
| 154 | protected $category; |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @var MediaHashInterface |
||
| 158 | */ |
||
| 159 | protected $hash; |
||
| 160 | |||
| 161 | /** |
||
| 162 | * @var MediaHashInterface |
||
| 163 | */ |
||
| 164 | protected $embed; |
||
| 165 | |||
| 166 | /** |
||
| 167 | * @var MediaLicenseInterface |
||
| 168 | */ |
||
| 169 | protected $license; |
||
| 170 | |||
| 171 | /** |
||
| 172 | * @var MediaCommunityInterface |
||
| 173 | */ |
||
| 174 | protected $community; |
||
| 175 | |||
| 176 | /** |
||
| 177 | * @var MediaRestrictionInterface |
||
| 178 | */ |
||
| 179 | protected $restriction; |
||
| 180 | |||
| 181 | /** |
||
| 182 | * @var MediaRatingInterface |
||
| 183 | */ |
||
| 184 | protected $rating; |
||
| 185 | |||
| 186 | /** |
||
| 187 | * @var MediaCopyrightInterface |
||
| 188 | */ |
||
| 189 | protected $copyright; |
||
| 190 | |||
| 191 | /** |
||
| 192 | * @return string |
||
| 193 | */ |
||
| 194 | public function getNodeName() : string |
||
| 198 | |||
| 199 | /** |
||
| 200 | * @param string $nodeName |
||
| 201 | * @return MediaInterface |
||
| 202 | */ |
||
| 203 | 51 | public function setNodeName(string $nodeName) : MediaInterface |
|
| 209 | |||
| 210 | /** |
||
| 211 | * @return string |
||
| 212 | */ |
||
| 213 | 7 | public function getType() : ? string |
|
| 217 | |||
| 218 | /** |
||
| 219 | * @param string $type |
||
| 220 | * @return MediaInterface |
||
| 221 | */ |
||
| 222 | 54 | public function setType(?string $type) : MediaInterface |
|
| 228 | |||
| 229 | /** |
||
| 230 | * @return string |
||
| 231 | */ |
||
| 232 | 10 | public function getUrl() : ? string |
|
| 236 | |||
| 237 | /** |
||
| 238 | * @param string $url |
||
| 239 | * @return MediaInterface |
||
| 240 | */ |
||
| 241 | 53 | public function setUrl(?string $url) : MediaInterface |
|
| 247 | |||
| 248 | /** |
||
| 249 | * @return string |
||
| 250 | */ |
||
| 251 | 3 | public function getLength() : ? string |
|
| 255 | |||
| 256 | /** |
||
| 257 | * @param string $length |
||
| 258 | * @return MediaInterface |
||
| 259 | */ |
||
| 260 | 6 | public function setLength(?string $length) : MediaInterface |
|
| 266 | |||
| 267 | /** |
||
| 268 | * @return string |
||
| 269 | */ |
||
| 270 | 4 | public function getTitle() : ? string |
|
| 274 | |||
| 275 | /** |
||
| 276 | * @param string $title |
||
| 277 | * @return MediaInterface |
||
| 278 | */ |
||
| 279 | 4 | public function setTitle(?string $title) : MediaInterface |
|
| 285 | |||
| 286 | /** |
||
| 287 | * @return string |
||
| 288 | */ |
||
| 289 | 5 | public function getDescription() : ? string |
|
| 293 | |||
| 294 | /** |
||
| 295 | * @param string $description |
||
| 296 | * @return MediaInterface |
||
| 297 | */ |
||
| 298 | 5 | public function setDescription(?string $description) : MediaInterface |
|
| 304 | |||
| 305 | /** |
||
| 306 | * @return string |
||
| 307 | */ |
||
| 308 | 1 | public function getRights() : ?int |
|
| 312 | |||
| 313 | /** |
||
| 314 | * @param int $rights |
||
| 315 | * @return MediaInterface |
||
| 316 | */ |
||
| 317 | 1 | public function setRights(int $rights) : MediaInterface |
|
| 323 | |||
| 324 | /** |
||
| 325 | * @return int |
||
| 326 | */ |
||
| 327 | 2 | public function getTitleType() : ?int |
|
| 331 | |||
| 332 | /** |
||
| 333 | * @param int $titleType |
||
| 334 | * @return MediaInterface |
||
| 335 | */ |
||
| 336 | 4 | public function setTitleType(?int $titleType) : MediaInterface |
|
| 342 | |||
| 343 | |||
| 344 | /** |
||
| 345 | * @return int |
||
| 346 | */ |
||
| 347 | 2 | public function getDescriptionType() : ?int |
|
| 351 | |||
| 352 | /** |
||
| 353 | * @param int $descriptionType |
||
| 354 | * @return MediaInterface |
||
| 355 | */ |
||
| 356 | 5 | public function setDescriptionType(?int $descriptionType) : MediaInterface |
|
| 362 | |||
| 363 | /** |
||
| 364 | * @return array |
||
| 365 | */ |
||
| 366 | 1 | public function getKeywords() : array |
|
| 370 | |||
| 371 | /** |
||
| 372 | * @param array $keywords |
||
| 373 | * @return MediaInterface |
||
| 374 | */ |
||
| 375 | 1 | public function setKeywords(array $keywords) : MediaInterface |
|
| 381 | |||
| 382 | /** |
||
| 383 | * @return array |
||
| 384 | */ |
||
| 385 | 2 | public function getComments() : array |
|
| 389 | |||
| 390 | /** |
||
| 391 | * @param array $comments |
||
| 392 | * @return MediaInterface |
||
| 393 | */ |
||
| 394 | 2 | public function setComments(array $comments) : MediaInterface |
|
| 400 | |||
| 401 | /** |
||
| 402 | * @return array |
||
| 403 | */ |
||
| 404 | 2 | public function getResponses() : array |
|
| 408 | |||
| 409 | /** |
||
| 410 | * @param array $responses |
||
| 411 | * @return MediaInterface |
||
| 412 | */ |
||
| 413 | 2 | public function setResponses(array $responses) : MediaInterface |
|
| 419 | |||
| 420 | /** |
||
| 421 | * @return array |
||
| 422 | */ |
||
| 423 | 2 | public function getBacklinks() : array |
|
| 427 | |||
| 428 | /** |
||
| 429 | * @param array $backlinks |
||
| 430 | * @return MediaInterface |
||
| 431 | */ |
||
| 432 | 2 | public function setBacklinks(array $backlinks) : MediaInterface |
|
| 438 | |||
| 439 | /** |
||
| 440 | * @return array |
||
| 441 | */ |
||
| 442 | 2 | public function getCredits() : array |
|
| 446 | |||
| 447 | /** |
||
| 448 | * @param array $credits |
||
| 449 | * @return MediaInterface |
||
| 450 | */ |
||
| 451 | 2 | public function setCredits(array $credits) : MediaInterface |
|
| 457 | |||
| 458 | /** |
||
| 459 | * @return array |
||
| 460 | */ |
||
| 461 | 2 | public function getTexts() : array |
|
| 465 | |||
| 466 | /** |
||
| 467 | * @param array $texts |
||
| 468 | * @return MediaInterface |
||
| 469 | */ |
||
| 470 | 2 | public function setTexts(array $texts) : MediaInterface |
|
| 476 | |||
| 477 | /** |
||
| 478 | * @return array |
||
| 479 | */ |
||
| 480 | 2 | public function getPrices() : array |
|
| 484 | |||
| 485 | /** |
||
| 486 | * @param array $prices |
||
| 487 | * @return MediaInterface |
||
| 488 | */ |
||
| 489 | 2 | public function setPrices(array $prices) : MediaInterface |
|
| 495 | |||
| 496 | /** |
||
| 497 | * @return array |
||
| 498 | */ |
||
| 499 | 1 | public function getSubTitles() : array |
|
| 503 | |||
| 504 | /** |
||
| 505 | * @param array $subTitles |
||
| 506 | * @return MediaInterface |
||
| 507 | */ |
||
| 508 | 1 | public function setSubTitles(array $subTitles) : MediaInterface |
|
| 514 | |||
| 515 | /** |
||
| 516 | * @return array |
||
| 517 | */ |
||
| 518 | 1 | public function getScenes() : array |
|
| 522 | |||
| 523 | /** |
||
| 524 | * @param array $scenes |
||
| 525 | * @return MediaInterface |
||
| 526 | */ |
||
| 527 | 1 | public function setScenes(array $scenes) : MediaInterface |
|
| 533 | |||
| 534 | /** |
||
| 535 | * @return MediaContentInterface |
||
| 536 | */ |
||
| 537 | 2 | public function getContent() : MediaContentInterface |
|
| 541 | |||
| 542 | /** |
||
| 543 | * @param MediaContentInterface $content |
||
| 544 | * @return MediaInterface |
||
| 545 | */ |
||
| 546 | 47 | public function setContent(MediaContentInterface $content) : MediaInterface |
|
| 552 | |||
| 553 | /** |
||
| 554 | * @return MediaThumbnailInterface |
||
| 555 | */ |
||
| 556 | 3 | public function getThumbnail() : MediaThumbnailInterface |
|
| 560 | |||
| 561 | /** |
||
| 562 | * @param MediaThumbnailInterface $thumbnail |
||
| 563 | * @return MediaInterface |
||
| 564 | */ |
||
| 565 | 3 | public function setThumbnail(MediaThumbnailInterface $thumbnail) : MediaInterface |
|
| 571 | |||
| 572 | /** |
||
| 573 | * @return MediaCategoryInterface |
||
| 574 | */ |
||
| 575 | 2 | public function getCategory() : MediaCategoryInterface |
|
| 579 | |||
| 580 | /** |
||
| 581 | * @param MediaCategoryInterface $category |
||
| 582 | * @return MediaInterface |
||
| 583 | */ |
||
| 584 | 2 | public function setCategory(MediaCategoryInterface $category) : MediaInterface |
|
| 590 | |||
| 591 | /** |
||
| 592 | * @return MediaPlayerInterface |
||
| 593 | */ |
||
| 594 | 2 | public function getPlayer() : MediaPlayerInterface |
|
| 598 | |||
| 599 | /** |
||
| 600 | * @param MediaPlayerInterface $player |
||
| 601 | * @return MediaInterface |
||
| 602 | */ |
||
| 603 | 2 | public function setPlayer(MediaPlayerInterface $player) : MediaInterface |
|
| 609 | |||
| 610 | /** |
||
| 611 | * @return MediaHashInterface |
||
| 612 | */ |
||
| 613 | 2 | public function getHash() : MediaHashInterface |
|
| 617 | |||
| 618 | /** |
||
| 619 | * @param MediaHashInterface $hash |
||
| 620 | * @return MediaInterface |
||
| 621 | */ |
||
| 622 | 2 | public function setHash(MediaHashInterface $hash) : MediaInterface |
|
| 628 | |||
| 629 | /** |
||
| 630 | * @return MediaEmbedInterface |
||
| 631 | */ |
||
| 632 | 2 | public function getEmbed() : MediaEmbedInterface |
|
| 636 | |||
| 637 | /** |
||
| 638 | * @param MediaEmbedInterface $embed |
||
| 639 | * @return MediaInterface |
||
| 640 | */ |
||
| 641 | 2 | public function setEmbed(MediaEmbedInterface $embed) : MediaInterface |
|
| 647 | |||
| 648 | /** |
||
| 649 | * @return MediaLicenseInterface |
||
| 650 | */ |
||
| 651 | 1 | public function getLicense() : MediaLicenseInterface |
|
| 655 | |||
| 656 | /** |
||
| 657 | * @param MediaLicenseInterface $license |
||
| 658 | * @return MediaInterface |
||
| 659 | */ |
||
| 660 | 1 | public function setLicense(MediaLicenseInterface $license) : MediaInterface |
|
| 666 | |||
| 667 | /** |
||
| 668 | * @return MediaCommunityInterface |
||
| 669 | */ |
||
| 670 | 2 | public function getCommunity() : MediaCommunityInterface |
|
| 674 | |||
| 675 | /** |
||
| 676 | * @param MediaCommunityInterface $community |
||
| 677 | * @return MediaInterface |
||
| 678 | */ |
||
| 679 | 3 | public function setCommunity(MediaCommunityInterface $community) : MediaInterface |
|
| 685 | |||
| 686 | /** |
||
| 687 | * @return MediaRestrictionInterface |
||
| 688 | */ |
||
| 689 | 2 | public function getRestriction() : MediaRestrictionInterface |
|
| 693 | |||
| 694 | /** |
||
| 695 | * @param MediaRestrictionInterface $restriction |
||
| 696 | * @return MediaInterface |
||
| 697 | */ |
||
| 698 | 2 | public function setRestriction(MediaRestrictionInterface $restriction) : MediaInterface |
|
| 704 | |||
| 705 | /** |
||
| 706 | * @return MediaRatingInterface |
||
| 707 | */ |
||
| 708 | 2 | public function getRating() : MediaRatingInterface |
|
| 712 | |||
| 713 | /** |
||
| 714 | * @param MediaRatingInterface $rating |
||
| 715 | * @return MediaInterface |
||
| 716 | */ |
||
| 717 | 2 | public function setRating(MediaRatingInterface $rating) : MediaInterface |
|
| 723 | |||
| 724 | /** |
||
| 725 | * @return MediaCopyrightInterface |
||
| 726 | */ |
||
| 727 | 2 | public function getCopyright() : MediaCopyrightInterface |
|
| 731 | |||
| 732 | /** |
||
| 733 | * @param MediaCopyrightInterface $copyright |
||
| 734 | * @return MediaInterface |
||
| 735 | */ |
||
| 736 | 2 | public function setCopyright(MediaCopyrightInterface $copyright) : MediaInterface |
|
| 742 | |||
| 743 | /** |
||
| 744 | * @return MediaStatusInterface |
||
| 745 | */ |
||
| 746 | 2 | public function getStatus() : MediaStatusInterface |
|
| 750 | |||
| 751 | /** |
||
| 752 | * @param MediaStatusInterface $status |
||
| 753 | * @return MediaInterface |
||
| 754 | */ |
||
| 755 | 2 | public function setStatus(MediaStatusInterface $status) : MediaInterface |
|
| 761 | |||
| 762 | /** |
||
| 763 | * @return MediaPeerLinkInterface |
||
| 764 | */ |
||
| 765 | 1 | public function getPeerLink() : MediaPeerLinkInterface |
|
| 769 | |||
| 770 | /** |
||
| 771 | * @param MediaPeerLinkInterface $peerLink |
||
| 772 | * @return MediaInterface |
||
| 773 | */ |
||
| 774 | 1 | public function setPeerLink(MediaPeerLinkInterface $peerLink) : MediaInterface |
|
| 780 | } |
||
| 781 |
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.