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 | 39 | public function __construct(RepositoryPathInterface $path, $payload = '', array $propertyData = []) |
|
| 94 | { |
||
| 95 | // If the domain property collection class is invalid |
||
| 96 | 39 | if (!$this->domainPropertyCClass |
|
| 97 | 39 | || !class_exists($this->domainPropertyCClass) |
|
| 98 | 39 | || !(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 | 38 | $this->path = $path; |
|
| 111 | |||
| 112 | // Load the current revision data |
||
| 113 | 38 | $this->loadRevisionData($payload, $propertyData); |
|
| 114 | |||
| 115 | // Determine the latest revision number (considering a possible draft) |
||
| 116 | 37 | $this->latestRevision = $this->hasDraft() |
|
| 117 | ? Kernel::create(Revision::class, [$this->getRevision()->getRevision() + 1, true]) |
||
| 118 | 37 | : $this->getRevision(); |
|
| 119 | |||
| 120 | // Update the object path |
||
| 121 | 37 | $this->updatePath(); |
|
| 122 | 37 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Load object revision data |
||
| 126 | * |
||
| 127 | * @param string $payload Object payload |
||
| 128 | * @param array $propertyData Property data |
||
| 129 | */ |
||
| 130 | 38 | 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 | 37 | protected function hasDraft() |
|
| 196 | |||
| 197 | /** |
||
| 198 | * Update the object path |
||
| 199 | */ |
||
| 200 | 37 | protected function updatePath() |
|
| 209 | |||
| 210 | /** |
||
| 211 | * Return this object's current revision |
||
| 212 | * |
||
| 213 | * @return Revision Current revision |
||
| 214 | */ |
||
| 215 | 34 | 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 | 33 | public function useRevision(Revision $revision) |
|
| 231 | { |
||
| 232 | // If a revision beyond the latest one is requested |
||
| 233 | 33 | 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 | 33 | $currentRevision = $this->getCurrentRevision(); |
|
| 242 | 33 | $isCurrentRevision = $revision->isCurrent() || $currentRevision->equals($revision); |
|
| 243 | 33 | if ($isCurrentRevision) { |
|
| 244 | 33 | $revision = $currentRevision; |
|
| 245 | } |
||
| 246 | |||
| 247 | // If the requested revision is not already used |
||
| 248 | 33 | 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 | 33 | return $this; |
|
| 268 | } |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Return the object repository path |
||
| 272 | * |
||
| 273 | * @return RepositoryPathInterface Object repository path |
||
| 274 | */ |
||
| 275 | 39 | public function getRepositoryPath() |
|
| 279 | |||
| 280 | /** |
||
| 281 | * Return the object property data |
||
| 282 | * |
||
| 283 | * @param bool $serialize Serialize property objects |
||
| 284 | * @return array Object property data |
||
| 285 | */ |
||
| 286 | 9 | public function getPropertyData($serialize = true) |
|
| 300 | |||
| 301 | /** |
||
| 302 | * Return the absolute object URL |
||
| 303 | * |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | 4 | public function getAbsoluteUrl() |
|
| 310 | |||
| 311 | /** |
||
| 312 | * Return the canonical object URL |
||
| 313 | * |
||
| 314 | * @return string |
||
| 315 | */ |
||
| 316 | public function getCanonicalUrl() |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Persist the current object revision |
||
| 324 | * |
||
| 325 | * @return ObjectInterface Object |
||
| 326 | */ |
||
| 327 | 5 | public function persist() |
|
| 354 | |||
| 355 | /** |
||
| 356 | * Post persistence hook |
||
| 357 | * |
||
| 358 | * @return void |
||
| 359 | */ |
||
| 360 | 3 | protected function postPersist() |
|
| 363 | |||
| 364 | /** |
||
| 365 | * Publish the current object revision |
||
| 366 | * |
||
| 367 | * @return ObjectInterface Object |
||
| 368 | */ |
||
| 369 | 2 | public function publish() |
|
| 380 | |||
| 381 | /** |
||
| 382 | * Delete the object and all its revisions |
||
| 383 | * |
||
| 384 | * @return ObjectInterface Object |
||
| 385 | */ |
||
| 386 | 3 | public function delete() |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Undelete the object and all its revisions |
||
| 399 | * |
||
| 400 | * @return ObjectInterface Object |
||
| 401 | */ |
||
| 402 | 2 | public function undelete() |
|
| 412 | |||
| 413 | /** |
||
| 414 | * Convert this object revision into a draft |
||
| 415 | */ |
||
| 416 | 5 | protected function convertToDraft() |
|
| 432 | } |
||
| 433 |