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; |
||
| 18 | abstract class Entity |
||
| 19 | { |
||
| 20 | public static $types; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * The content represented by this entity |
||
| 24 | * |
||
| 25 | * @var Content |
||
| 26 | */ |
||
| 27 | protected $content; //id, created_at |
||
| 28 | |||
| 29 | /** |
||
| 30 | * The revision represented by this entity |
||
| 31 | * |
||
| 32 | * @var Revision |
||
| 33 | */ |
||
| 34 | protected $revision; //language_id, updated_at |
||
| 35 | |||
| 36 | /** |
||
| 37 | * The fields in this entity |
||
| 38 | * |
||
| 39 | * @var array<FieldCollection> |
||
| 40 | */ |
||
| 41 | protected $data; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Entity constructor. |
||
| 45 | * |
||
| 46 | * @param int $language_id The language this specific entity is in |
||
| 47 | */ |
||
| 48 | 57 | public function __construct($language_id) |
|
| 49 | { |
||
| 50 | 57 | if (!is_int($language_id) || $language_id == 0) { |
|
| 51 | 3 | throw new InvalidArgumentException("You must set a valid 'language_id'."); |
|
| 52 | } |
||
| 53 | |||
| 54 | 54 | $fields = $this->getFields(); |
|
| 55 | |||
| 56 | 54 | $this->initialize($fields); |
|
| 57 | |||
| 58 | 48 | $this->type = $this->getContentType(); |
|
|
|
|||
| 59 | 48 | $this->language_id = $language_id; |
|
| 60 | 48 | } |
|
| 61 | |||
| 62 | 54 | protected function initialize(array $fields) |
|
| 63 | { |
||
| 64 | 54 | $this->content = new Content; |
|
| 65 | 54 | $this->revision = new Revision; |
|
| 66 | |||
| 67 | 54 | foreach ($fields as $field => $settings) { |
|
| 68 | 54 | $this->data[$field] = $this->initializeField($field, $settings); |
|
| 69 | 48 | } |
|
| 70 | 48 | } |
|
| 71 | |||
| 72 | /** |
||
| 73 | * @param string $field |
||
| 74 | * @param array $settings |
||
| 75 | * @throws InvalidFieldTypeException |
||
| 76 | * @throws ReservedFieldNameException |
||
| 77 | * @return FieldCollection |
||
| 78 | */ |
||
| 79 | 54 | protected function initializeField($field, $settings) |
|
| 80 | { |
||
| 81 | 54 | if ($this->isContentField($field) || $this->isRevisionField($field)) { |
|
| 82 | 3 | throw new ReservedFieldNameException( |
|
| 83 | 3 | "The field '$field' cannot be used in '" . get_class($this) . "' as it is a reserved name" |
|
| 84 | 3 | ); |
|
| 85 | } |
||
| 86 | |||
| 87 | 51 | $type = $settings['type']; |
|
| 88 | |||
| 89 | 51 | if (!array_key_exists($type, self::$types)) { |
|
| 90 | 3 | throw new InvalidFieldTypeException("Unkown type '$type' in '" . get_class($this) . "'"); |
|
| 91 | } |
||
| 92 | |||
| 93 | 48 | $settings['type'] = self::$types[$settings['type']]; |
|
| 94 | |||
| 95 | 48 | return FieldCollection::initField($settings); |
|
| 96 | } |
||
| 97 | |||
| 98 | abstract public function getFields(); |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Get the database friendly content type |
||
| 102 | * |
||
| 103 | * @return string |
||
| 104 | */ |
||
| 105 | 54 | public static function getContentType() |
|
| 109 | |||
| 110 | /** |
||
| 111 | * Create a new revision based on the same content ID but without the content. |
||
| 112 | * Very useful if you want to add a new language |
||
| 113 | * |
||
| 114 | * @param int $language_id |
||
| 115 | * @return static |
||
| 116 | */ |
||
| 117 | 3 | public function newRevision($language_id = null) |
|
| 124 | |||
| 125 | /** |
||
| 126 | * Check if the field is related to the content |
||
| 127 | * |
||
| 128 | * @param string $field |
||
| 129 | * @return bool |
||
| 130 | */ |
||
| 131 | 54 | protected function isContentField($field) |
|
| 135 | |||
| 136 | /** |
||
| 137 | * Check if the field exists on the entity |
||
| 138 | * |
||
| 139 | * @param string $field |
||
| 140 | * @return bool |
||
| 141 | */ |
||
| 142 | 42 | public function hasField($field) |
|
| 146 | |||
| 147 | /** |
||
| 148 | * @param string $field |
||
| 149 | * @return FieldCollection |
||
| 150 | */ |
||
| 151 | 36 | public function getField($field) |
|
| 155 | |||
| 156 | /** |
||
| 157 | * Check if the field is related to the revision |
||
| 158 | * |
||
| 159 | * @param string $field |
||
| 160 | * @return bool |
||
| 161 | */ |
||
| 162 | 51 | protected function isRevisionField($field) |
|
| 166 | |||
| 167 | /** |
||
| 168 | * Dynamically retrieve attributes on the model. |
||
| 169 | * |
||
| 170 | * @param string $key |
||
| 171 | * @throws NonExistentFieldException |
||
| 172 | * @return $this|bool|\Carbon\Carbon|\DateTime|mixed|static |
||
| 173 | */ |
||
| 174 | 42 | public function __get($key) |
|
| 194 | |||
| 195 | /** |
||
| 196 | * Dynamically set attributes on the model. |
||
| 197 | * |
||
| 198 | * @param string $key |
||
| 199 | * @param mixed $value |
||
| 200 | * @throws NonExistentFieldException |
||
| 201 | */ |
||
| 202 | 48 | public function __set($key, $value) |
|
| 239 | |||
| 240 | /** |
||
| 241 | * Find the latest valid revision for this entity |
||
| 242 | * |
||
| 243 | * @param int $id |
||
| 244 | * @param int $language_id |
||
| 245 | * @return static |
||
| 246 | */ |
||
| 247 | 12 | public static function find($id, $language_id) |
|
| 277 | |||
| 278 | /** |
||
| 279 | * Save a revision |
||
| 280 | */ |
||
| 281 | 15 | public function save($newRevision = false, $publishRevision = true) |
|
| 319 | |||
| 320 | 15 | protected function saveContent() |
|
| 324 | |||
| 325 | 15 | protected function saveRevision($publishRevision) |
|
| 343 | |||
| 344 | 15 | protected function saveField(Field $field, $newRevision) |
|
| 358 | |||
| 359 | /** |
||
| 360 | * Convert the Entity to an array. |
||
| 361 | * |
||
| 362 | * @return array |
||
| 363 | */ |
||
| 364 | 16 | public function toArray() |
|
| 377 | } |
||
| 378 |
Since your code implements the magic setter
_set, this function will be called for any write access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.Since the property has write access only, you can use the @property-write 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.