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 | 39 | ) { |
|
100 | 1 | throw new PropertyInvalidArgumentException( |
|
101 | 1 | sprintf( |
|
102 | 1 | 'Invalid domain property collection class "%s"', |
|
103 | 1 | $this->domainPropertyCClass |
|
104 | 1 | ), |
|
105 | PropertyInvalidArgumentException::INVALID_DOMAIN_PROPERTY_COLLECTION_CLASS |
||
106 | 1 | ); |
|
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 | 37 | ? 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) |
|
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() |
|
328 | { |
||
329 | // If this is not the latest revision |
||
330 | 5 | if ($this->getRevision()->getRevision() !== $this->latestRevision->getRevision()) { |
|
331 | 1 | throw new RuntimeException( |
|
332 | 1 | sprintf( |
|
333 | 1 | 'Cannot persist revision %s/%s', |
|
334 | 1 | $this->getRevision()->getRevision(), |
|
335 | 1 | $this->latestRevision->getRevision() |
|
336 | 1 | ), |
|
337 | RuntimeException::CANNOT_PERSIST_EARLIER_REVISION |
||
338 | 1 | ); |
|
339 | } |
||
340 | |||
341 | // Update the object repository |
||
342 | 5 | $this->path->getRepository()->updateObject($this); |
|
343 | |||
344 | // Reset to a clean state |
||
345 | 3 | $this->resetState(); |
|
346 | 3 | $this->latestRevision = $this->getRevision(); |
|
347 | 3 | $this->updatePath(); |
|
348 | |||
349 | // Post persistence hook |
||
350 | 3 | $this->postPersist(); |
|
351 | |||
352 | 2 | return $this; |
|
353 | } |
||
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 |