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; |
||
| 25 | abstract class Entity |
||
| 26 | { |
||
| 27 | /** |
||
| 28 | * @var array<class> The list of field types, filled with the configuration |
||
| 29 | */ |
||
| 30 | public static $types; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * The content represented by this entity |
||
| 34 | * |
||
| 35 | * @var Content |
||
| 36 | */ |
||
| 37 | protected $content; //id, created_at, type, published |
||
| 38 | |||
| 39 | /** |
||
| 40 | * The revision represented by this entity |
||
| 41 | * |
||
| 42 | * @var Revision |
||
| 43 | */ |
||
| 44 | protected $revision; //language_id, updated_at, type, published |
||
| 45 | |||
| 46 | /** |
||
| 47 | * The fields in this entity |
||
| 48 | * |
||
| 49 | * @var array<FieldCollection> |
||
| 50 | */ |
||
| 51 | protected $data; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Entity constructor. |
||
| 55 | * |
||
| 56 | * @param int $language_id The language this specific entity is in |
||
| 57 | */ |
||
| 58 | 81 | public function __construct($language_id) |
|
| 71 | |||
| 72 | /** |
||
| 73 | * Creates the Content, Revision and FieldCollections |
||
| 74 | * |
||
| 75 | * @param array $fields The fields and their configurations |
||
| 76 | * @throws InvalidFieldTypeException |
||
| 77 | * @throws ReservedFieldNameException |
||
| 78 | */ |
||
| 79 | 78 | protected function initialize(array $fields) |
|
| 88 | |||
| 89 | /** |
||
| 90 | * Validate configuration and prepare a FieldCollection |
||
| 91 | * |
||
| 92 | * @param string $field |
||
| 93 | * @param array $settings |
||
| 94 | * @throws InvalidFieldTypeException |
||
| 95 | * @throws ReservedFieldNameException |
||
| 96 | * @return FieldCollection |
||
| 97 | */ |
||
| 98 | 78 | protected function initializeField($field, $settings) |
|
| 116 | |||
| 117 | /** |
||
| 118 | * Return the fields in this entity |
||
| 119 | * |
||
| 120 | * @return array |
||
| 121 | */ |
||
| 122 | abstract public function getFields(); |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Get the database friendly content type |
||
| 126 | * |
||
| 127 | * @return string |
||
| 128 | */ |
||
| 129 | 78 | public static function getContentType() |
|
| 133 | |||
| 134 | /** |
||
| 135 | * Create a new revision based on the same content ID but without the content. |
||
| 136 | * Very useful if you want to add a new language |
||
| 137 | * |
||
| 138 | * @param int $language_id |
||
| 139 | * @return static |
||
| 140 | */ |
||
| 141 | 3 | public function newRevision($language_id = null) |
|
| 148 | |||
| 149 | /** |
||
| 150 | * Check if the field is related to the content |
||
| 151 | * |
||
| 152 | * @param string $field |
||
| 153 | * @return bool |
||
| 154 | */ |
||
| 155 | 78 | protected function isContentField($field) |
|
| 159 | |||
| 160 | /** |
||
| 161 | * Check if the field exists on the entity |
||
| 162 | * |
||
| 163 | * @param string $field |
||
| 164 | * @return bool |
||
| 165 | */ |
||
| 166 | 60 | public function hasField($field) |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Get a field's FieldCollection. |
||
| 173 | * |
||
| 174 | * Be careful as this gives you the real field instances. |
||
| 175 | * |
||
| 176 | * @param string $field |
||
| 177 | * @return FieldCollection |
||
| 178 | */ |
||
| 179 | 54 | public function getField($field) |
|
| 183 | |||
| 184 | /** |
||
| 185 | * Check if the field is related to the revision |
||
| 186 | * |
||
| 187 | * @param string $field |
||
| 188 | * @return bool |
||
| 189 | */ |
||
| 190 | 75 | protected function isRevisionField($field) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Dynamically retrieve attributes on the model. |
||
| 197 | * |
||
| 198 | * @param string $key |
||
| 199 | * @throws NonExistentFieldException |
||
| 200 | * @return $this|bool|\Carbon\Carbon|\DateTime|mixed|static |
||
| 201 | */ |
||
| 202 | 63 | public function __get($key) |
|
| 222 | |||
| 223 | /** |
||
| 224 | * Dynamically set attributes on the model. |
||
| 225 | * |
||
| 226 | * @param string $key |
||
| 227 | * @param mixed $value |
||
| 228 | * @throws NonExistentFieldException |
||
| 229 | */ |
||
| 230 | 72 | public function __set($key, $value) |
|
| 252 | |||
| 253 | /** |
||
| 254 | * Set values on a field |
||
| 255 | * |
||
| 256 | * @param FieldCollection $field |
||
| 257 | * @param $value |
||
| 258 | */ |
||
| 259 | 18 | protected function setOnField(FieldCollection $field, $value) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Get all field types in this Entity. |
||
| 280 | * |
||
| 281 | * @return Collection<string> |
||
|
|
|||
| 282 | */ |
||
| 283 | 20 | protected function getFieldTypes() |
|
| 295 | |||
| 296 | /** |
||
| 297 | * Find the latest valid revision for this entity |
||
| 298 | * |
||
| 299 | * @param int $id |
||
| 300 | * @param int $language_id |
||
| 301 | * @return static |
||
| 302 | * @throws EntityNotFoundException |
||
| 303 | * @throws NoPublishedRevisionForLanguageException |
||
| 304 | * @throws NoRevisionForLanguageException |
||
| 305 | */ |
||
| 306 | 28 | public static function find($id, $language_id) |
|
| 345 | |||
| 346 | /** |
||
| 347 | * Save a revision |
||
| 348 | * |
||
| 349 | * @param bool $newRevision Should we create a new revision, false by default |
||
| 350 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
| 351 | */ |
||
| 352 | 33 | public function save($newRevision = false, $publishRevision = true) |
|
| 389 | |||
| 390 | /** |
||
| 391 | * Save the content |
||
| 392 | */ |
||
| 393 | 33 | protected function saveContent() |
|
| 397 | |||
| 398 | /** |
||
| 399 | * Save the revision |
||
| 400 | * |
||
| 401 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
| 402 | */ |
||
| 403 | 33 | protected function saveRevision($publishRevision) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Unpublish the revisions other than this one. |
||
| 419 | * Only for the same content_id and language_id |
||
| 420 | */ |
||
| 421 | 18 | protected function unpublishOtherRevisions() |
|
| 433 | |||
| 434 | /** |
||
| 435 | * Save a single field instance |
||
| 436 | * |
||
| 437 | * @param Field $field The field instance to save |
||
| 438 | * @param bool $newRevision Should we create a new revision? |
||
| 439 | */ |
||
| 440 | 33 | protected function saveField(Field $field, $newRevision) |
|
| 454 | |||
| 455 | /** |
||
| 456 | * Convert the Entity to an array. |
||
| 457 | * |
||
| 458 | * @return array |
||
| 459 | */ |
||
| 460 | 16 | public function toArray() |
|
| 473 | |||
| 474 | 4 | public function delete($clear = true) |
|
| 497 | |||
| 498 | public function deleteRevision($clear = true) |
||
| 521 | |||
| 522 | 4 | protected function clearFields() |
|
| 533 | } |
||
| 534 |
This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.