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 | * Begin set of behaviour and description constants. |
||
52 | * ================================================ |
||
53 | */ |
||
54 | |||
55 | /** |
||
56 | * Set of schema sections needed to describe entity behaviour. |
||
57 | */ |
||
58 | const SH_INSTANTIATION = 0; |
||
59 | const SH_DEFAULTS = 1; |
||
60 | const SH_COMPOSITIONS = 6; |
||
61 | const SH_AGGREGATIONS = 7; |
||
62 | |||
63 | /** |
||
64 | * Constants used to describe aggregation relations (also used internally to identify |
||
65 | * composition). |
||
66 | * |
||
67 | * Example: |
||
68 | * 'items' => [self::MANY => Item::class, ['parentID' => 'key::_id']] |
||
69 | * |
||
70 | * @see DocumentEntity::SCHEMA |
||
71 | */ |
||
72 | const MANY = 778; |
||
73 | const ONE = 899; |
||
74 | |||
75 | /* |
||
76 | * ================================================ |
||
77 | * End set of behaviour and description constants. |
||
78 | */ |
||
79 | |||
80 | /** |
||
81 | * Class responsible for instance construction. |
||
82 | */ |
||
83 | const INSTANTIATOR = DocumentInstantiator::class; |
||
84 | |||
85 | /** |
||
86 | * Document fields, accessors and relations. ODM will generate setters and getters for some |
||
87 | * fields based on their types. |
||
88 | * |
||
89 | * Example, fields: |
||
90 | * const SCHEMA = [ |
||
91 | * '_id' => 'MongoId', //Primary key field |
||
92 | * 'value' => 'string', //Default string field |
||
93 | * 'values' => ['string'] //ScalarArray accessor will be applied for fields like that |
||
94 | * ]; |
||
95 | * |
||
96 | * Compositions: |
||
97 | * const SCHEMA = [ |
||
98 | * ..., |
||
99 | * 'child' => Child::class, //One document are composited, for example user Profile |
||
100 | * 'many' => [Child::class] //Compositor accessor will be applied, allows to |
||
101 | * //composite many document instances |
||
102 | * ]; |
||
103 | * |
||
104 | * Documents can extend each other, in this case schema will also be inherited. |
||
105 | * |
||
106 | * Attention, make sure you properly set FILLABLE option in parent class to use constructions |
||
107 | * like: |
||
108 | * $parent->child = [...]; |
||
109 | * |
||
110 | * or |
||
111 | * $parent->setFields(['child'=>[...]]); |
||
112 | * |
||
113 | * @var array |
||
114 | */ |
||
115 | const SCHEMA = []; |
||
116 | |||
117 | /** |
||
118 | * Default field values. |
||
119 | * |
||
120 | * @var array |
||
121 | */ |
||
122 | const DEFAULTS = []; |
||
123 | |||
124 | /** |
||
125 | * Model behaviour configurations. |
||
126 | */ |
||
127 | const SECURED = '*'; |
||
128 | const HIDDEN = []; |
||
129 | const FILLABLE = []; |
||
130 | const SETTERS = []; |
||
131 | const GETTERS = []; |
||
132 | const ACCESSORS = []; |
||
133 | |||
134 | /** |
||
135 | * Document behaviour schema. |
||
136 | * |
||
137 | * @var array |
||
138 | */ |
||
139 | private $documentSchema = []; |
||
140 | |||
141 | /** |
||
142 | * Document field updates (changed values). |
||
143 | * |
||
144 | * @var array |
||
145 | */ |
||
146 | private $changes = []; |
||
147 | |||
148 | /** |
||
149 | * Parent ODM instance, responsible for aggregations and lazy loading operations. |
||
150 | * |
||
151 | * @invisible |
||
152 | * @var ODMInterface |
||
153 | */ |
||
154 | protected $odm; |
||
155 | |||
156 | /** |
||
157 | * {@inheritdoc} |
||
158 | * |
||
159 | * @param ODMInterface $odm To lazy create nested document ang aggregations. |
||
160 | * |
||
161 | * @throws ScopeException When no ODM instance can be resolved. |
||
162 | */ |
||
163 | public function __construct(array $data = [], ODMInterface $odm = null, array $schema = null) |
||
182 | |||
183 | /** |
||
184 | * {@inheritdoc} |
||
185 | */ |
||
186 | public function getField(string $name, $default = null, bool $filter = true) |
||
192 | |||
193 | /** |
||
194 | * {@inheritdoc} |
||
195 | * |
||
196 | * Tracks field changes. |
||
197 | */ |
||
198 | public function setField(string $name, $value, bool $filter = true) |
||
205 | |||
206 | /** |
||
207 | * {@inheritdoc} |
||
208 | * |
||
209 | * Will restore default value if presented. |
||
210 | */ |
||
211 | public function __unset($offset) |
||
219 | |||
220 | /** |
||
221 | * Provides ability to invoke document aggregation. |
||
222 | * |
||
223 | * @param string $method |
||
224 | * @param array $arguments |
||
225 | * |
||
226 | * @return mixed|null|AccessorInterface|CompositableInterface|Document|Entities\DocumentSelector |
||
227 | */ |
||
228 | public function __call($method, array $arguments) |
||
242 | |||
243 | /** |
||
244 | * {@inheritdoc} |
||
245 | * |
||
246 | * @param string $field Check once specific field changes. |
||
247 | */ |
||
248 | public function hasUpdates(string $field = null): bool |
||
279 | |||
280 | /** |
||
281 | * {@inheritdoc} |
||
282 | */ |
||
283 | public function buildAtomics(string $container = null): array |
||
327 | |||
328 | /** |
||
329 | * {@inheritdoc} |
||
330 | */ |
||
331 | public function flushUpdates() |
||
341 | |||
342 | /** |
||
343 | * {@inheritdoc} |
||
344 | * |
||
345 | * @param bool $includeID Set to false to exclude _id from packed fields. |
||
346 | */ |
||
347 | public function packValue(bool $includeID = true) |
||
357 | |||
358 | /** |
||
359 | * Since most of ODM documents might contain ObjectIDs and other fields we will try to normalize |
||
360 | * them into string values. |
||
361 | * |
||
362 | * @return array |
||
363 | */ |
||
364 | public function publicValue(): array |
||
376 | |||
377 | /** |
||
378 | * Cloning will be called when object will be embedded into another document. |
||
379 | */ |
||
380 | public function __clone() |
||
389 | |||
390 | /** |
||
391 | * @return array |
||
392 | */ |
||
393 | public function __debugInfo() |
||
400 | |||
401 | /** |
||
402 | * {@inheritdoc} |
||
403 | * |
||
404 | * @see CompositionDefinition |
||
405 | */ |
||
406 | protected function getMutator(string $field, string $mutator) |
||
417 | |||
418 | /** |
||
419 | * {@inheritdoc} |
||
420 | */ |
||
421 | protected function isNullable(string $field): bool |
||
431 | |||
432 | /** |
||
433 | * {@inheritdoc} |
||
434 | * |
||
435 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
436 | * |
||
437 | * @see CompositionDefinition |
||
438 | */ |
||
439 | protected function createAccessor( |
||
461 | |||
462 | /** |
||
463 | * {@inheritdoc} |
||
464 | */ |
||
465 | protected function iocContainer() |
||
474 | |||
475 | /** |
||
476 | * @param string $name |
||
477 | */ |
||
478 | private function registerChange(string $name) |
||
489 | |||
490 | /** |
||
491 | * @param string $name |
||
492 | * |
||
493 | * @throws FieldException |
||
494 | */ |
||
495 | private function assertField(string $name) |
||
505 | } |