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 |
||
| 65 | abstract class AbstractObject implements ObjectInterface |
||
| 66 | { |
||
| 67 | /** |
||
| 68 | * Use traits |
||
| 69 | */ |
||
| 70 | use SystemPropertiesTrait, MetaPropertiesTrait, DomainPropertiesTrait, RelationsTrait, |
||
| 71 | ProcessingInstructionsTrait, PayloadTrait, IterableTrait, StatesTrait; |
||
| 72 | |||
| 73 | /** |
||
| 74 | * Repository path |
||
| 75 | * |
||
| 76 | * @var RepositoryPathInterface |
||
| 77 | */ |
||
| 78 | protected $path; |
||
| 79 | /** |
||
| 80 | * Latest revision |
||
| 81 | * |
||
| 82 | * @var Revision |
||
| 83 | */ |
||
| 84 | protected $latestRevision; |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Object constructor |
||
| 88 | * |
||
| 89 | * @param RepositoryPathInterface $path Object repository path |
||
| 90 | * @param string $payload Object payload |
||
| 91 | * @param array $propertyData Property data |
||
| 92 | */ |
||
| 93 | 34 | public function __construct(RepositoryPathInterface $path, $payload = '', array $propertyData = []) |
|
| 94 | { |
||
| 95 | // If the domain property collection class is invalid |
||
| 96 | 34 | if (!$this->domainPropertyCClass |
|
| 97 | 34 | || !class_exists($this->domainPropertyCClass) |
|
| 98 | 34 | || !(new \ReflectionClass($this->domainPropertyCClass))->isSubclassOf(AbstractDomainProperties::class) |
|
| 99 | ) { |
||
| 100 | 1 | throw new PropertyInvalidArgumentException( |
|
| 101 | sprintf( |
||
| 102 | 1 | 'Invalid domain property collection class "%s"', |
|
| 103 | 1 | $this->domainPropertyCClass |
|
| 104 | ), |
||
| 105 | 1 | PropertyInvalidArgumentException::INVALID_DOMAIN_PROPERTY_COLLECTION_CLASS |
|
| 106 | ); |
||
| 107 | } |
||
| 108 | |||
| 109 | // Save the original object path |
||
| 110 | 33 | $this->path = $path; |
|
| 111 | |||
| 112 | // Load the current revision data |
||
| 113 | 33 | $this->loadRevisionData($payload, $propertyData); |
|
| 114 | |||
| 115 | // Determine the latest revision number (considering a possible draft) |
||
| 116 | 32 | $this->latestRevision = $this->hasDraft() |
|
| 117 | ? Kernel::create(Revision::class, [$this->getRevision()->getRevision() + 1, true]) |
||
| 118 | 32 | : $this->getRevision(); |
|
| 119 | |||
| 120 | // Update the object path |
||
| 121 | 32 | $this->updatePath(); |
|
| 122 | 32 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Load object revision data |
||
| 126 | * |
||
| 127 | * @param string $payload Object payload |
||
| 128 | * @param array $propertyData Property data |
||
| 129 | */ |
||
| 130 | 33 | protected function loadRevisionData($payload = '', array $propertyData = []) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Return whether this object already has a draft revision |
||
| 183 | * |
||
| 184 | * @return bool Whether this object has a draft revision |
||
| 185 | */ |
||
| 186 | 32 | protected function hasDraft() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Update the object path |
||
| 199 | */ |
||
| 200 | 32 | protected function updatePath() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Return this object's current revision |
||
| 212 | * |
||
| 213 | * @return Revision Current revision |
||
| 214 | */ |
||
| 215 | 29 | protected function getCurrentRevision() |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Use a specific object revision |
||
| 225 | * |
||
| 226 | * @param Revision $revision Revision to be used |
||
| 227 | * @return ObjectInterface Object |
||
| 228 | * @throws OutOfBoundsException If a revision beyond the latest one is requested |
||
| 229 | */ |
||
| 230 | 28 | public function useRevision(Revision $revision) |
|
| 231 | { |
||
| 232 | // If a revision beyond the latest one is requested |
||
| 233 | 28 | if (!$revision->isCurrent() && ($revision->getRevision() > $this->latestRevision->getRevision())) { |
|
| 234 | 1 | throw new OutOfBoundsException( |
|
| 235 | 1 | sprintf('Invalid object revision "%s"', $revision->getRevision()), |
|
| 236 | 1 | OutOfBoundsException::INVALID_OBJECT_REVISION |
|
| 237 | ); |
||
| 238 | } |
||
| 239 | |||
| 240 | // Determine whether the current revision was requested |
||
| 241 | 28 | $currentRevision = $this->getCurrentRevision(); |
|
| 242 | 28 | $isCurrentRevision = $revision->isCurrent() || $currentRevision->equals($revision); |
|
| 243 | 28 | if ($isCurrentRevision) { |
|
| 244 | 28 | $revision = $currentRevision; |
|
| 245 | } |
||
| 246 | |||
| 247 | // If the requested revision is not already used |
||
| 248 | 28 | if (!$this->getRevision()->equals($revision)) { |
|
| 249 | |||
| 250 | /** @var ManagerInterface $objectManager */ |
||
| 251 | 1 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
| 252 | /** @var Revision $newRevision */ |
||
| 253 | 1 | $newRevision = $isCurrentRevision ? Revision::current() : $revision; |
|
| 254 | /** @var RepositoryPath $newRevisionPath */ |
||
| 255 | 1 | $newRevisionPath = $this->path->setRevision($newRevision); |
|
| 256 | |||
| 257 | // Instantiate the requested revision resource |
||
| 258 | 1 | $revisionResource = $objectManager->loadObjectResource($newRevisionPath, SelectorInterface::ALL); |
|
| 259 | |||
| 260 | // Load the revision resource data |
||
| 261 | 1 | $this->loadRevisionData($revisionResource->getPayload(), $revisionResource->getPropertyData()); |
|
| 262 | |||
| 263 | // Update the object path |
||
| 264 | 1 | $this->updatePath(); |
|
| 265 | } |
||
| 266 | |||
| 267 | 28 | return $this; |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return the object repository path |
||
| 272 | * |
||
| 273 | * @return RepositoryPathInterface Object repository path |
||
| 274 | */ |
||
| 275 | 34 | public function getRepositoryPath() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Return the object property data |
||
| 282 | * |
||
| 283 | * @return array Object property data |
||
| 284 | */ |
||
| 285 | 9 | public function getPropertyData() |
|
| 299 | |||
| 300 | /** |
||
| 301 | * Return the absolute object URL |
||
| 302 | * |
||
| 303 | * @return string |
||
| 304 | */ |
||
| 305 | 4 | public function getAbsoluteUrl() |
|
| 309 | |||
| 310 | /** |
||
| 311 | * Persist the current object revision |
||
| 312 | * |
||
| 313 | * @return ObjectInterface Object |
||
| 314 | */ |
||
| 315 | 5 | public function persist() |
|
| 316 | { |
||
| 317 | // If this is not the latest revision |
||
| 318 | 5 | if ($this->getRevision()->getRevision() !== $this->latestRevision->getRevision()) { |
|
| 319 | 1 | throw new RuntimeException( |
|
| 320 | sprintf( |
||
| 321 | 1 | 'Cannot persist revision %s/%s', |
|
| 322 | 1 | $this->getRevision()->getRevision(), |
|
| 323 | 1 | $this->latestRevision->getRevision() |
|
| 324 | ), |
||
| 325 | 1 | RuntimeException::CANNOT_PERSIST_EARLIER_REVISION |
|
| 326 | ); |
||
| 327 | } |
||
| 328 | |||
| 329 | // Update the object repository |
||
| 330 | 5 | $this->path->getRepository()->updateObject($this); |
|
| 331 | |||
| 332 | // Reset to a clean state |
||
| 333 | 3 | $this->resetState(); |
|
| 334 | 3 | $this->latestRevision = $this->getRevision(); |
|
| 335 | 3 | $this->updatePath(); |
|
| 336 | |||
| 337 | // Post persistence hook |
||
| 338 | 3 | $this->postPersist(); |
|
| 339 | |||
| 340 | 2 | return $this; |
|
| 341 | } |
||
| 342 | |||
| 343 | /** |
||
| 344 | * Post persistence hook |
||
| 345 | * |
||
| 346 | * @return void |
||
| 347 | */ |
||
| 348 | 3 | protected function postPersist() |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Publish the current object revision |
||
| 354 | * |
||
| 355 | * @return ObjectInterface Object |
||
| 356 | */ |
||
| 357 | 2 | public function publish() |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Delete the object and all its revisions |
||
| 371 | * |
||
| 372 | * @return ObjectInterface Object |
||
| 373 | */ |
||
| 374 | 3 | public function delete() |
|
| 384 | |||
| 385 | /** |
||
| 386 | * Undelete the object and all its revisions |
||
| 387 | * |
||
| 388 | * @return ObjectInterface Object |
||
| 389 | */ |
||
| 390 | 2 | public function undelete() |
|
| 400 | |||
| 401 | /** |
||
| 402 | * Convert this object revision into a draft |
||
| 403 | */ |
||
| 404 | 5 | protected function convertToDraft() |
|
| 420 | } |
||
| 421 |