Complex classes like DocumentEntity 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 DocumentEntity, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
46 | abstract class DocumentEntity extends SchematicEntity implements CompositableInterface |
||
47 | { |
||
48 | use SaturateTrait, SolidableTrait; |
||
49 | |||
50 | /** |
||
51 | * Set of schema sections needed to describe entity behaviour. |
||
52 | */ |
||
53 | const SH_INSTANTIATION = 0; |
||
54 | const SH_DEFAULTS = 1; |
||
55 | const SH_COMPOSITIONS = 6; |
||
56 | const SH_AGGREGATIONS = 7; |
||
57 | |||
58 | /** |
||
59 | * Constants used to describe aggregation relations (also used internally to identify |
||
60 | * composition). |
||
61 | * |
||
62 | * Example: |
||
63 | * 'items' => [self::MANY => Item::class, ['parentID' => 'key::_id']] |
||
64 | * |
||
65 | * @see DocumentEntity::SCHEMA |
||
66 | */ |
||
67 | const MANY = 778; |
||
68 | const ONE = 899; |
||
69 | |||
70 | /** |
||
71 | * Class responsible for instance construction. |
||
72 | */ |
||
73 | const INSTANTIATOR = DocumentInstantiator::class; |
||
74 | |||
75 | /** |
||
76 | * Document fields, accessors and relations. ODM will generate setters and getters for some |
||
77 | * fields based on their types. |
||
78 | * |
||
79 | * Example, fields: |
||
80 | * const SCHEMA = [ |
||
81 | * '_id' => 'MongoId', //Primary key field |
||
82 | * 'value' => 'string', //Default string field |
||
83 | * 'values' => ['string'] //ScalarArray accessor will be applied for fields like that |
||
84 | * ]; |
||
85 | * |
||
86 | * Compositions: |
||
87 | * const SCHEMA = [ |
||
88 | * ..., |
||
89 | * 'child' => Child::class, //One document are composited, for example user Profile |
||
90 | * 'many' => [Child::class] //Compositor accessor will be applied, allows to |
||
91 | * //composite many document instances |
||
92 | * ]; |
||
93 | * |
||
94 | * Documents can extend each other, in this case schema will also be inherited. |
||
95 | * |
||
96 | * Attention, make sure you properly set FILLABLE option in parent class to use constructions |
||
97 | * like: |
||
98 | * $parent->child = [...]; |
||
99 | * |
||
100 | * or |
||
101 | * $parent->setFields(['child'=>[...]]); |
||
102 | * |
||
103 | * @var array |
||
104 | */ |
||
105 | const SCHEMA = []; |
||
106 | |||
107 | /** |
||
108 | * Default field values. |
||
109 | * |
||
110 | * @var array |
||
111 | */ |
||
112 | const DEFAULTS = []; |
||
113 | |||
114 | /** |
||
115 | * Model behaviour configurations. |
||
116 | */ |
||
117 | const SECURED = '*'; |
||
118 | const HIDDEN = []; |
||
119 | const FILLABLE = []; |
||
120 | const SETTERS = []; |
||
121 | const GETTERS = []; |
||
122 | const ACCESSORS = []; |
||
123 | |||
124 | /** |
||
125 | * Document behaviour schema. |
||
126 | * |
||
127 | * @var array |
||
128 | */ |
||
129 | private $documentSchema = []; |
||
130 | |||
131 | /** |
||
132 | * Document field updates (changed values). |
||
133 | * |
||
134 | * @var array |
||
135 | */ |
||
136 | private $changes = []; |
||
137 | |||
138 | /** |
||
139 | * Parent ODM instance, responsible for aggregations and lazy loading operations. |
||
140 | * |
||
141 | * @invisible |
||
142 | * @var ODMInterface |
||
143 | */ |
||
144 | protected $odm; |
||
145 | |||
146 | /** |
||
147 | * {@inheritdoc} |
||
148 | * |
||
149 | * @param ODMInterface $odm To lazy create nested document ang aggregations. |
||
150 | * |
||
151 | * @throws ScopeException When no ODM instance can be resolved. |
||
152 | */ |
||
153 | public function __construct($fields = [], ODMInterface $odm = null, array $schema = null) |
||
172 | |||
173 | /** |
||
174 | * {@inheritdoc} |
||
175 | */ |
||
176 | public function getField(string $name, $default = null, bool $filter = true) |
||
188 | |||
189 | /** |
||
190 | * {@inheritdoc} |
||
191 | * |
||
192 | * Tracks field changes. |
||
193 | */ |
||
194 | public function setField(string $name, $value, bool $filter = true) |
||
209 | |||
210 | /** |
||
211 | * {@inheritdoc} |
||
212 | * |
||
213 | * Will restore default value if presented. |
||
214 | */ |
||
215 | public function __unset($offset) |
||
223 | |||
224 | /** |
||
225 | * Provides ability to invoke document aggregation. |
||
226 | * |
||
227 | * @param string $method |
||
228 | * @param array $arguments |
||
229 | * |
||
230 | * @return mixed|null|AccessorInterface|CompositableInterface|Document|Entities\DocumentSelector |
||
231 | */ |
||
232 | public function __call($method, array $arguments) |
||
246 | |||
247 | /** |
||
248 | * {@inheritdoc} |
||
249 | * |
||
250 | * @param string $field Check once specific field changes. |
||
251 | */ |
||
252 | public function hasUpdates(string $field = null): bool |
||
283 | |||
284 | /** |
||
285 | * {@inheritdoc} |
||
286 | */ |
||
287 | public function buildAtomics(string $container = null): array |
||
331 | |||
332 | /** |
||
333 | * {@inheritdoc} |
||
334 | */ |
||
335 | public function commitUpdates() |
||
345 | |||
346 | /** |
||
347 | * {@inheritdoc} |
||
348 | * |
||
349 | * @param bool $includeID Set to false to exclude _id from packed fields. |
||
350 | */ |
||
351 | public function packValue(bool $includeID = true) |
||
361 | |||
362 | /** |
||
363 | * Since most of ODM documents might contain ObjectIDs and other fields we will try to normalize |
||
364 | * them into string values. |
||
365 | * |
||
366 | * @return array |
||
367 | */ |
||
368 | public function publicFields(): array |
||
380 | |||
381 | /** |
||
382 | * Cloning will be called when object will be embedded into another document. |
||
383 | */ |
||
384 | public function __clone() |
||
393 | |||
394 | /** |
||
395 | * @return array |
||
396 | */ |
||
397 | public function __debugInfo() |
||
404 | |||
405 | /** |
||
406 | * {@inheritdoc} |
||
407 | * |
||
408 | * @see CompositionDefinition |
||
409 | */ |
||
410 | protected function getMutator(string $field, string $mutator) |
||
421 | |||
422 | /** |
||
423 | * {@inheritdoc} |
||
424 | */ |
||
425 | protected function isNullable(string $field): bool |
||
435 | |||
436 | /** |
||
437 | * {@inheritdoc} |
||
438 | * |
||
439 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
440 | * |
||
441 | * @see CompositionDefinition |
||
442 | */ |
||
443 | protected function createAccessor( |
||
465 | |||
466 | /** |
||
467 | * {@inheritdoc} |
||
468 | */ |
||
469 | protected function iocContainer() |
||
478 | |||
479 | /** |
||
480 | * @param string $name |
||
481 | */ |
||
482 | private function registerChange(string $name) |
||
493 | } |