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; |
||
28 | abstract class Entity |
||
29 | { |
||
30 | /** |
||
31 | * @var array<class> The list of field types, filled with the configuration |
||
32 | */ |
||
33 | public static $types; |
||
34 | |||
35 | /** |
||
36 | * The content represented by this entity |
||
37 | * |
||
38 | * @var Content |
||
39 | */ |
||
40 | protected $content; //id, created_at, type, published |
||
41 | |||
42 | /** |
||
43 | * The revision represented by this entity |
||
44 | * |
||
45 | * @var Revision |
||
46 | */ |
||
47 | protected $revision; //language_id, updated_at, type, published |
||
48 | |||
49 | /** |
||
50 | * The fields in this entity |
||
51 | * |
||
52 | * @var array<FieldCollection> |
||
53 | */ |
||
54 | protected $data; |
||
55 | |||
56 | /** |
||
57 | * Entity constructor. |
||
58 | * |
||
59 | * @param int $language_id The language this specific entity is in |
||
60 | */ |
||
61 | 90 | public function __construct($language_id) |
|
74 | |||
75 | /** |
||
76 | * Creates the Content, Revision and FieldCollections |
||
77 | * |
||
78 | * @param array $fields The fields and their configurations |
||
79 | * @throws InvalidFieldTypeException |
||
80 | * @throws ReservedFieldNameException |
||
81 | */ |
||
82 | 87 | protected function initialize(array $fields) |
|
91 | |||
92 | /** |
||
93 | * Validate configuration and prepare a FieldCollection |
||
94 | * |
||
95 | * @param string $field |
||
96 | * @param array $settings |
||
97 | * @throws InvalidFieldTypeException |
||
98 | * @throws ReservedFieldNameException |
||
99 | * @return FieldCollection |
||
100 | */ |
||
101 | 87 | protected function initializeField($field, $settings) |
|
119 | |||
120 | /** |
||
121 | * Return the fields in this entity |
||
122 | * |
||
123 | * @return array |
||
124 | */ |
||
125 | abstract public function getFields(); |
||
126 | |||
127 | /** |
||
128 | * Get the database friendly content type |
||
129 | * |
||
130 | * @return string |
||
131 | */ |
||
132 | 87 | public static function getContentType() |
|
136 | |||
137 | /** |
||
138 | * Create a new revision based on the same content ID but without the content. |
||
139 | * Very useful if you want to add a new language |
||
140 | * |
||
141 | * @param int $language_id |
||
142 | * @return static |
||
143 | */ |
||
144 | 3 | public function newRevision($language_id = null) |
|
151 | |||
152 | /** |
||
153 | * Check if the field is related to the content |
||
154 | * |
||
155 | * @param string $field |
||
156 | * @return bool |
||
157 | */ |
||
158 | 87 | protected function isContentField($field) |
|
162 | |||
163 | /** |
||
164 | * Check if the field exists on the entity |
||
165 | * |
||
166 | * @param string $field |
||
167 | * @return bool |
||
168 | */ |
||
169 | 69 | public function hasField($field) |
|
173 | |||
174 | /** |
||
175 | * Get a field's FieldCollection. |
||
176 | * |
||
177 | * Be careful as this gives you the real field instances. |
||
178 | * |
||
179 | * @param string $field |
||
180 | * @return FieldCollection |
||
181 | */ |
||
182 | 63 | public function getField($field) |
|
186 | |||
187 | /** |
||
188 | * Check if the field is related to the revision |
||
189 | * |
||
190 | * @param string $field |
||
191 | * @return bool |
||
192 | */ |
||
193 | 84 | protected function isRevisionField($field) |
|
197 | |||
198 | /** |
||
199 | * Dynamically retrieve attributes on the model. |
||
200 | * |
||
201 | * @param string $key |
||
202 | * @throws NonExistentFieldException |
||
203 | * @return $this|bool|\Carbon\Carbon|\DateTime|mixed|static |
||
204 | */ |
||
205 | 72 | public function __get($key) |
|
229 | |||
230 | /** |
||
231 | * Dynamically set attributes on the model. |
||
232 | * |
||
233 | * @param string $key |
||
234 | * @param mixed $value |
||
235 | * @throws NonExistentFieldException |
||
236 | */ |
||
237 | 81 | public function __set($key, $value) |
|
259 | |||
260 | /** |
||
261 | * Set values on a field |
||
262 | * |
||
263 | * @param FieldCollection $field |
||
264 | * @param $value |
||
265 | */ |
||
266 | 18 | protected function setOnField(FieldCollection $field, $value) |
|
284 | |||
285 | /** |
||
286 | * Get all field types in this Entity. |
||
287 | * |
||
288 | * @return Collection |
||
289 | */ |
||
290 | 23 | protected function getFieldTypes() |
|
302 | |||
303 | /** |
||
304 | * @param int $id The content ID |
||
305 | * @param int $language_id The language ID |
||
306 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
307 | * @throws NoPublishedRevisionForLanguageException |
||
308 | * @throws NoRevisionForLanguageException |
||
309 | * @throws RevisionEntityMismatchException |
||
310 | * @throws RevisionNotFoundException |
||
311 | * @return Revision |
||
312 | */ |
||
313 | 30 | protected static function findRevision($id, $language_id, $revision_id = null) { |
|
346 | |||
347 | /** |
||
348 | * Find the latest valid revision for this entity |
||
349 | * |
||
350 | * @param int $id The content ID |
||
351 | * @param int $language_id The language ID |
||
352 | * @param int $revision_id The revision ID which you want to load, this is optional |
||
353 | * @throws EntityNotFoundException |
||
354 | * @throws NoPublishedRevisionForLanguageException |
||
355 | * @throws NoRevisionForLanguageException |
||
356 | * @throws RevisionEntityMismatchException |
||
357 | * @throws RevisionNotFoundException |
||
358 | * @return static |
||
359 | */ |
||
360 | 37 | public static function find($id, $language_id, $revision_id = null) |
|
383 | |||
384 | /** |
||
385 | * Save a revision |
||
386 | * |
||
387 | * @param bool $newRevision Should we create a new revision, false by default |
||
388 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
389 | */ |
||
390 | 42 | public function save($newRevision = false, $publishRevision = true) |
|
427 | |||
428 | /** |
||
429 | * Save the content |
||
430 | */ |
||
431 | 42 | protected function saveContent() |
|
435 | |||
436 | /** |
||
437 | * Save the revision |
||
438 | * |
||
439 | * @param bool $publishRevision Should we immediately publish this revision, true by default |
||
440 | */ |
||
441 | 42 | protected function saveRevision($publishRevision) |
|
454 | |||
455 | /** |
||
456 | * Unpublish the revisions other than this one. |
||
457 | * Only for the same content_id and language_id |
||
458 | */ |
||
459 | 27 | protected function unpublishOtherRevisions() |
|
471 | |||
472 | /** |
||
473 | * Save a single field instance |
||
474 | * |
||
475 | * @param Field $field The field instance to save |
||
476 | * @param bool $newRevision Should we create a new revision? |
||
477 | */ |
||
478 | 42 | protected function saveField(Field $field, $newRevision) |
|
492 | |||
493 | /** |
||
494 | * Convert the Entity to an array. |
||
495 | * |
||
496 | * @return array |
||
497 | */ |
||
498 | 18 | public function toArray() |
|
512 | |||
513 | 4 | public function delete($clear = true) |
|
536 | |||
537 | public function deleteRevision($clear = true) |
||
560 | |||
561 | 4 | protected function clearFields() |
|
572 | } |
||
573 |
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
integer
values, zero is a special case, in particular the following results might be unexpected: