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