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 | * System properties |
||
| 79 | * |
||
| 80 | * @var SystemProperties |
||
| 81 | */ |
||
| 82 | protected $systemProperties; |
||
| 83 | /** |
||
| 84 | * Meta properties |
||
| 85 | * |
||
| 86 | * @var MetaProperties |
||
| 87 | */ |
||
| 88 | protected $metaProperties; |
||
| 89 | /** |
||
| 90 | * Domain properties |
||
| 91 | * |
||
| 92 | * @var AbstractDomainProperties |
||
| 93 | */ |
||
| 94 | protected $domainProperties; |
||
| 95 | /** |
||
| 96 | * Object payload |
||
| 97 | * |
||
| 98 | * @var string |
||
| 99 | */ |
||
| 100 | protected $payload; |
||
| 101 | /** |
||
| 102 | * Repository path |
||
| 103 | * |
||
| 104 | * @var RepositoryPathInterface |
||
| 105 | */ |
||
| 106 | protected $path; |
||
| 107 | /** |
||
| 108 | * Domain property collection class |
||
| 109 | * |
||
| 110 | * @var string |
||
| 111 | */ |
||
| 112 | protected $domainPropertyCClass = AbstractDomainProperties::class; |
||
| 113 | /** |
||
| 114 | * Object relations |
||
| 115 | * |
||
| 116 | * @var Relations |
||
| 117 | */ |
||
| 118 | protected $relations; |
||
| 119 | /** |
||
| 120 | * Processing instructions |
||
| 121 | * |
||
| 122 | * @var ProcessingInstructions |
||
| 123 | */ |
||
| 124 | protected $processingInstructions; |
||
| 125 | /** |
||
| 126 | * Latest revision index |
||
| 127 | * |
||
| 128 | * @var Revision |
||
| 129 | */ |
||
| 130 | protected $latestRevision; |
||
| 131 | /** |
||
| 132 | * Object state |
||
| 133 | * |
||
| 134 | * @var int |
||
| 135 | */ |
||
| 136 | protected $state = self::STATE_CLEAN; |
||
| 137 | /** |
||
| 138 | * Property collection states |
||
| 139 | * |
||
| 140 | * @var array |
||
| 141 | */ |
||
| 142 | protected $collectionStates = []; |
||
| 143 | |||
| 144 | /** |
||
| 145 | * Object constructor |
||
| 146 | * |
||
| 147 | * @param string $payload Object payload |
||
| 148 | * @param array $propertyData Property data |
||
| 149 | * @param RepositoryPathInterface $path Object repository path |
||
| 150 | */ |
||
| 151 | 19 | public function __construct($payload = '', array $propertyData = [], RepositoryPathInterface $path = null) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Load object revision data |
||
| 178 | * |
||
| 179 | * @param string $payload Object payload |
||
| 180 | * @param array $propertyData Property data |
||
| 181 | */ |
||
| 182 | 18 | protected function loadRevisionData($payload = '', array $propertyData = []) |
|
| 228 | |||
| 229 | /** |
||
| 230 | * Set the meta properties collection |
||
| 231 | * |
||
| 232 | * @param MetaProperties $metaProperties Meta property collection |
||
| 233 | * @param bool $overwrite Overwrite the existing collection (if present) |
||
| 234 | */ |
||
| 235 | 16 | protected function setMetaProperties(MetaProperties $metaProperties, $overwrite = false) |
|
| 236 | { |
||
| 237 | 16 | $this->metaProperties = $metaProperties; |
|
| 238 | 16 | $metaPropertiesState = spl_object_hash($this->metaProperties); |
|
| 239 | |||
| 240 | // If the meta property collection state has changed |
||
| 241 | 16 | if (!$overwrite && !empty($this->collectionStates[MetaProperties::COLLECTION]) && |
|
| 242 | 1 | ($metaPropertiesState !== $this->collectionStates[MetaProperties::COLLECTION]) |
|
| 243 | 16 | ) { |
|
| 244 | // Flag this object as mutated |
||
| 245 | 1 | $this->setMutatedState(); |
|
| 246 | 1 | } |
|
| 247 | |||
| 248 | 16 | $this->collectionStates[MetaProperties::COLLECTION] = $metaPropertiesState; |
|
| 249 | 16 | } |
|
| 250 | |||
| 251 | /** |
||
| 252 | * Set the object state to mutated |
||
| 253 | */ |
||
| 254 | 1 | protected function setMutatedState() |
|
| 255 | { |
||
| 256 | // If this object is not in mutated state yet |
||
| 257 | 1 | if (!($this->state & self::STATE_MUTATED) && !$this->isDraft()) { |
|
| 258 | // Increment the latest revision number |
||
| 259 | 1 | $this->latestRevision = $this->latestRevision->increment(); |
|
| 260 | |||
| 261 | // Create draft system properties |
||
| 262 | 1 | $this->systemProperties = $this->systemProperties->createDraft($this->latestRevision); |
|
| 263 | |||
| 264 | // Set the draft flag on the repository path |
||
| 265 | 1 | $this->path = $this->path->setDraft(true); |
|
| 266 | |||
| 267 | // If this is not already a draft ... |
||
| 268 | // Recreate the system properties |
||
| 269 | // Copy the object ID |
||
| 270 | // Copy the object type |
||
| 271 | // Set the revision number to latest revision + 1 |
||
| 272 | // Set the creation date to now |
||
| 273 | // Set no publication date |
||
| 274 | // Set the draft flag on the repository path |
||
| 275 | // Increase the latest revision by 1 |
||
| 276 | |||
| 277 | // Else if this is a draft |
||
| 278 | // No action needed |
||
| 279 | 1 | } |
|
| 280 | |||
| 281 | // Enable the mutated (and dirty) state |
||
| 282 | 1 | $this->state |= (self::STATE_DIRTY | self::STATE_MUTATED); |
|
| 283 | 1 | } |
|
| 284 | |||
| 285 | /** |
||
| 286 | * Return the object revision |
||
| 287 | * |
||
| 288 | * @return Revision Object revision |
||
| 289 | */ |
||
| 290 | 16 | public function getRevision() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Return whether the object is in dirty state |
||
| 297 | * |
||
| 298 | * @return boolean Dirty state |
||
| 299 | */ |
||
| 300 | public function isDirty() |
||
| 304 | |||
| 305 | /** |
||
| 306 | * Return whether the object is in mutated state |
||
| 307 | * |
||
| 308 | * @return boolean Mutated state |
||
| 309 | */ |
||
| 310 | public function isMutated() |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Return the object draft mode |
||
| 317 | * |
||
| 318 | * @return boolean Object draft mode |
||
| 319 | */ |
||
| 320 | 2 | public function isDraft() |
|
| 324 | |||
| 325 | /** |
||
| 326 | * Use a specific object revision |
||
| 327 | * |
||
| 328 | * @param Revision $revision Revision to be used |
||
| 329 | * @return ObjectInterface Object |
||
| 330 | * @throws OutOfBoundsException If the requested revision is invalid |
||
| 331 | */ |
||
| 332 | 15 | public function useRevision(Revision $revision) |
|
| 333 | { |
||
| 334 | 15 | $isCurrentRevision = false; |
|
| 335 | |||
| 336 | // If the requested revision is invalid |
||
| 337 | 15 | if (!$revision->isCurrent() && |
|
| 338 | (($revision->getRevision() < 1) || ($revision->getRevision() > $this->latestRevision->getRevision())) |
||
| 339 | 15 | ) { |
|
| 340 | throw new OutOfBoundsException(sprintf('Invalid object revision "%s"', $revision->getRevision()), |
||
| 341 | OutOfBoundsException::INVALID_OBJECT_REVISION); |
||
| 342 | } |
||
| 343 | |||
| 344 | // If the current revision got requested |
||
| 345 | 15 | if ($revision->isCurrent()) { |
|
| 346 | 15 | $isCurrentRevision = true; |
|
| 347 | 15 | $revision = $this->latestRevision; |
|
| 348 | 15 | } |
|
| 349 | |||
| 350 | // If the requested revision is not already used |
||
| 351 | 15 | if ($revision != $this->getRevision()) { |
|
| 352 | /** @var ManagerInterface $objectManager */ |
||
| 353 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
||
| 354 | |||
| 355 | // Load the requested object revision resource |
||
| 356 | /** @var Revision $newRevision */ |
||
| 357 | $newRevision = $isCurrentRevision ? Kernel::create(Revision::class, [Revision::CURRENT]) : |
||
| 358 | $revision; |
||
| 359 | /** @var RepositoryPath $newRevisionPath */ |
||
| 360 | $newRevisionPath = $this->path->setRevision($newRevision); |
||
| 361 | $revisionResource = $objectManager->loadObject($newRevisionPath); |
||
| 362 | |||
| 363 | // Load the revision resource data |
||
| 364 | $this->loadRevisionData($revisionResource->getPayload(), $revisionResource->getPropertyData()); |
||
| 365 | |||
| 366 | // Set the current revision path |
||
| 367 | $this->path = $newRevisionPath; |
||
| 368 | } |
||
| 369 | |||
| 370 | 15 | return $this; |
|
| 371 | } |
||
| 372 | |||
| 373 | /** |
||
| 374 | * Return the object ID |
||
| 375 | * |
||
| 376 | * @return Id Object ID |
||
| 377 | */ |
||
| 378 | 5 | public function getId() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Return the object type |
||
| 385 | * |
||
| 386 | * @return Type Object type |
||
| 387 | */ |
||
| 388 | 1 | public function getType() |
|
| 392 | |||
| 393 | /** |
||
| 394 | * Return the creation date & time |
||
| 395 | * |
||
| 396 | * @return \DateTimeImmutable Creation date & time |
||
| 397 | */ |
||
| 398 | 1 | public function getCreated() |
|
| 402 | |||
| 403 | /** |
||
| 404 | * Return the publication date & time |
||
| 405 | * |
||
| 406 | * @return \DateTimeImmutable Publication date & time |
||
| 407 | */ |
||
| 408 | 1 | public function getPublished() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Return the object hash |
||
| 415 | * |
||
| 416 | * @return string Object hash |
||
| 417 | */ |
||
| 418 | 1 | public function getHash() |
|
| 422 | |||
| 423 | /** |
||
| 424 | * Return the object description |
||
| 425 | * |
||
| 426 | * @return string Object description |
||
| 427 | */ |
||
| 428 | 2 | public function getDescription() |
|
| 432 | |||
| 433 | /** |
||
| 434 | * Set the description |
||
| 435 | * |
||
| 436 | * @param string $description Description |
||
| 437 | * @return ObjectInterface Self reference |
||
| 438 | */ |
||
| 439 | 1 | public function setDescription($description) |
|
| 444 | |||
| 445 | /** |
||
| 446 | * Return the object abstract |
||
| 447 | * |
||
| 448 | * @return string Object abstract |
||
| 449 | */ |
||
| 450 | 2 | public function getAbstract() |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Set the abstract |
||
| 457 | * |
||
| 458 | * @param string $abstract Abstract |
||
| 459 | * @return ObjectInterface Self reference |
||
| 460 | */ |
||
| 461 | 1 | public function setAbstract($abstract) |
|
| 462 | { |
||
| 463 | 1 | $this->setMetaProperties($this->metaProperties->setAbstract($abstract)); |
|
| 464 | 1 | return $this; |
|
| 465 | } |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Return all object keywords |
||
| 469 | * |
||
| 470 | * @return array Object keywords |
||
| 471 | */ |
||
| 472 | 2 | public function getKeywords() |
|
| 476 | |||
| 477 | /** |
||
| 478 | * Set the keywords |
||
| 479 | * |
||
| 480 | * @param array $keywords Keywords |
||
| 481 | * @return ObjectInterface Self reference |
||
| 482 | */ |
||
| 483 | 1 | public function setKeywords(array $keywords) |
|
| 484 | { |
||
| 485 | 1 | $this->setMetaProperties($this->metaProperties->setKeywords($keywords)); |
|
| 486 | 1 | return $this; |
|
| 487 | } |
||
| 488 | |||
| 489 | /** |
||
| 490 | * Return all object categories |
||
| 491 | * |
||
| 492 | * @return array Object categories |
||
| 493 | */ |
||
| 494 | 2 | public function getCategories() |
|
| 498 | |||
| 499 | /** |
||
| 500 | * Set the categories |
||
| 501 | * |
||
| 502 | * @param array $categories Categories |
||
| 503 | * @return ObjectInterface Self reference |
||
| 504 | */ |
||
| 505 | 1 | public function setCategories(array $categories) |
|
| 506 | { |
||
| 507 | 1 | $this->setMetaProperties($this->metaProperties->setCategories($categories)); |
|
| 508 | 1 | return $this; |
|
| 509 | } |
||
| 510 | |||
| 511 | /** |
||
| 512 | * Return all object authors |
||
| 513 | * |
||
| 514 | * @return AuthorInterface[] Authors |
||
| 515 | */ |
||
| 516 | 2 | public function getAuthors() |
|
| 520 | |||
| 521 | /** |
||
| 522 | * Add an object author |
||
| 523 | * |
||
| 524 | * @param AuthorInterface $author Author |
||
| 525 | * @return ObjectInterface Self reference |
||
| 526 | */ |
||
| 527 | 1 | public function addAuthor(AuthorInterface $author) |
|
| 534 | |||
| 535 | /** |
||
| 536 | * Return the object repository path |
||
| 537 | * |
||
| 538 | * @return RepositoryPathInterface Object repository path |
||
| 539 | */ |
||
| 540 | 16 | public function getRepositoryPath() |
|
| 544 | |||
| 545 | /** |
||
| 546 | * Return the object property data |
||
| 547 | * |
||
| 548 | * @return array Object property data |
||
| 549 | */ |
||
| 550 | 3 | public function getPropertyData() |
|
| 564 | |||
| 565 | /** |
||
| 566 | * Return the object payload |
||
| 567 | * |
||
| 568 | * @return string Object payload |
||
| 569 | */ |
||
| 570 | 2 | public function getPayload() |
|
| 574 | |||
| 575 | /** |
||
| 576 | * Return the absolute object URL |
||
| 577 | * |
||
| 578 | * @return string |
||
| 579 | */ |
||
| 580 | 2 | public function getAbsoluteUrl() |
|
| 584 | |||
| 585 | /** |
||
| 586 | * Get a particular property value |
||
| 587 | * |
||
| 588 | * Multi-level properties might be traversed by property name paths separated with colons (":"). |
||
| 589 | * |
||
| 590 | * @param string $property Property name |
||
| 591 | * @return mixed Property value |
||
| 592 | */ |
||
| 593 | 2 | public function getDomainProperty($property) |
|
| 597 | |||
| 598 | protected function setDirtyState() |
||
| 599 | { |
||
| 600 | |||
| 601 | // If this object is not in dirty state yet |
||
| 602 | if (!($this->state & self::STATE_DIRTY)) { |
||
|
|
|||
| 603 | |||
| 609 | } |
||
| 610 |