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 $fields = [], ODMInterface $odm = null, array $schema = null) |
||
| 182 | |||
| 183 | /** |
||
| 184 | * {@inheritdoc} |
||
| 185 | */ |
||
| 186 | public function getField(string $name, $default = null, bool $filter = true) |
||
| 198 | |||
| 199 | /** |
||
| 200 | * {@inheritdoc} |
||
| 201 | * |
||
| 202 | * Tracks field changes. |
||
| 203 | */ |
||
| 204 | public function setField(string $name, $value, bool $filter = true) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * {@inheritdoc} |
||
| 222 | * |
||
| 223 | * Will restore default value if presented. |
||
| 224 | */ |
||
| 225 | public function __unset($offset) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * Provides ability to invoke document aggregation. |
||
| 236 | * |
||
| 237 | * @param string $method |
||
| 238 | * @param array $arguments |
||
| 239 | * |
||
| 240 | * @return mixed|null|AccessorInterface|CompositableInterface|Document|Entities\DocumentSelector |
||
| 241 | */ |
||
| 242 | public function __call($method, array $arguments) |
||
| 256 | |||
| 257 | /** |
||
| 258 | * {@inheritdoc} |
||
| 259 | * |
||
| 260 | * @param string $field Check once specific field changes. |
||
| 261 | */ |
||
| 262 | public function hasUpdates(string $field = null): bool |
||
| 293 | |||
| 294 | /** |
||
| 295 | * {@inheritdoc} |
||
| 296 | */ |
||
| 297 | public function buildAtomics(string $container = null): array |
||
| 341 | |||
| 342 | /** |
||
| 343 | * {@inheritdoc} |
||
| 344 | */ |
||
| 345 | public function flushUpdates() |
||
| 346 | { |
||
| 347 | $this->changes = []; |
||
| 348 | |||
| 349 | foreach ($this->getFields(false) as $field => $value) { |
||
| 350 | if ($value instanceof CompositableInterface) { |
||
| 351 | $value->flushUpdates(); |
||
| 352 | } |
||
| 353 | } |
||
| 354 | } |
||
| 355 | |||
| 356 | /** |
||
| 357 | * {@inheritdoc} |
||
| 358 | * |
||
| 359 | * @param bool $includeID Set to false to exclude _id from packed fields. |
||
| 360 | */ |
||
| 361 | public function packValue(bool $includeID = true) |
||
| 371 | |||
| 372 | /** |
||
| 373 | * Since most of ODM documents might contain ObjectIDs and other fields we will try to normalize |
||
| 374 | * them into string values. |
||
| 375 | * |
||
| 376 | * @return array |
||
| 377 | */ |
||
| 378 | public function publicValue(): array |
||
| 379 | { |
||
| 380 | $public = parent::publicValue(); |
||
| 381 | |||
| 382 | array_walk_recursive($public, function (&$value) { |
||
| 383 | if ($value instanceof ObjectID) { |
||
| 384 | $value = (string)$value; |
||
| 385 | } |
||
| 386 | }); |
||
| 387 | |||
| 388 | return $public; |
||
| 389 | } |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Cloning will be called when object will be embedded into another document. |
||
| 393 | */ |
||
| 394 | public function __clone() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * @return array |
||
| 406 | */ |
||
| 407 | public function __debugInfo() |
||
| 414 | |||
| 415 | /** |
||
| 416 | * {@inheritdoc} |
||
| 417 | * |
||
| 418 | * @see CompositionDefinition |
||
| 419 | */ |
||
| 420 | protected function getMutator(string $field, string $mutator) |
||
| 431 | |||
| 432 | /** |
||
| 433 | * {@inheritdoc} |
||
| 434 | */ |
||
| 435 | protected function isNullable(string $field): bool |
||
| 445 | |||
| 446 | /** |
||
| 447 | * {@inheritdoc} |
||
| 448 | * |
||
| 449 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
| 450 | * |
||
| 451 | * @see CompositionDefinition |
||
| 452 | */ |
||
| 453 | protected function createAccessor( |
||
| 475 | |||
| 476 | /** |
||
| 477 | * {@inheritdoc} |
||
| 478 | */ |
||
| 479 | protected function iocContainer() |
||
| 488 | |||
| 489 | /** |
||
| 490 | * @param string $name |
||
| 491 | */ |
||
| 492 | private function registerChange(string $name) |
||
| 503 | } |