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, \Iterator, \Countable |
||
| 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 | 30 | public function __construct(RepositoryPathInterface $path, $payload = '', array $propertyData = []) |
|
| 94 | { |
||
| 95 | // If the domain property collection class is invalid |
||
| 96 | 30 | if (!$this->domainPropertyCClass |
|
| 97 | 30 | || !class_exists($this->domainPropertyCClass) |
|
| 98 | 30 | || !(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 | 29 | $this->path = $path; |
|
| 111 | |||
| 112 | // Load the current revision data |
||
| 113 | 29 | $this->loadRevisionData($payload, $propertyData); |
|
| 114 | |||
| 115 | // Determine the latest revision number (considering a possible draft) |
||
| 116 | 28 | $this->latestRevision = $this->hasDraft() |
|
| 117 | ? Kernel::create(Revision::class, [$this->getRevision()->getRevision() + 1, true]) |
||
| 118 | 28 | : $this->getRevision(); |
|
| 119 | |||
| 120 | // Update the object path |
||
| 121 | 28 | $this->updatePath(); |
|
| 122 | 28 | } |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Load object revision data |
||
| 126 | * |
||
| 127 | * @param string $payload Object payload |
||
| 128 | * @param array $propertyData Property data |
||
| 129 | */ |
||
| 130 | 29 | protected function loadRevisionData($payload = '', array $propertyData = []) |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Return whether this object already has a draft revision |
||
| 183 | */ |
||
| 184 | 28 | protected function hasDraft() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Update the object path |
||
| 197 | */ |
||
| 198 | 28 | protected function updatePath() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Return this object's current revision |
||
| 210 | * |
||
| 211 | * @return Revision Current revision |
||
| 212 | */ |
||
| 213 | 27 | protected function getCurrentRevision() |
|
| 214 | { |
||
| 215 | 27 | if ($this->latestRevision->isDraft() && ($this->latestRevision->getRevision() > 1)) { |
|
| 216 | 1 | return Kernel::create(Revision::class, [$this->latestRevision->getRevision() - 1, false]); |
|
| 217 | } |
||
| 218 | 27 | return $this->latestRevision; |
|
| 219 | } |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Use a specific object revision |
||
| 223 | * |
||
| 224 | * @param Revision $revision Revision to be used |
||
| 225 | * @return ObjectInterface Object |
||
| 226 | * @throws OutOfBoundsException If a revision beyond the latest one is requested |
||
| 227 | */ |
||
| 228 | 26 | public function useRevision(Revision $revision) |
|
| 229 | { |
||
| 230 | // If a revision beyond the latest one is requested |
||
| 231 | 26 | if (!$revision->isCurrent() && ($revision->getRevision() > $this->latestRevision->getRevision())) { |
|
| 232 | 1 | throw new OutOfBoundsException( |
|
| 233 | 1 | sprintf('Invalid object revision "%s"', $revision->getRevision()), |
|
| 234 | 1 | OutOfBoundsException::INVALID_OBJECT_REVISION |
|
| 235 | ); |
||
| 236 | } |
||
| 237 | |||
| 238 | // Determine whether the current revision was requested |
||
| 239 | 26 | $currentRevision = $this->getCurrentRevision(); |
|
| 240 | 26 | $isCurrentRevision = $revision->isCurrent() || $currentRevision->equals($revision); |
|
| 241 | 26 | if ($isCurrentRevision) { |
|
| 242 | 26 | $revision = $currentRevision; |
|
| 243 | } |
||
| 244 | |||
| 245 | // If the requested revision is not already used |
||
| 246 | 26 | if (!$this->getRevision()->equals($revision)) { |
|
| 247 | |||
| 248 | /** @var ManagerInterface $objectManager */ |
||
| 249 | 1 | $objectManager = Kernel::create(Service::class)->getObjectManager(); |
|
| 250 | /** @var Revision $newRevision */ |
||
| 251 | 1 | $newRevision = $isCurrentRevision ? Revision::current() : $revision; |
|
| 252 | /** @var RepositoryPath $newRevisionPath */ |
||
| 253 | 1 | $newRevisionPath = $this->path->setRevision($newRevision); |
|
| 254 | |||
| 255 | // Instantiate the requested revision resource |
||
| 256 | 1 | $revisionResource = $objectManager->loadObjectResource($newRevisionPath, SelectorInterface::ALL); |
|
| 257 | |||
| 258 | // Load the revision resource data |
||
| 259 | 1 | $this->loadRevisionData($revisionResource->getPayload(), $revisionResource->getPropertyData()); |
|
| 260 | |||
| 261 | // Update the object path |
||
| 262 | 1 | $this->updatePath(); |
|
| 263 | } |
||
| 264 | |||
| 265 | 26 | return $this; |
|
| 266 | } |
||
| 267 | |||
| 268 | /** |
||
| 269 | * Return the object repository path |
||
| 270 | * |
||
| 271 | * @return RepositoryPathInterface Object repository path |
||
| 272 | */ |
||
| 273 | 28 | public function getRepositoryPath() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Return the object property data |
||
| 280 | * |
||
| 281 | * @return array Object property data |
||
| 282 | */ |
||
| 283 | 8 | public function getPropertyData() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Return the absolute object URL |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | 4 | public function getAbsoluteUrl() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Persist the current object revision |
||
| 310 | * |
||
| 311 | * @return ObjectInterface Object |
||
| 312 | */ |
||
| 313 | 4 | public function persist() |
|
| 340 | |||
| 341 | /** |
||
| 342 | * Post persistence hook |
||
| 343 | * |
||
| 344 | * @return void |
||
| 345 | */ |
||
| 346 | 2 | protected function postPersist() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Publish the current object revision |
||
| 352 | * |
||
| 353 | * @return ObjectInterface Object |
||
| 354 | */ |
||
| 355 | 2 | public function publish() |
|
| 366 | |||
| 367 | /** |
||
| 368 | * Delete the object and all its revisions |
||
| 369 | * |
||
| 370 | * @return ObjectInterface Object |
||
| 371 | */ |
||
| 372 | 3 | public function delete() |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Undelete the object and all its revisions |
||
| 385 | * |
||
| 386 | * @return ObjectInterface Object |
||
| 387 | */ |
||
| 388 | 2 | public function undelete() |
|
| 398 | |||
| 399 | /** |
||
| 400 | * Convert this object revision into a draft |
||
| 401 | */ |
||
| 402 | 5 | protected function convertToDraft() |
|
| 418 | } |
||
| 419 |