Complex classes like Entity 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 Entity, and based on these observations, apply Extract Interface, too.
1 | <?php namespace Rocket\Entities; |
||
37 | abstract class Entity |
||
38 | { |
||
39 | /** |
||
40 | * @var int This key identifies where the only item is stored in single item fields. |
||
41 | */ |
||
42 | public static $SINGLE_ITEM_KEY = 0; |
||
43 | |||
44 | /** |
||
45 | * @var array<class> The list of field types, filled by the ServiceProvider from a configuration file. |
||
46 | */ |
||
47 | public static $types; |
||
48 | |||
49 | /** |
||
50 | * @var Content The content represented by this entity |
||
51 | */ |
||
52 | protected $content; //id, created_at, type, published |
||
53 | |||
54 | /** |
||
55 | * @var Revision The revision represented by this entity |
||
56 | */ |
||
57 | protected $revision; //language_id, updated_at, type, published |
||
58 | |||
59 | /** |
||
60 | * @var FieldCollection[] The fields in this entity |
||
61 | */ |
||
62 | protected $data; |
||
63 | |||
64 | /** |
||
65 | * Entity constructor. |
||
66 | * |
||
67 | * @param int $language_id The language this specific entity is in |
||
68 | */ |
||
69 | 111 | public function __construct($language_id) |
|
82 | |||
83 | /** |
||
84 | * Creates the Content, Revision and FieldCollections |
||
85 | * |
||
86 | * @param array $fields The fields and their configurations |
||
87 | * @throws InvalidFieldTypeException |
||
88 | * @throws ReservedFieldNameException |
||
89 | */ |
||
90 | 108 | protected function initialize(array $fields) |
|
99 | |||
100 | /** |
||
101 | * Validate configuration and prepare a FieldCollection |
||
102 | * |
||
103 | * @param string $field |
||
104 | * @param array $settings |
||
105 | * @throws InvalidFieldTypeException |
||
106 | * @throws ReservedFieldNameException |
||
107 | * @return FieldCollection |
||
108 | */ |
||
109 | 108 | protected function initializeField($field, $settings) |
|
127 | |||
128 | /** |
||
129 | * Return the fields in this entity |
||
130 | * |
||
131 | * @return array |
||
132 | */ |
||
133 | abstract public function getFields(); |
||
134 | |||
135 | /** |
||
136 | * Get the database friendly content type |
||
137 | * |
||
138 | * @return string |
||
139 | */ |
||
140 | 108 | public static function getContentType() |
|
144 | |||
145 | /** |
||
146 | * Create a new revision based on the same content ID but without the content. |
||
147 | * Very useful if you want to add a new language |
||
148 | * |
||
149 | * @param int $language_id |
||
150 | * @return static |
||
151 | */ |
||
152 | 3 | public function newRevision($language_id = null) |
|
159 | |||
160 | /** |
||
161 | * Check if the field is related to the content |
||
162 | * |
||
163 | * @param string $field |
||
164 | * @return bool |
||
165 | */ |
||
166 | 108 | protected function isContentField($field) |
|
170 | |||
171 | /** |
||
172 | * Check if the field exists on the entity |
||
173 | * |
||
174 | * @param string $field |
||
175 | * @return bool |
||
176 | */ |
||
177 | 90 | public function hasField($field) |
|
181 | |||
182 | /** |
||
183 | * Get a field's FieldCollection. |
||
184 | * |
||
185 | * Be careful as this gives you the real field instances. |
||
186 | * |
||
187 | * @param string $field |
||
188 | * @return FieldCollection |
||
189 | */ |
||
190 | 84 | public function getField($field) |
|
194 | |||
195 | /** |
||
196 | * Check if the field is related to the revision |
||
197 | * |
||
198 | * @param string $field |
||
199 | * @return bool |
||
200 | */ |
||
201 | 105 | protected function isRevisionField($field) |
|
205 | |||
206 | /** |
||
207 | * Dynamically retrieve attributes on the model. |
||
208 | * |
||
209 | * @param string $key |
||
210 | * @throws NonExistentFieldException |
||
211 | * @return $this|bool|\Carbon\Carbon|\DateTime|mixed|static |
||
212 | */ |
||
213 | 90 | public function __get($key) |
|
251 | |||
252 | /** |
||
253 | * Dynamically set attributes on the model. |
||
254 | * |
||
255 | * @param string $key |
||
256 | * @param mixed $value |
||
257 | * @throws NonExistentFieldException |
||
258 | */ |
||
259 | 102 | public function __set($key, $value) |
|
281 | |||
282 | /** |
||
283 | * Set values on a field |
||
284 | * |
||
285 | * @param FieldCollection $field |
||
286 | * @param $value |
||
287 | * @throws MultipleFieldAssignmentException |
||
288 | */ |
||
289 | 27 | protected function setOnField(FieldCollection $field, $value) |
|
311 | |||
312 | /** |
||
313 | * Get all field types in this Entity. |
||
314 | * |
||
315 | * @return Collection |
||
316 | */ |
||
317 | 43 | protected function getFieldTypes() |
|
329 | |||
330 | /** |
||
331 | * Find the requested Revision. |
||
332 | * |
||
333 | * If a revision_id it will be requested against the requested ID and Language. |
||
334 | * |
||
335 | * If none is requested, it will find the revision that is published in that language. |
||
336 | * |
||
337 | * @param int $id The content ID |
||
338 | * @param int $language_id The language ID |
||
339 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
340 | * @throws NoPublishedRevisionForLanguageException |
||
341 | * @throws NoRevisionForLanguageException |
||
342 | * @throws RevisionEntityMismatchException |
||
343 | * @throws RevisionNotFoundException |
||
344 | * @return Revision |
||
345 | */ |
||
346 | 50 | protected static function findRevision($id, $language_id, $revision_id = null) |
|
383 | |||
384 | /** |
||
385 | * Find the latest valid revision for this entity |
||
386 | * |
||
387 | * @param int $id The content ID |
||
388 | * @param int $language_id The language ID |
||
389 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
390 | * @throws EntityNotFoundException |
||
391 | * @throws NoPublishedRevisionForLanguageException |
||
392 | * @throws NoRevisionForLanguageException |
||
393 | * @throws RevisionEntityMismatchException |
||
394 | * @throws RevisionNotFoundException |
||
395 | * @return static |
||
396 | */ |
||
397 | 57 | public static function find($id, $language_id, $revision_id = null) |
|
420 | |||
421 | /** |
||
422 | * Save a revision |
||
423 | * |
||
424 | * @param bool $newRevision Should we create a new revision, false by default |
||
425 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
426 | */ |
||
427 | 60 | public function save($newRevision = false, $publishRevision = true) |
|
464 | |||
465 | /** |
||
466 | * Save the content |
||
467 | */ |
||
468 | 60 | protected function saveContent() |
|
472 | |||
473 | /** |
||
474 | * Save the revision |
||
475 | * |
||
476 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
477 | */ |
||
478 | 60 | protected function saveRevision($publishRevision) |
|
491 | |||
492 | /** |
||
493 | * Unpublish the revisions other than this one. |
||
494 | * Only for the same content_id and language_id |
||
495 | */ |
||
496 | 51 | protected function unpublishOtherRevisions() |
|
508 | |||
509 | /** |
||
510 | * Save a single field instance |
||
511 | * |
||
512 | * @param Field $field The field instance to save |
||
513 | * @param bool $newRevision Should we create a new revision? |
||
514 | */ |
||
515 | 60 | protected function saveField(Field $field, $newRevision) |
|
529 | |||
530 | /** |
||
531 | * Convert the Entity to an array. |
||
532 | * |
||
533 | * @return array |
||
534 | */ |
||
535 | 27 | public function toArray() |
|
549 | |||
550 | /** |
||
551 | * Delete this entity and all the underlying Revisions. |
||
552 | * |
||
553 | * @param bool $clear Should we clear the fields after deleting the revision ? |
||
554 | */ |
||
555 | 4 | public function delete($clear = true) |
|
578 | |||
579 | /** |
||
580 | * Delete the current revision. |
||
581 | * |
||
582 | * @param bool $clear Should we clear the fields after deleting the revision ? |
||
583 | */ |
||
584 | public function deleteRevision($clear = true) |
||
607 | |||
608 | /** |
||
609 | * Clear all the fields from their content |
||
610 | */ |
||
611 | 7 | protected function clearFields() |
|
622 | |||
623 | /** |
||
624 | * Publish the current revision and unpublish the other revisions of the same language. |
||
625 | */ |
||
626 | 2 | public function publishRevision() |
|
633 | } |
||
634 |