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; |
||
29 | abstract class Entity |
||
30 | { |
||
31 | /** |
||
32 | * @var array<class> The list of field types, filled with the configuration |
||
33 | */ |
||
34 | public static $types; |
||
35 | |||
36 | /** |
||
37 | * The content represented by this entity |
||
38 | * |
||
39 | * @var Content |
||
40 | */ |
||
41 | protected $content; //id, created_at, type, published |
||
42 | |||
43 | /** |
||
44 | * The revision represented by this entity |
||
45 | * |
||
46 | * @var Revision |
||
47 | */ |
||
48 | protected $revision; //language_id, updated_at, type, published |
||
49 | |||
50 | /** |
||
51 | * The fields in this entity |
||
52 | * |
||
53 | * @var array<FieldCollection> |
||
54 | */ |
||
55 | protected $data; |
||
56 | |||
57 | /** |
||
58 | * Entity constructor. |
||
59 | * |
||
60 | * @param int $language_id The language this specific entity is in |
||
61 | */ |
||
62 | 102 | 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 | 99 | 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 | 99 | 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 | 99 | 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 | 99 | 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 | 81 | 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 | 75 | 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 | 96 | 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 | 84 | public function __get($key) |
|
234 | |||
235 | /** |
||
236 | * Dynamically set attributes on the model. |
||
237 | * |
||
238 | * @param string $key |
||
239 | * @param mixed $value |
||
240 | * @throws NonExistentFieldException |
||
241 | */ |
||
242 | 93 | public function __set($key, $value) |
|
264 | |||
265 | /** |
||
266 | * Set values on a field |
||
267 | * |
||
268 | * @param FieldCollection $field |
||
269 | * @param $value |
||
270 | */ |
||
271 | 18 | protected function setOnField(FieldCollection $field, $value) |
|
289 | |||
290 | /** |
||
291 | * Get all field types in this Entity. |
||
292 | * |
||
293 | * @return Collection |
||
294 | */ |
||
295 | 37 | protected function getFieldTypes() |
|
307 | |||
308 | /** |
||
309 | * @param int $id The content ID |
||
310 | * @param int $language_id The language ID |
||
311 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
312 | * @throws NoPublishedRevisionForLanguageException |
||
313 | * @throws NoRevisionForLanguageException |
||
314 | * @throws RevisionEntityMismatchException |
||
315 | * @throws RevisionNotFoundException |
||
316 | * @return Revision |
||
317 | */ |
||
318 | 44 | protected static function findRevision($id, $language_id, $revision_id = null) |
|
351 | |||
352 | /** |
||
353 | * Find the latest valid revision for this entity |
||
354 | * |
||
355 | * @param int $id The content ID |
||
356 | * @param int $language_id The language ID |
||
357 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
358 | * @throws EntityNotFoundException |
||
359 | * @throws NoPublishedRevisionForLanguageException |
||
360 | * @throws NoRevisionForLanguageException |
||
361 | * @throws RevisionEntityMismatchException |
||
362 | * @throws RevisionNotFoundException |
||
363 | * @return static |
||
364 | */ |
||
365 | 51 | public static function find($id, $language_id, $revision_id = null) |
|
388 | |||
389 | /** |
||
390 | * Save a revision |
||
391 | * |
||
392 | * @param bool $newRevision Should we create a new revision, false by default |
||
393 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
394 | */ |
||
395 | 54 | public function save($newRevision = false, $publishRevision = true) |
|
432 | |||
433 | /** |
||
434 | * Save the content |
||
435 | */ |
||
436 | 54 | protected function saveContent() |
|
440 | |||
441 | /** |
||
442 | * Save the revision |
||
443 | * |
||
444 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
445 | */ |
||
446 | 54 | protected function saveRevision($publishRevision) |
|
459 | |||
460 | /** |
||
461 | * Unpublish the revisions other than this one. |
||
462 | * Only for the same content_id and language_id |
||
463 | */ |
||
464 | 45 | protected function unpublishOtherRevisions() |
|
476 | |||
477 | /** |
||
478 | * Save a single field instance |
||
479 | * |
||
480 | * @param Field $field The field instance to save |
||
481 | * @param bool $newRevision Should we create a new revision? |
||
482 | */ |
||
483 | 54 | protected function saveField(Field $field, $newRevision) |
|
497 | |||
498 | /** |
||
499 | * Convert the Entity to an array. |
||
500 | * |
||
501 | * @return array |
||
502 | */ |
||
503 | 21 | public function toArray() |
|
517 | |||
518 | 4 | public function delete($clear = true) |
|
541 | |||
542 | public function deleteRevision($clear = true) |
||
565 | |||
566 | 7 | protected function clearFields() |
|
577 | |||
578 | 2 | public function publishRevision() |
|
585 | } |
||
586 |