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 |
||
| 62 | abstract class AbstractObject implements ObjectInterface |
||
| 63 | { |
||
| 64 | /** |
||
| 65 | * Use traits |
||
| 66 | */ |
||
| 67 | use SystemPropertiesTrait, MetaPropertiesTrait, DomainPropertiesTrait, RelationsTrait, |
||
| 68 | ProcessingInstructionsTrait, PayloadTrait; |
||
| 69 | /** |
||
| 70 | * Clean state |
||
| 71 | * |
||
| 72 | * @var int |
||
| 73 | */ |
||
| 74 | const STATE_CLEAN = 0; |
||
| 75 | /** |
||
| 76 | * Modified state |
||
| 77 | * |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | const STATE_MODIFIED = 1; |
||
| 81 | /** |
||
| 82 | * Mutated state |
||
| 83 | * |
||
| 84 | * @var int |
||
| 85 | */ |
||
| 86 | const STATE_MUTATED = 2; |
||
| 87 | /** |
||
| 88 | * Published state |
||
| 89 | * |
||
| 90 | * @var int |
||
| 91 | */ |
||
| 92 | const STATE_PUBLISHED = 4; |
||
| 93 | /** |
||
| 94 | * Deleted state |
||
| 95 | * |
||
| 96 | * @var int |
||
| 97 | */ |
||
| 98 | const STATE_DELETED = 8; |
||
| 99 | /** |
||
| 100 | * Undeleted state |
||
| 101 | * |
||
| 102 | * @var int |
||
| 103 | */ |
||
| 104 | const STATE_UNDELETED = 16; |
||
| 105 | /** |
||
| 106 | * Repository path |
||
| 107 | * |
||
| 108 | * @var RepositoryPathInterface |
||
| 109 | */ |
||
| 110 | protected $path; |
||
| 111 | /** |
||
| 112 | * Latest revision index |
||
| 113 | * |
||
| 114 | * @var Revision |
||
| 115 | */ |
||
| 116 | protected $latestRevision; |
||
| 117 | /** |
||
| 118 | * Object state |
||
| 119 | * |
||
| 120 | * @var int |
||
| 121 | */ |
||
| 122 | protected $state = self::STATE_CLEAN; |
||
| 123 | /** |
||
| 124 | * Property collection states |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | protected $collectionStates = []; |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Object constructor |
||
| 132 | * |
||
| 133 | * @param RepositoryPathInterface $path Object repository path |
||
| 134 | * @param string $payload Object payload |
||
| 135 | * @param array $propertyData Property data |
||
| 136 | */ |
||
| 137 | 26 | public function __construct(RepositoryPathInterface $path, $payload = '', array $propertyData = []) |
|
| 138 | { |
||
| 139 | // If the domain property collection class is invalid |
||
| 140 | 26 | if (!$this->domainPropertyCClass |
|
| 141 | 26 | || !class_exists($this->domainPropertyCClass) |
|
| 142 | 26 | || !(new \ReflectionClass($this->domainPropertyCClass))->isSubclassOf(AbstractDomainProperties::class) |
|
| 143 | ) { |
||
| 144 | 1 | throw new PropertyInvalidArgumentException( |
|
| 145 | sprintf( |
||
| 146 | 1 | 'Invalid domain property collection class "%s"', |
|
| 147 | 1 | $this->domainPropertyCClass |
|
| 148 | ), |
||
| 149 | 1 | PropertyInvalidArgumentException::INVALID_DOMAIN_PROPERTY_COLLECTION_CLASS |
|
| 150 | ); |
||
| 151 | } |
||
| 152 | |||
| 153 | // Right after instantiation it's always the current revision |
||
| 154 | 25 | $this->path = $path->setRevision(Revision::current()); |
|
| 155 | |||
| 156 | // Load the current revision data |
||
| 157 | 25 | $this->loadRevisionData($payload, $propertyData); |
|
| 158 | |||
| 159 | // Save the latest revision index |
||
| 160 | 24 | $this->latestRevision = $this->getRevision(); |
|
| 161 | 24 | } |
|
| 162 | |||
| 163 | /** |
||
| 164 | * Load object revision data |
||
| 165 | * |
||
| 166 | * @param string $payload Object payload |
||
| 167 | * @param array $propertyData Property data |
||
| 168 | */ |
||
| 169 | 25 | protected function loadRevisionData($payload = '', array $propertyData = []) |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Return whether the object is in mutated state |
||
| 222 | * |
||
| 223 | * @return boolean Mutated state |
||
| 224 | */ |
||
| 225 | 3 | public function hasBeenMutated() |
|
| 226 | { |
||
| 227 | 3 | return !!($this->state & self::STATE_MUTATED); |
|
| 228 | } |
||
| 229 | |||
| 230 | /** |
||
| 231 | * Use a specific object revision |
||
| 232 | * |
||
| 233 | * @param Revision $revision Revision to be used |
||
| 234 | * @return ObjectInterface Object |
||
| 235 | * @throws OutOfBoundsException If the requested revision is invalid |
||
| 236 | */ |
||
| 237 | 23 | public function useRevision(Revision $revision) |
|
| 238 | { |
||
| 239 | 23 | $isCurrentRevision = false; |
|
| 240 | |||
| 241 | // If the requested revision is invalid |
||
| 242 | 23 | if (!$revision->isCurrent() && |
|
| 243 | 23 | (($revision->getRevision() < 1) || ($revision->getRevision() > $this->latestRevision->getRevision())) |
|
| 244 | ) { |
||
| 245 | throw new OutOfBoundsException( |
||
| 246 | sprintf('Invalid object revision "%s"', $revision->getRevision()), |
||
| 247 | OutOfBoundsException::INVALID_OBJECT_REVISION |
||
| 248 | ); |
||
| 249 | } |
||
| 250 | |||
| 251 | // If the current revision got requested |
||
| 252 | 23 | if ($revision->isCurrent()) { |
|
| 253 | 23 | $isCurrentRevision = true; |
|
| 254 | 23 | $revision = $this->latestRevision; |
|
| 255 | } |
||
| 256 | |||
| 257 | // If the requested revision is not already used |
||
| 258 | 23 | if ($revision != $this->getRevision()) { |
|
| 259 | /** @var ManagerInterface $objectManager */ |
||
| 260 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
||
| 261 | |||
| 262 | // Load the requested object revision resource |
||
| 263 | /** @var Revision $newRevision */ |
||
| 264 | $newRevision = $isCurrentRevision ? Revision::current() : $revision; |
||
| 265 | /** @var RepositoryPath $newRevisionPath */ |
||
| 266 | $newRevisionPath = $this->path->setRevision($newRevision); |
||
| 267 | $revisionResource = $objectManager->loadObject($newRevisionPath); |
||
| 268 | |||
| 269 | // Load the revision resource data |
||
| 270 | $this->loadRevisionData($revisionResource->getPayload(), $revisionResource->getPropertyData()); |
||
| 271 | |||
| 272 | // Set the current revision path |
||
| 273 | $this->path = $newRevisionPath; |
||
| 274 | } |
||
| 275 | |||
| 276 | 23 | return $this; |
|
| 277 | } |
||
| 278 | |||
| 279 | /** |
||
| 280 | * Return the object repository path |
||
| 281 | * |
||
| 282 | * @return RepositoryPathInterface Object repository path |
||
| 283 | */ |
||
| 284 | 24 | public function getRepositoryPath() |
|
| 288 | |||
| 289 | /** |
||
| 290 | * Return the object property data |
||
| 291 | * |
||
| 292 | * @return array Object property data |
||
| 293 | */ |
||
| 294 | 5 | public function getPropertyData() |
|
| 308 | |||
| 309 | /** |
||
| 310 | * Return the absolute object URL |
||
| 311 | * |
||
| 312 | * @return string |
||
| 313 | */ |
||
| 314 | 4 | public function getAbsoluteUrl() |
|
| 318 | |||
| 319 | /** |
||
| 320 | * Persist the current object revision |
||
| 321 | * |
||
| 322 | * @return ObjectInterface Object |
||
| 323 | */ |
||
| 324 | 1 | public function persist() |
|
| 325 | { |
||
| 326 | // If this is not the latest revision |
||
| 327 | 1 | if ($this->getRevision() != $this->latestRevision) { |
|
| 328 | throw new RuntimeException( |
||
| 329 | sprintf( |
||
| 330 | 'Cannot persist revision %s/%s', |
||
| 331 | $this->getRevision()->getRevision(), |
||
| 332 | $this->latestRevision->getRevision() |
||
| 333 | ), |
||
| 334 | RuntimeException::CANNOT_PERSIST_EARLIER_REVISION |
||
| 335 | ); |
||
| 336 | } |
||
| 337 | |||
| 338 | // Update the object repository |
||
| 339 | 1 | $this->path->getRepository()->updateObject($this); |
|
| 340 | |||
| 341 | // Reset to a clean state |
||
| 342 | 1 | $this->state &= self::STATE_CLEAN; |
|
| 343 | |||
| 344 | 1 | return $this; |
|
| 345 | } |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Publish the current object revision |
||
| 349 | * |
||
| 350 | * @return ObjectInterface Object |
||
| 351 | */ |
||
| 352 | 1 | public function publish() |
|
| 353 | { |
||
| 354 | // If this is an unpublished draft |
||
| 355 | 1 | if ($this->isDraft() & !($this->state & self::STATE_PUBLISHED)) { |
|
| 356 | // TODO: Send signal |
||
| 357 | |||
| 358 | // Update system properties |
||
| 359 | 1 | $this->setSystemProperties($this->systemProperties->publish(), true); |
|
| 360 | |||
| 361 | // Remove the draft flag from the repository path |
||
| 362 | 1 | $this->path = $this->path->setDraft(false); |
|
| 363 | |||
| 364 | // Enable the modified & published state |
||
| 365 | 1 | $this->state |= (self::STATE_MODIFIED | self::STATE_PUBLISHED); |
|
| 366 | } |
||
| 367 | |||
| 368 | 1 | return $this; |
|
| 369 | } |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Return the object draft mode |
||
| 373 | * |
||
| 374 | * @return boolean Object draft mode |
||
| 375 | */ |
||
| 376 | 5 | public function isDraft() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Return whether the object is in published state |
||
| 383 | * |
||
| 384 | * @return boolean Published state |
||
| 385 | */ |
||
| 386 | public function isPublished() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Return whether the object has just been published |
||
| 393 | * |
||
| 394 | * @return boolean Object has just been published |
||
| 395 | */ |
||
| 396 | 5 | public function hasBeenPublished() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Delete the object and all its revisions |
||
| 403 | * |
||
| 404 | * @return ObjectInterface Object |
||
| 405 | */ |
||
| 406 | public function delete() |
||
| 407 | { |
||
| 408 | // If this object is not already deleted |
||
| 409 | if (!$this->isDeleted() && !$this->hasBeenDeleted()) { |
||
| 410 | // TODO: Send delete signal |
||
| 411 | |||
| 412 | // Update system properties |
||
| 413 | $this->setSystemProperties($this->systemProperties->delete(), true); |
||
| 414 | |||
| 415 | // TODO: Modify the object path so that it's deleted |
||
| 416 | |||
| 417 | // Flag the object as just deleted |
||
| 418 | $this->state |= self::STATE_MODIFIED; |
||
| 419 | $this->state |= self::STATE_DELETED; |
||
| 420 | $this->state &= ~self::STATE_UNDELETED; |
||
| 421 | } |
||
| 422 | |||
| 423 | return $this; |
||
| 424 | |||
| 425 | } |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Return whether the object has been deleted |
||
| 429 | * |
||
| 430 | * @return boolean Object is deleted |
||
| 431 | */ |
||
| 432 | public function isDeleted() |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Return whether the object has just been deleted |
||
| 439 | * |
||
| 440 | * @return boolean Object has just been deleted |
||
| 441 | */ |
||
| 442 | 1 | public function hasBeenDeleted() |
|
| 446 | |||
| 447 | /** |
||
| 448 | * Undelete the object and all its revisions |
||
| 449 | * |
||
| 450 | * @return ObjectInterface Object |
||
| 451 | */ |
||
| 452 | public function undelete() |
||
| 453 | { |
||
| 454 | // If this object is already deleted |
||
| 455 | if ($this->isDeleted() && !$this->hasBeenUndeleted()) { |
||
| 456 | // TODO: Send undelete signal |
||
| 457 | |||
| 458 | // Update system properties |
||
| 459 | $this->setSystemProperties($this->systemProperties->undelete(), true); |
||
| 460 | |||
| 461 | // TODO: Modify the object path so that it's not deleted |
||
| 462 | |||
| 463 | // Flag the object as just undeleted |
||
| 464 | $this->state |= self::STATE_MODIFIED; |
||
| 465 | $this->state |= self::STATE_UNDELETED; |
||
| 466 | $this->state &= ~self::STATE_DELETED; |
||
| 467 | } |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Return whether the object has just been undeleted |
||
| 472 | * |
||
| 473 | * @return boolean Object has just been undeleted |
||
| 474 | */ |
||
| 475 | 1 | public function hasBeenUndeleted() |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Return whether the object is in modified state |
||
| 482 | * |
||
| 483 | * @return boolean Modified state |
||
| 484 | */ |
||
| 485 | 3 | public function hasBeenModified() |
|
| 486 | { |
||
| 487 | 3 | return !!($this->state & self::STATE_MODIFIED); |
|
| 488 | } |
||
| 489 | |||
| 490 | /** |
||
| 491 | * Set the object state to mutated |
||
| 492 | */ |
||
| 493 | 4 | protected function setMutatedState() |
|
| 494 | { |
||
| 495 | // If this object is not in mutated state yet |
||
| 496 | 4 | if (!($this->state & self::STATE_MUTATED) && !$this->isDraft()) { |
|
| 497 | // TODO: Send signal |
||
| 498 | 4 | $this->convertToDraft(); |
|
| 499 | } |
||
| 500 | |||
| 501 | // Enable the mutated state |
||
| 502 | 4 | $this->state |= self::STATE_MUTATED; |
|
| 503 | |||
| 504 | // Enable the modified state |
||
| 505 | 4 | $this->setModifiedState(); |
|
| 506 | 4 | } |
|
| 507 | |||
| 508 | /** |
||
| 509 | * Convert this object revision into a draft |
||
| 510 | */ |
||
| 511 | 4 | protected function convertToDraft() |
|
| 538 | |||
| 539 | /** |
||
| 540 | * Set the object state to modified |
||
| 541 | */ |
||
| 542 | 7 | protected function setModifiedState() |
|
| 543 | { |
||
| 544 | // If this object is not in modified state yet |
||
| 545 | 7 | if (!($this->state & self::STATE_MODIFIED)) { |
|
| 546 | // TODO: Send signal |
||
| 547 | } |
||
| 555 | } |
||
| 556 |