Complex classes like AbstractObject 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 AbstractObject, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 57 | abstract class AbstractObject implements ObjectInterface |
||
| 58 | { |
||
| 59 | /** |
||
| 60 | * Clean state |
||
| 61 | * |
||
| 62 | * @var int |
||
| 63 | */ |
||
| 64 | const STATE_CLEAN = 0; |
||
| 65 | /** |
||
| 66 | * Dirty state |
||
| 67 | * |
||
| 68 | * @var int |
||
| 69 | */ |
||
| 70 | const STATE_DIRTY = 1; |
||
| 71 | /** |
||
| 72 | * Mutated state |
||
| 73 | * |
||
| 74 | * @var int |
||
| 75 | */ |
||
| 76 | const STATE_MUTATED = 2; |
||
| 77 | /** |
||
| 78 | * Published state |
||
| 79 | * |
||
| 80 | * @var int |
||
| 81 | */ |
||
| 82 | const STATE_PUBLISHED = 4; |
||
| 83 | /** |
||
| 84 | * System properties |
||
| 85 | * |
||
| 86 | * @var SystemProperties |
||
| 87 | */ |
||
| 88 | protected $systemProperties; |
||
| 89 | /** |
||
| 90 | * Meta properties |
||
| 91 | * |
||
| 92 | * @var MetaProperties |
||
| 93 | */ |
||
| 94 | protected $metaProperties; |
||
| 95 | /** |
||
| 96 | * Domain properties |
||
| 97 | * |
||
| 98 | * @var AbstractDomainProperties |
||
| 99 | */ |
||
| 100 | protected $domainProperties; |
||
| 101 | /** |
||
| 102 | * Object payload |
||
| 103 | * |
||
| 104 | * @var string |
||
| 105 | */ |
||
| 106 | protected $payload; |
||
| 107 | /** |
||
| 108 | * Repository path |
||
| 109 | * |
||
| 110 | * @var RepositoryPathInterface |
||
| 111 | */ |
||
| 112 | protected $path; |
||
| 113 | /** |
||
| 114 | * Domain property collection class |
||
| 115 | * |
||
| 116 | * @var string |
||
| 117 | */ |
||
| 118 | protected $domainPropertyCClass = AbstractDomainProperties::class; |
||
| 119 | /** |
||
| 120 | * Object relations |
||
| 121 | * |
||
| 122 | * @var Relations |
||
| 123 | */ |
||
| 124 | protected $relations; |
||
| 125 | /** |
||
| 126 | * Processing instructions |
||
| 127 | * |
||
| 128 | * @var ProcessingInstructions |
||
| 129 | */ |
||
| 130 | protected $processingInstructions; |
||
| 131 | /** |
||
| 132 | * Latest revision index |
||
| 133 | * |
||
| 134 | * @var Revision |
||
| 135 | */ |
||
| 136 | protected $latestRevision; |
||
| 137 | /** |
||
| 138 | * Object state |
||
| 139 | * |
||
| 140 | * @var int |
||
| 141 | */ |
||
| 142 | protected $state = self::STATE_CLEAN; |
||
| 143 | /** |
||
| 144 | * Property collection states |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $collectionStates = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Object constructor |
||
| 152 | * |
||
| 153 | * @param string $payload Object payload |
||
| 154 | * @param array $propertyData Property data |
||
| 155 | * @param RepositoryPathInterface $path Object repository path |
||
| 156 | */ |
||
| 157 | 20 | public function __construct($payload = '', array $propertyData = [], RepositoryPathInterface $path = null) |
|
| 182 | |||
| 183 | /** |
||
| 184 | * Load object revision data |
||
| 185 | * |
||
| 186 | * @param string $payload Object payload |
||
| 187 | * @param array $propertyData Property data |
||
| 188 | */ |
||
| 189 | 19 | protected function loadRevisionData($payload = '', array $propertyData = []) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Set the meta properties collection |
||
| 242 | * |
||
| 243 | * @param MetaProperties $metaProperties Meta property collection |
||
| 244 | * @param bool $overwrite Overwrite the existing collection (if present) |
||
| 245 | */ |
||
| 246 | 18 | protected function setMetaProperties(MetaProperties $metaProperties, $overwrite = false) |
|
| 262 | |||
| 263 | /** |
||
| 264 | * Set the object state to mutated |
||
| 265 | */ |
||
| 266 | 3 | protected function setMutatedState() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Return the object draft mode |
||
| 280 | * |
||
| 281 | * @return boolean Object draft mode |
||
| 282 | */ |
||
| 283 | 4 | public function isDraft() |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Return whether the object is in published state |
||
| 290 | * |
||
| 291 | * @return boolean Published state |
||
| 292 | */ |
||
| 293 | 4 | public function isPublished() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Convert this object revision into a draft |
||
| 300 | */ |
||
| 301 | 3 | protected function convertToDraft() |
|
| 328 | |||
| 329 | /** |
||
| 330 | * Set the domain properties collection |
||
| 331 | * |
||
| 332 | * @param GenericPropertiesInterface $domainProperties Domain property collection |
||
| 333 | * @param bool $overwrite Overwrite the existing collection (if present) |
||
| 334 | */ |
||
| 335 | 18 | protected function setDomainProperties(GenericPropertiesInterface $domainProperties, $overwrite = false) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Set the processing instruction collection |
||
| 354 | * |
||
| 355 | * @param GenericPropertiesInterface $processingInstructions Processing instruction collection |
||
| 356 | * @param bool $overwrite Overwrite the existing collection (if present) |
||
| 357 | */ |
||
| 358 | 18 | protected function setProcessingInstructions(GenericPropertiesInterface $processingInstructions, $overwrite = false) |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Set the object state to dirty |
||
| 377 | */ |
||
| 378 | 1 | protected function setDirtyState() |
|
| 388 | |||
| 389 | /** |
||
| 390 | * Set the relations collection |
||
| 391 | * |
||
| 392 | * @param Relations $relations Relations collection |
||
| 393 | * @param bool $overwrite Overwrite the existing collection (if present) |
||
| 394 | */ |
||
| 395 | 18 | protected function setRelations(Relations $relations, $overwrite = false) |
|
| 411 | |||
| 412 | /** |
||
| 413 | * Return the object revision |
||
| 414 | * |
||
| 415 | * @return Revision Object revision |
||
| 416 | */ |
||
| 417 | 18 | public function getRevision() |
|
| 421 | |||
| 422 | /** |
||
| 423 | * Return whether the object is in mutated state |
||
| 424 | * |
||
| 425 | * @return boolean Mutated state |
||
| 426 | */ |
||
| 427 | 3 | public function isMutated() |
|
| 431 | |||
| 432 | /** |
||
| 433 | * Use a specific object revision |
||
| 434 | * |
||
| 435 | * @param Revision $revision Revision to be used |
||
| 436 | * @return ObjectInterface Object |
||
| 437 | * @throws OutOfBoundsException If the requested revision is invalid |
||
| 438 | */ |
||
| 439 | 17 | public function useRevision(Revision $revision) |
|
| 478 | |||
| 479 | /** |
||
| 480 | * Return the object ID |
||
| 481 | * |
||
| 482 | * @return Id Object ID |
||
| 483 | */ |
||
| 484 | 5 | public function getId() |
|
| 488 | |||
| 489 | /** |
||
| 490 | * Return the object type |
||
| 491 | * |
||
| 492 | * @return Type Object type |
||
| 493 | */ |
||
| 494 | 1 | public function getType() |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Return the creation date & time |
||
| 501 | * |
||
| 502 | * @return \DateTimeImmutable Creation date & time |
||
| 503 | */ |
||
| 504 | 1 | public function getCreated() |
|
| 508 | |||
| 509 | /** |
||
| 510 | * Return the publication date & time |
||
| 511 | * |
||
| 512 | * @return \DateTimeImmutable|null Publication date & time |
||
| 513 | */ |
||
| 514 | 1 | public function getPublished() |
|
| 518 | |||
| 519 | /** |
||
| 520 | * Return the object hash |
||
| 521 | * |
||
| 522 | * @return string Object hash |
||
| 523 | */ |
||
| 524 | 1 | public function getHash() |
|
| 528 | |||
| 529 | /** |
||
| 530 | * Return the object title |
||
| 531 | * |
||
| 532 | * @return string Object title |
||
| 533 | */ |
||
| 534 | 1 | public function getTitle() |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Set the title |
||
| 541 | * |
||
| 542 | * @param string $title Title |
||
| 543 | * @return ObjectInterface Self reference |
||
| 544 | */ |
||
| 545 | 1 | public function setTitle($title) |
|
| 550 | |||
| 551 | /** |
||
| 552 | * Return the object slug |
||
| 553 | * |
||
| 554 | * @return string Object slug |
||
| 555 | */ |
||
| 556 | 1 | public function getSlug() |
|
| 560 | |||
| 561 | /** |
||
| 562 | * Set the slug |
||
| 563 | * |
||
| 564 | * @param string $slug Slug |
||
| 565 | * @return ObjectInterface Self reference |
||
| 566 | */ |
||
| 567 | 1 | public function setSlug($slug) |
|
| 572 | |||
| 573 | /** |
||
| 574 | * Return the object description |
||
| 575 | * |
||
| 576 | * @return string Object description |
||
| 577 | */ |
||
| 578 | 2 | public function getDescription() |
|
| 582 | |||
| 583 | /** |
||
| 584 | * Set the description |
||
| 585 | * |
||
| 586 | * @param string $description Description |
||
| 587 | * @return ObjectInterface Self reference |
||
| 588 | */ |
||
| 589 | 1 | public function setDescription($description) |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Return the object abstract |
||
| 597 | * |
||
| 598 | * @return string Object abstract |
||
| 599 | */ |
||
| 600 | 2 | public function getAbstract() |
|
| 604 | |||
| 605 | /** |
||
| 606 | * Set the abstract |
||
| 607 | * |
||
| 608 | * @param string $abstract Abstract |
||
| 609 | * @return ObjectInterface Self reference |
||
| 610 | */ |
||
| 611 | 1 | public function setAbstract($abstract) |
|
| 616 | |||
| 617 | /** |
||
| 618 | * Return all object keywords |
||
| 619 | * |
||
| 620 | * @return array Object keywords |
||
| 621 | */ |
||
| 622 | 2 | public function getKeywords() |
|
| 626 | |||
| 627 | /** |
||
| 628 | * Set the keywords |
||
| 629 | * |
||
| 630 | * @param array $keywords Keywords |
||
| 631 | * @return ObjectInterface Self reference |
||
| 632 | */ |
||
| 633 | 1 | public function setKeywords(array $keywords) |
|
| 638 | |||
| 639 | /** |
||
| 640 | * Return all object categories |
||
| 641 | * |
||
| 642 | * @return array Object categories |
||
| 643 | */ |
||
| 644 | 2 | public function getCategories() |
|
| 648 | |||
| 649 | /** |
||
| 650 | * Set the categories |
||
| 651 | * |
||
| 652 | * @param array $categories Categories |
||
| 653 | * @return ObjectInterface Self reference |
||
| 654 | */ |
||
| 655 | 1 | public function setCategories(array $categories) |
|
| 660 | |||
| 661 | /** |
||
| 662 | * Return the object repository path |
||
| 663 | * |
||
| 664 | * @return RepositoryPathInterface Object repository path |
||
| 665 | */ |
||
| 666 | 18 | public function getRepositoryPath() |
|
| 670 | |||
| 671 | /** |
||
| 672 | * Return the object property data |
||
| 673 | * |
||
| 674 | * @return array Object property data |
||
| 675 | */ |
||
| 676 | 5 | public function getPropertyData() |
|
| 690 | |||
| 691 | /** |
||
| 692 | * Return the object payload |
||
| 693 | * |
||
| 694 | * @return string Object payload |
||
| 695 | */ |
||
| 696 | 2 | public function getPayload() |
|
| 700 | |||
| 701 | /** |
||
| 702 | * Set the payload |
||
| 703 | * |
||
| 704 | * @param string $payload Payload |
||
| 705 | * @return ObjectInterface Self reference |
||
| 706 | */ |
||
| 707 | 1 | public function setPayload($payload) |
|
| 717 | |||
| 718 | /** |
||
| 719 | * Return the absolute object URL |
||
| 720 | * |
||
| 721 | * @return string |
||
| 722 | */ |
||
| 723 | 4 | public function getAbsoluteUrl() |
|
| 727 | |||
| 728 | /** |
||
| 729 | * Get a domain property value |
||
| 730 | * |
||
| 731 | * Multi-level properties might be traversed by property name paths separated with colons (":"). |
||
| 732 | * |
||
| 733 | * @param string $property Property name |
||
| 734 | * @return mixed Property value |
||
| 735 | */ |
||
| 736 | 2 | public function getDomainProperty($property) |
|
| 740 | |||
| 741 | /** |
||
| 742 | * Set a domain property value |
||
| 743 | * |
||
| 744 | * @param string $property Property name |
||
| 745 | * @param mixed $value Property value |
||
| 746 | * @return ObjectInterface Self reference |
||
| 747 | */ |
||
| 748 | 1 | public function setDomainProperty($property, $value) |
|
| 753 | |||
| 754 | /** |
||
| 755 | * Get a processing instruction |
||
| 756 | * |
||
| 757 | * @param string $procInst Processing instruction name |
||
| 758 | * @return mixed Processing instruction |
||
| 759 | */ |
||
| 760 | public function getProcessingInstruction($procInst) |
||
| 764 | |||
| 765 | /** |
||
| 766 | * Set a processing instruction |
||
| 767 | * |
||
| 768 | * @param string $procInst Processing instruction name |
||
| 769 | * @param mixed $value Processing instruction |
||
| 770 | * @return ObjectInterface Self reference |
||
| 771 | */ |
||
| 772 | 1 | public function setProcessingInstruction($procInst, $value) |
|
| 777 | |||
| 778 | /** |
||
| 779 | * Persist the current object revision |
||
| 780 | * |
||
| 781 | * @return ObjectInterface Object |
||
| 782 | */ |
||
| 783 | 1 | public function persist() |
|
| 805 | |||
| 806 | /** |
||
| 807 | * Publish the current object revision |
||
| 808 | * |
||
| 809 | * @return ObjectInterface Object |
||
| 810 | */ |
||
| 811 | 1 | public function publish() |
|
| 832 | |||
| 833 | /** |
||
| 834 | * Set the object state to published |
||
| 835 | */ |
||
| 836 | 1 | protected function setPublishedState() |
|
| 846 | |||
| 847 | /** |
||
| 848 | * Return whether the object is in dirty state |
||
| 849 | * |
||
| 850 | * @return boolean Dirty state |
||
| 851 | */ |
||
| 852 | 3 | public function isDirty() |
|
| 856 | } |
||
| 857 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: