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