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 = []) |
|
| 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 = []) |
|
| 131 | { |
||
| 132 | 29 | $this->payload = $payload; |
|
| 133 | |||
| 134 | // Instantiate the system properties |
||
| 135 | 29 | $systemPropertyData = (empty($propertyData[SystemProperties::COLLECTION]) || |
|
| 136 | 29 | !is_array( |
|
| 137 | 29 | $propertyData[SystemProperties::COLLECTION] |
|
| 138 | 29 | )) ? [] : $propertyData[SystemProperties::COLLECTION]; |
|
| 139 | 29 | $this->systemProperties = Kernel::create(SystemProperties::class, [$systemPropertyData, $this]); |
|
| 140 | |||
| 141 | // Instantiate the meta properties |
||
| 142 | 4 | $metaPropertyData = (empty($propertyData[MetaProperties::COLLECTION]) || |
|
| 143 | !is_array( |
||
| 144 | $propertyData[MetaProperties::COLLECTION] |
||
| 145 | 4 | )) ? [] : $propertyData[MetaProperties::COLLECTION]; |
|
| 146 | /** @var MetaProperties $metaProperties */ |
||
| 147 | 4 | $metaProperties = Kernel::create(MetaProperties::class, [$metaPropertyData, $this]); |
|
| 148 | 4 | $this->setMetaProperties($metaProperties, true); |
|
| 149 | |||
| 150 | // Instantiate the domain properties |
||
| 151 | 4 | $domainPropertyData = (empty($propertyData[AbstractDomainProperties::COLLECTION]) || |
|
| 152 | !is_array( |
||
| 153 | $propertyData[AbstractDomainProperties::COLLECTION] |
||
| 154 | 4 | )) ? [] : $propertyData[AbstractDomainProperties::COLLECTION]; |
|
| 155 | /** @var AbstractDomainProperties $domainProperties */ |
||
| 156 | 4 | $domainProperties = Kernel::create($this->domainPropertyCClass, [$domainPropertyData, $this]); |
|
| 157 | 4 | $this->setDomainProperties($domainProperties, true); |
|
| 158 | |||
| 159 | // Instantiate the processing instructions |
||
| 160 | 4 | $procInstData = (empty($propertyData[ProcessingInstructions::COLLECTION]) || |
|
| 161 | !is_array( |
||
| 162 | $propertyData[ProcessingInstructions::COLLECTION] |
||
| 163 | 4 | )) ? [] : $propertyData[ProcessingInstructions::COLLECTION]; |
|
| 164 | /** @var ProcessingInstructions $procInstCollection */ |
||
| 165 | 4 | $procInstCollection = Kernel::create(ProcessingInstructions::class, [$procInstData, $this]); |
|
| 166 | 4 | $this->setProcessingInstructions($procInstCollection, true); |
|
| 167 | |||
| 168 | // Instantiate the object relations |
||
| 169 | 4 | $relationData = (empty($propertyData[Relations::COLLECTION]) || |
|
| 170 | !is_array( |
||
| 171 | $propertyData[Relations::COLLECTION] |
||
| 172 | 4 | )) ? [] : $propertyData[Relations::COLLECTION]; |
|
| 173 | /** @var Relations $relationCollection */ |
||
| 174 | 4 | $relationCollection = Kernel::create(Relations::class, [$relationData, $this]); |
|
| 175 | 4 | $this->setRelations($relationCollection, true); |
|
| 176 | |||
| 177 | // Reset the object state |
||
| 178 | 4 | $this->resetState(); |
|
| 179 | 4 | } |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Return whether this object already has a draft revision |
||
| 183 | */ |
||
| 184 | 4 | protected function hasDraft() |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Update the object path |
||
| 197 | */ |
||
| 198 | 4 | protected function updatePath() |
|
| 207 | |||
| 208 | /** |
||
| 209 | * Return this object's current revision |
||
| 210 | * |
||
| 211 | * @return Revision Current revision |
||
| 212 | */ |
||
| 213 | 3 | protected function getCurrentRevision() |
|
| 214 | { |
||
| 215 | 3 | if ($this->latestRevision->isDraft() && ($this->latestRevision->getRevision() > 1)) { |
|
| 216 | 1 | return Kernel::create(Revision::class, [$this->latestRevision->getRevision() - 1, false]); |
|
| 217 | } |
||
| 218 | 3 | 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 | 2 | public function useRevision(Revision $revision) |
|
| 267 | |||
| 268 | /** |
||
| 269 | * Return the object repository path |
||
| 270 | * |
||
| 271 | * @return RepositoryPathInterface Object repository path |
||
| 272 | */ |
||
| 273 | 4 | public function getRepositoryPath() |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Return the object property data |
||
| 280 | * |
||
| 281 | * @return array Object property data |
||
| 282 | */ |
||
| 283 | 4 | public function getPropertyData() |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Return the absolute object URL |
||
| 300 | * |
||
| 301 | * @return string |
||
| 302 | */ |
||
| 303 | public function getAbsoluteUrl() |
||
| 304 | { |
||
| 305 | return getenv('APPARAT_BASE_URL').ltrim($this->path->getRepository()->getUrl(), '/').strval($this->path); |
||
| 306 | } |
||
| 307 | |||
| 308 | /** |
||
| 309 | * Persist the current object revision |
||
| 310 | * |
||
| 311 | * @return ObjectInterface Object |
||
| 312 | */ |
||
| 313 | 4 | public function persist() |
|
| 314 | { |
||
| 315 | // If this is not the latest revision |
||
| 316 | 4 | if ($this->getRevision()->getRevision() !== $this->latestRevision->getRevision()) { |
|
| 317 | 1 | throw new RuntimeException( |
|
| 318 | 1 | sprintf( |
|
| 319 | 1 | 'Cannot persist revision %s/%s', |
|
| 320 | 1 | $this->getRevision()->getRevision(), |
|
| 321 | 1 | $this->latestRevision->getRevision() |
|
| 322 | 1 | ), |
|
| 323 | RuntimeException::CANNOT_PERSIST_EARLIER_REVISION |
||
| 324 | 1 | ); |
|
| 325 | } |
||
| 326 | |||
| 327 | // Update the object repository |
||
| 328 | 4 | $this->path->getRepository()->updateObject($this); |
|
| 329 | |||
| 330 | // Reset to a clean state |
||
| 331 | 2 | $this->resetState(); |
|
| 332 | 2 | $this->latestRevision = $this->getRevision(); |
|
| 333 | 2 | $this->updatePath(); |
|
| 334 | |||
| 335 | // Post persistence hook |
||
| 336 | 2 | $this->postPersist(); |
|
| 337 | |||
| 338 | 2 | return $this; |
|
| 339 | } |
||
| 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 | 2 | protected function convertToDraft() |
|
| 418 | } |
||
| 419 |