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 | * Dirty state |
||
| 77 | * |
||
| 78 | * @var int |
||
| 79 | */ |
||
| 80 | const STATE_DIRTY = 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 | * Repository path |
||
| 95 | * |
||
| 96 | * @var RepositoryPathInterface |
||
| 97 | */ |
||
| 98 | protected $path; |
||
| 99 | /** |
||
| 100 | * Latest revision index |
||
| 101 | * |
||
| 102 | * @var Revision |
||
| 103 | */ |
||
| 104 | protected $latestRevision; |
||
| 105 | /** |
||
| 106 | * Object state |
||
| 107 | * |
||
| 108 | * @var int |
||
| 109 | */ |
||
| 110 | protected $state = self::STATE_CLEAN; |
||
| 111 | /** |
||
| 112 | * Property collection states |
||
| 113 | * |
||
| 114 | * @var array |
||
| 115 | */ |
||
| 116 | protected $collectionStates = []; |
||
| 117 | |||
| 118 | /** |
||
| 119 | * Object constructor |
||
| 120 | * |
||
| 121 | * @param string $payload Object payload |
||
| 122 | * @param array $propertyData Property data |
||
| 123 | * @param RepositoryPathInterface $path Object repository path |
||
| 124 | */ |
||
| 125 | 21 | public function __construct($payload = '', array $propertyData = [], RepositoryPathInterface $path = null) |
|
| 126 | { |
||
| 127 | // If the domain property collection class is invalid |
||
| 128 | 21 | if (!$this->domainPropertyCClass |
|
| 129 | 21 | || !class_exists($this->domainPropertyCClass) |
|
| 130 | 21 | || !(new \ReflectionClass($this->domainPropertyCClass))->isSubclassOf(AbstractDomainProperties::class) |
|
| 131 | ) { |
||
| 132 | 1 | throw new PropertyInvalidArgumentException( |
|
| 133 | sprintf( |
||
| 134 | 1 | 'Invalid domain property collection class "%s"', |
|
| 135 | 1 | $this->domainPropertyCClass |
|
| 136 | ), |
||
| 137 | 1 | PropertyInvalidArgumentException::INVALID_DOMAIN_PROPERTY_COLLECTION_CLASS |
|
| 138 | ); |
||
| 139 | } |
||
| 140 | |||
| 141 | // Right after instantiation it's always the current revision |
||
| 142 | 20 | $this->path = $path->setRevision(Revision::current()); |
|
|
|
|||
| 143 | |||
| 144 | // Load the current revision data |
||
| 145 | 20 | $this->loadRevisionData($payload, $propertyData); |
|
| 146 | |||
| 147 | // Save the latest revision index |
||
| 148 | 19 | $this->latestRevision = $this->getRevision(); |
|
| 149 | 19 | } |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Load object revision data |
||
| 153 | * |
||
| 154 | * @param string $payload Object payload |
||
| 155 | * @param array $propertyData Property data |
||
| 156 | */ |
||
| 157 | 20 | protected function loadRevisionData($payload = '', array $propertyData = []) |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Return whether the object is in mutated state |
||
| 210 | * |
||
| 211 | * @return boolean Mutated state |
||
| 212 | */ |
||
| 213 | 3 | public function isMutated() |
|
| 217 | |||
| 218 | /** |
||
| 219 | * Use a specific object revision |
||
| 220 | * |
||
| 221 | * @param Revision $revision Revision to be used |
||
| 222 | * @return ObjectInterface Object |
||
| 223 | * @throws OutOfBoundsException If the requested revision is invalid |
||
| 224 | */ |
||
| 225 | 18 | public function useRevision(Revision $revision) |
|
| 226 | { |
||
| 227 | 18 | $isCurrentRevision = false; |
|
| 228 | |||
| 229 | // If the requested revision is invalid |
||
| 230 | 18 | if (!$revision->isCurrent() && |
|
| 231 | 18 | (($revision->getRevision() < 1) || ($revision->getRevision() > $this->latestRevision->getRevision())) |
|
| 232 | ) { |
||
| 233 | throw new OutOfBoundsException(sprintf('Invalid object revision "%s"', $revision->getRevision()), |
||
| 234 | OutOfBoundsException::INVALID_OBJECT_REVISION); |
||
| 235 | } |
||
| 236 | |||
| 237 | // If the current revision got requested |
||
| 238 | 18 | if ($revision->isCurrent()) { |
|
| 239 | 18 | $isCurrentRevision = true; |
|
| 240 | 18 | $revision = $this->latestRevision; |
|
| 241 | } |
||
| 242 | |||
| 243 | // If the requested revision is not already used |
||
| 244 | 18 | if ($revision != $this->getRevision()) { |
|
| 245 | /** @var ManagerInterface $objectManager */ |
||
| 246 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
||
| 247 | |||
| 248 | // Load the requested object revision resource |
||
| 249 | /** @var Revision $newRevision */ |
||
| 250 | $newRevision = $isCurrentRevision ? Revision::current() : $revision; |
||
| 251 | /** @var RepositoryPath $newRevisionPath */ |
||
| 252 | $newRevisionPath = $this->path->setRevision($newRevision); |
||
| 253 | $revisionResource = $objectManager->loadObject($newRevisionPath); |
||
| 254 | |||
| 255 | // Load the revision resource data |
||
| 256 | $this->loadRevisionData($revisionResource->getPayload(), $revisionResource->getPropertyData()); |
||
| 257 | |||
| 258 | // Set the current revision path |
||
| 259 | $this->path = $newRevisionPath; |
||
| 260 | } |
||
| 261 | |||
| 262 | 18 | return $this; |
|
| 263 | } |
||
| 264 | |||
| 265 | /** |
||
| 266 | * Return the object repository path |
||
| 267 | * |
||
| 268 | * @return RepositoryPathInterface Object repository path |
||
| 269 | */ |
||
| 270 | 19 | public function getRepositoryPath() |
|
| 274 | |||
| 275 | /** |
||
| 276 | * Return the object property data |
||
| 277 | * |
||
| 278 | * @return array Object property data |
||
| 279 | */ |
||
| 280 | 5 | public function getPropertyData() |
|
| 294 | |||
| 295 | /** |
||
| 296 | * Set the object state to mutated |
||
| 297 | */ |
||
| 298 | 3 | protected function setMutatedState() |
|
| 299 | { |
||
| 300 | // If this object is not in mutated state yet |
||
| 301 | 3 | if (!($this->state & self::STATE_MUTATED) && !$this->isDraft()) { |
|
| 302 | // TODO: Send signal |
||
| 303 | 3 | $this->convertToDraft(); |
|
| 304 | } |
||
| 305 | |||
| 306 | // Enable the mutated (and dirty) state |
||
| 307 | 3 | $this->state |= (self::STATE_DIRTY | self::STATE_MUTATED); |
|
| 308 | 3 | } |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Return the object draft mode |
||
| 312 | * |
||
| 313 | * @return boolean Object draft mode |
||
| 314 | */ |
||
| 315 | 4 | public function isDraft() |
|
| 319 | |||
| 320 | /** |
||
| 321 | * Return whether the object is in published state |
||
| 322 | * |
||
| 323 | * @return boolean Published state |
||
| 324 | */ |
||
| 325 | 4 | public function isPublished() |
|
| 329 | |||
| 330 | /** |
||
| 331 | * Convert this object revision into a draft |
||
| 332 | */ |
||
| 333 | 3 | protected function convertToDraft() |
|
| 360 | |||
| 361 | /** |
||
| 362 | * Return the absolute object URL |
||
| 363 | * |
||
| 364 | * @return string |
||
| 365 | */ |
||
| 366 | 4 | public function getAbsoluteUrl() |
|
| 370 | |||
| 371 | /** |
||
| 372 | * Persist the current object revision |
||
| 373 | * |
||
| 374 | * @return ObjectInterface Object |
||
| 375 | */ |
||
| 376 | 1 | public function persist() |
|
| 377 | { |
||
| 378 | // If this is not the latest revision |
||
| 379 | 1 | if ($this->getRevision() != $this->latestRevision) { |
|
| 380 | throw new RuntimeException( |
||
| 381 | sprintf( |
||
| 382 | 'Cannot persist revision %s/%s', |
||
| 383 | $this->getRevision()->getRevision(), |
||
| 384 | $this->latestRevision->getRevision() |
||
| 385 | ), |
||
| 386 | RuntimeException::CANNOT_PERSIST_EARLIER_REVISION |
||
| 387 | ); |
||
| 388 | } |
||
| 389 | |||
| 390 | // Update the object repository |
||
| 391 | 1 | $this->path->getRepository()->updateObject($this); |
|
| 392 | |||
| 393 | // Reset state |
||
| 394 | 1 | $this->state = self::STATE_CLEAN; |
|
| 395 | |||
| 396 | 1 | return $this; |
|
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Publish the current object revision |
||
| 401 | * |
||
| 402 | * @return ObjectInterface Object |
||
| 403 | */ |
||
| 404 | 1 | public function publish() |
|
| 405 | { |
||
| 406 | // If this is a draft |
||
| 407 | 1 | if ($this->isDraft()) { |
|
| 408 | // TODO: Send signal |
||
| 409 | |||
| 410 | // Create draft system properties |
||
| 411 | 1 | $this->systemProperties = $this->systemProperties->publish(); |
|
| 412 | |||
| 413 | // Adapt the system properties collection state |
||
| 414 | 1 | $this->collectionStates[SystemProperties::COLLECTION] = spl_object_hash($this->systemProperties); |
|
| 415 | |||
| 416 | // Set the draft flag on the repository path |
||
| 417 | 1 | $this->path = $this->path->setDraft(false); |
|
| 418 | |||
| 419 | // Flag this object as dirty |
||
| 420 | 1 | $this->setPublishedState(); |
|
| 421 | } |
||
| 422 | |||
| 423 | 1 | return $this; |
|
| 424 | } |
||
| 425 | |||
| 426 | /** |
||
| 427 | * Set the object state to published |
||
| 428 | */ |
||
| 429 | 1 | protected function setPublishedState() |
|
| 430 | { |
||
| 431 | // If this object is not in dirty state yet |
||
| 432 | 1 | if (!($this->state & self::STATE_PUBLISHED)) { |
|
| 433 | // TODO: Send signal |
||
| 434 | } |
||
| 435 | |||
| 436 | // Enable the dirty state |
||
| 437 | 1 | $this->state |= (self::STATE_DIRTY | self::STATE_PUBLISHED); |
|
| 438 | 1 | } |
|
| 439 | |||
| 440 | /** |
||
| 441 | * Return whether the object is in dirty state |
||
| 442 | * |
||
| 443 | * @return boolean Dirty state |
||
| 444 | */ |
||
| 445 | 3 | public function isDirty() |
|
| 449 | |||
| 450 | /** |
||
| 451 | * Set the object state to dirty |
||
| 452 | */ |
||
| 453 | 2 | protected function setDirtyState() |
|
| 454 | { |
||
| 455 | // If this object is not in dirty state yet |
||
| 463 | } |
||
| 464 |
If a variable is not always an object, we recommend to add an additional type check to ensure your method call is safe: