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; |
||
30 | abstract class Entity |
||
31 | { |
||
32 | /** |
||
33 | * @var int This key identifies where the only item is stored in single item fields. |
||
34 | */ |
||
35 | public static $SINGLE_ITEM_KEY = 0; |
||
36 | |||
37 | /** |
||
38 | * @var array<class> The list of field types, filled by the ServiceProvider from a configuration file. |
||
39 | */ |
||
40 | public static $types; |
||
41 | |||
42 | /** |
||
43 | * @var Content The content represented by this entity |
||
44 | */ |
||
45 | protected $content; //id, created_at, type, published |
||
46 | |||
47 | /** |
||
48 | * @var Revision The revision represented by this entity |
||
49 | */ |
||
50 | protected $revision; //language_id, updated_at, type, published |
||
51 | |||
52 | /** |
||
53 | * @var FieldCollection[] The fields in this entity |
||
54 | */ |
||
55 | protected $data; |
||
56 | |||
57 | /** |
||
58 | * Entity constructor. |
||
59 | * |
||
60 | * @param int $language_id The language this specific entity is in |
||
61 | */ |
||
62 | 111 | public function __construct($language_id) |
|
75 | |||
76 | /** |
||
77 | * Creates the Content, Revision and FieldCollections |
||
78 | * |
||
79 | * @param array $fields The fields and their configurations |
||
80 | * @throws InvalidFieldTypeException |
||
81 | * @throws ReservedFieldNameException |
||
82 | */ |
||
83 | 108 | protected function initialize(array $fields) |
|
92 | |||
93 | /** |
||
94 | * Validate configuration and prepare a FieldCollection |
||
95 | * |
||
96 | * @param string $field |
||
97 | * @param array $settings |
||
98 | * @throws InvalidFieldTypeException |
||
99 | * @throws ReservedFieldNameException |
||
100 | * @return FieldCollection |
||
101 | */ |
||
102 | 108 | protected function initializeField($field, $settings) |
|
120 | |||
121 | /** |
||
122 | * Return the fields in this entity |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | abstract public function getFields(); |
||
127 | |||
128 | /** |
||
129 | * Get the database friendly content type |
||
130 | * |
||
131 | * @return string |
||
132 | */ |
||
133 | 108 | public static function getContentType() |
|
137 | |||
138 | /** |
||
139 | * Create a new revision based on the same content ID but without the content. |
||
140 | * Very useful if you want to add a new language |
||
141 | * |
||
142 | * @param int $language_id |
||
143 | * @return static |
||
144 | */ |
||
145 | 3 | public function newRevision($language_id = null) |
|
152 | |||
153 | /** |
||
154 | * Check if the field is related to the content |
||
155 | * |
||
156 | * @param string $field |
||
157 | * @return bool |
||
158 | */ |
||
159 | 108 | protected function isContentField($field) |
|
163 | |||
164 | /** |
||
165 | * Check if the field exists on the entity |
||
166 | * |
||
167 | * @param string $field |
||
168 | * @return bool |
||
169 | */ |
||
170 | 90 | public function hasField($field) |
|
174 | |||
175 | /** |
||
176 | * Get a field's FieldCollection. |
||
177 | * |
||
178 | * Be careful as this gives you the real field instances. |
||
179 | * |
||
180 | * @param string $field |
||
181 | * @return FieldCollection |
||
182 | */ |
||
183 | 84 | public function getField($field) |
|
187 | |||
188 | /** |
||
189 | * Check if the field is related to the revision |
||
190 | * |
||
191 | * @param string $field |
||
192 | * @return bool |
||
193 | */ |
||
194 | 105 | protected function isRevisionField($field) |
|
198 | |||
199 | /** |
||
200 | * Dynamically retrieve attributes on the model. |
||
201 | * |
||
202 | * @param string $key |
||
203 | * @throws NonExistentFieldException |
||
204 | * @return $this|bool|\Carbon\Carbon|\DateTime|mixed|static |
||
205 | */ |
||
206 | 90 | public function __get($key) |
|
244 | |||
245 | /** |
||
246 | * Dynamically set attributes on the model. |
||
247 | * |
||
248 | * @param string $key |
||
249 | * @param mixed $value |
||
250 | * @throws NonExistentFieldException |
||
251 | */ |
||
252 | 102 | public function __set($key, $value) |
|
274 | |||
275 | /** |
||
276 | * Set values on a field |
||
277 | * |
||
278 | * @param FieldCollection $field |
||
279 | * @param $value |
||
280 | * @throws MultipleFieldAssignmentException |
||
281 | */ |
||
282 | 27 | protected function setOnField(FieldCollection $field, $value) |
|
304 | |||
305 | /** |
||
306 | * Get all field types in this Entity. |
||
307 | * |
||
308 | * @return Collection |
||
309 | */ |
||
310 | 43 | protected function getFieldTypes() |
|
322 | |||
323 | /** |
||
324 | * @param int $id The content ID |
||
325 | * @param int $language_id The language ID |
||
326 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
327 | * @throws NoPublishedRevisionForLanguageException |
||
328 | * @throws NoRevisionForLanguageException |
||
329 | * @throws RevisionEntityMismatchException |
||
330 | * @throws RevisionNotFoundException |
||
331 | * @return Revision |
||
332 | */ |
||
333 | 50 | protected static function findRevision($id, $language_id, $revision_id = null) |
|
366 | |||
367 | /** |
||
368 | * Find the latest valid revision for this entity |
||
369 | * |
||
370 | * @param int $id The content ID |
||
371 | * @param int $language_id The language ID |
||
372 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
373 | * @throws EntityNotFoundException |
||
374 | * @throws NoPublishedRevisionForLanguageException |
||
375 | * @throws NoRevisionForLanguageException |
||
376 | * @throws RevisionEntityMismatchException |
||
377 | * @throws RevisionNotFoundException |
||
378 | * @return static |
||
379 | */ |
||
380 | 57 | public static function find($id, $language_id, $revision_id = null) |
|
403 | |||
404 | /** |
||
405 | * Save a revision |
||
406 | * |
||
407 | * @param bool $newRevision Should we create a new revision, false by default |
||
408 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
409 | */ |
||
410 | 60 | public function save($newRevision = false, $publishRevision = true) |
|
447 | |||
448 | /** |
||
449 | * Save the content |
||
450 | */ |
||
451 | 60 | protected function saveContent() |
|
455 | |||
456 | /** |
||
457 | * Save the revision |
||
458 | * |
||
459 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
460 | */ |
||
461 | 60 | protected function saveRevision($publishRevision) |
|
474 | |||
475 | /** |
||
476 | * Unpublish the revisions other than this one. |
||
477 | * Only for the same content_id and language_id |
||
478 | */ |
||
479 | 51 | protected function unpublishOtherRevisions() |
|
491 | |||
492 | /** |
||
493 | * Save a single field instance |
||
494 | * |
||
495 | * @param Field $field The field instance to save |
||
496 | * @param bool $newRevision Should we create a new revision? |
||
497 | */ |
||
498 | 60 | protected function saveField(Field $field, $newRevision) |
|
512 | |||
513 | /** |
||
514 | * Convert the Entity to an array. |
||
515 | * |
||
516 | * @return array |
||
517 | */ |
||
518 | 27 | public function toArray() |
|
532 | |||
533 | 4 | public function delete($clear = true) |
|
556 | |||
557 | public function deleteRevision($clear = true) |
||
580 | |||
581 | 7 | protected function clearFields() |
|
592 | |||
593 | 2 | public function publishRevision() |
|
600 | } |
||
601 |