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; |
||
| 21 | abstract class Entity |
||
| 22 | { |
||
| 23 | /** |
||
| 24 | * @var array<class> The list of field types, filled with the configuration |
||
| 25 | */ |
||
| 26 | public static $types; |
||
| 27 | |||
| 28 | /** |
||
| 29 | * The content represented by this entity |
||
| 30 | * |
||
| 31 | * @var Content |
||
| 32 | */ |
||
| 33 | protected $content; //id, created_at, type, published |
||
| 34 | |||
| 35 | /** |
||
| 36 | * The revision represented by this entity |
||
| 37 | * |
||
| 38 | * @var Revision |
||
| 39 | */ |
||
| 40 | protected $revision; //language_id, updated_at, type, published |
||
| 41 | |||
| 42 | /** |
||
| 43 | * The fields in this entity |
||
| 44 | * |
||
| 45 | * @var array<FieldCollection> |
||
| 46 | */ |
||
| 47 | protected $data; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Entity constructor. |
||
| 51 | * |
||
| 52 | * @param int $language_id The language this specific entity is in |
||
| 53 | */ |
||
| 54 | 57 | public function __construct($language_id) |
|
| 67 | |||
| 68 | /** |
||
| 69 | * Creates the Content, Revision and FieldCollections |
||
| 70 | * |
||
| 71 | * @param array $fields The fields and their configurations |
||
| 72 | * @throws InvalidFieldTypeException |
||
| 73 | * @throws ReservedFieldNameException |
||
| 74 | */ |
||
| 75 | 54 | protected function initialize(array $fields) |
|
| 84 | |||
| 85 | /** |
||
| 86 | * Validate configuration and prepare a FieldCollection |
||
| 87 | * |
||
| 88 | * @param string $field |
||
| 89 | * @param array $settings |
||
| 90 | * @throws InvalidFieldTypeException |
||
| 91 | * @throws ReservedFieldNameException |
||
| 92 | * @return FieldCollection |
||
| 93 | */ |
||
| 94 | 54 | protected function initializeField($field, $settings) |
|
| 112 | |||
| 113 | /** |
||
| 114 | * Return the fields in this entity |
||
| 115 | * |
||
| 116 | * @return array |
||
| 117 | */ |
||
| 118 | abstract public function getFields(); |
||
| 119 | |||
| 120 | /** |
||
| 121 | * Get the database friendly content type |
||
| 122 | * |
||
| 123 | * @return string |
||
| 124 | */ |
||
| 125 | 54 | public static function getContentType() |
|
| 129 | |||
| 130 | /** |
||
| 131 | * Create a new revision based on the same content ID but without the content. |
||
| 132 | * Very useful if you want to add a new language |
||
| 133 | * |
||
| 134 | * @param int $language_id |
||
| 135 | * @return static |
||
| 136 | */ |
||
| 137 | 3 | public function newRevision($language_id = null) |
|
| 144 | |||
| 145 | /** |
||
| 146 | * Check if the field is related to the content |
||
| 147 | * |
||
| 148 | * @param string $field |
||
| 149 | * @return bool |
||
| 150 | */ |
||
| 151 | 54 | protected function isContentField($field) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Check if the field exists on the entity |
||
| 158 | * |
||
| 159 | * @param string $field |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | 42 | public function hasField($field) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * @param string $field |
||
| 169 | * @return FieldCollection |
||
| 170 | */ |
||
| 171 | 36 | public function getField($field) |
|
| 175 | |||
| 176 | /** |
||
| 177 | * Check if the field is related to the revision |
||
| 178 | * |
||
| 179 | * @param string $field |
||
| 180 | * @return bool |
||
| 181 | */ |
||
| 182 | 51 | protected function isRevisionField($field) |
|
| 186 | |||
| 187 | /** |
||
| 188 | * Dynamically retrieve attributes on the model. |
||
| 189 | * |
||
| 190 | * @param string $key |
||
| 191 | * @throws NonExistentFieldException |
||
| 192 | * @return $this|bool|\Carbon\Carbon|\DateTime|mixed|static |
||
| 193 | */ |
||
| 194 | 42 | public function __get($key) |
|
| 214 | |||
| 215 | /** |
||
| 216 | * Dynamically set attributes on the model. |
||
| 217 | * |
||
| 218 | * @param string $key |
||
| 219 | * @param mixed $value |
||
| 220 | * @throws NonExistentFieldException |
||
| 221 | */ |
||
| 222 | 48 | public function __set($key, $value) |
|
| 259 | |||
| 260 | /** |
||
| 261 | * Find the latest valid revision for this entity |
||
| 262 | * |
||
| 263 | * @param int $id |
||
| 264 | * @param int $language_id |
||
| 265 | * @return static |
||
| 266 | */ |
||
| 267 | 12 | public static function find($id, $language_id) |
|
| 297 | |||
| 298 | /** |
||
| 299 | * Save a revision |
||
| 300 | * |
||
| 301 | * @param bool $newRevision Should we create a new revision, false by default |
||
| 302 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
| 303 | */ |
||
| 304 | 15 | public function save($newRevision = false, $publishRevision = true) |
|
| 341 | |||
| 342 | /** |
||
| 343 | * Save the content |
||
| 344 | */ |
||
| 345 | 15 | protected function saveContent() |
|
| 349 | |||
| 350 | /** |
||
| 351 | * Save the revision |
||
| 352 | * |
||
| 353 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
| 354 | */ |
||
| 355 | 15 | protected function saveRevision($publishRevision) |
|
| 368 | |||
| 369 | /** |
||
| 370 | * Unpublish the revisions other than this one. |
||
| 371 | * Only for the same content_id and language_id |
||
| 372 | */ |
||
| 373 | 15 | protected function unpublishOtherRevisions() |
|
| 385 | |||
| 386 | /** |
||
| 387 | * Save a single field instance |
||
| 388 | * |
||
| 389 | * @param Field $field The field instance to save |
||
| 390 | * @param boolean $newRevision Should we create a new revision? |
||
| 391 | */ |
||
| 392 | 15 | protected function saveField(Field $field, $newRevision) |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Convert the Entity to an array. |
||
| 409 | * |
||
| 410 | * @return array |
||
| 411 | */ |
||
| 412 | 16 | public function toArray() |
|
| 425 | } |
||
| 426 |
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.