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) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Set values on a field |
||
| 247 | * |
||
| 248 | * @param FieldCollection $field |
||
| 249 | * @param $value |
||
| 250 | */ |
||
| 251 | 18 | protected function setOnField(FieldCollection $field, $value) |
|
| 269 | |||
| 270 | /** |
||
| 271 | * Find the latest valid revision for this entity |
||
| 272 | * |
||
| 273 | * @param int $id |
||
| 274 | * @param int $language_id |
||
| 275 | * @return static |
||
| 276 | */ |
||
| 277 | 12 | public static function find($id, $language_id) |
|
| 307 | |||
| 308 | /** |
||
| 309 | * Save a revision |
||
| 310 | * |
||
| 311 | * @param bool $newRevision Should we create a new revision, false by default |
||
| 312 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
| 313 | */ |
||
| 314 | 15 | public function save($newRevision = false, $publishRevision = true) |
|
| 351 | |||
| 352 | /** |
||
| 353 | * Save the content |
||
| 354 | */ |
||
| 355 | 15 | protected function saveContent() |
|
| 359 | |||
| 360 | /** |
||
| 361 | * Save the revision |
||
| 362 | * |
||
| 363 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
| 364 | */ |
||
| 365 | 15 | protected function saveRevision($publishRevision) |
|
| 378 | |||
| 379 | /** |
||
| 380 | * Unpublish the revisions other than this one. |
||
| 381 | * Only for the same content_id and language_id |
||
| 382 | */ |
||
| 383 | 15 | protected function unpublishOtherRevisions() |
|
| 395 | |||
| 396 | /** |
||
| 397 | * Save a single field instance |
||
| 398 | * |
||
| 399 | * @param Field $field The field instance to save |
||
| 400 | * @param bool $newRevision Should we create a new revision? |
||
| 401 | */ |
||
| 402 | 15 | protected function saveField(Field $field, $newRevision) |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Convert the Entity to an array. |
||
| 419 | * |
||
| 420 | * @return array |
||
| 421 | */ |
||
| 422 | 16 | public function toArray() |
|
| 435 | } |
||
| 436 |
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.