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) |
||
| 217 | |||
| 218 | /** |
||
| 219 | * {@inheritdoc} |
||
| 220 | * |
||
| 221 | * Will restore default value if presented. |
||
| 222 | */ |
||
| 223 | public function __unset($offset) |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Provides ability to invoke document aggregation. |
||
| 234 | * |
||
| 235 | * @param string $method |
||
| 236 | * @param array $arguments |
||
| 237 | * |
||
| 238 | * @return mixed|null|AccessorInterface|CompositableInterface|Document|Entities\DocumentSelector |
||
| 239 | */ |
||
| 240 | public function __call($method, array $arguments) |
||
| 254 | |||
| 255 | /** |
||
| 256 | * {@inheritdoc} |
||
| 257 | * |
||
| 258 | * @param string $field Check once specific field changes. |
||
| 259 | */ |
||
| 260 | public function hasUpdates(string $field = null): bool |
||
| 291 | |||
| 292 | /** |
||
| 293 | * {@inheritdoc} |
||
| 294 | */ |
||
| 295 | public function buildAtomics(string $container = null): array |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | public function commitUpdates() |
||
| 344 | { |
||
| 345 | $this->changes = []; |
||
| 346 | |||
| 347 | foreach ($this->getFields(false) as $field => $value) { |
||
| 348 | if ($value instanceof CompositableInterface) { |
||
| 349 | $value->commitUpdates(); |
||
| 350 | } |
||
| 351 | } |
||
| 352 | } |
||
| 353 | |||
| 354 | /** |
||
| 355 | * {@inheritdoc} |
||
| 356 | * |
||
| 357 | * @param bool $includeID Set to false to exclude _id from packed fields. |
||
| 358 | */ |
||
| 359 | public function packValue(bool $includeID = true) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Since most of ODM documents might contain ObjectIDs and other fields we will try to normalize |
||
| 372 | * them into string values. |
||
| 373 | * |
||
| 374 | * @return array |
||
| 375 | */ |
||
| 376 | public function publicFields(): array |
||
| 388 | |||
| 389 | /** |
||
| 390 | * Cloning will be called when object will be embedded into another document. |
||
| 391 | */ |
||
| 392 | public function __clone() |
||
| 401 | |||
| 402 | /** |
||
| 403 | * @return array |
||
| 404 | */ |
||
| 405 | public function __debugInfo() |
||
| 412 | |||
| 413 | /** |
||
| 414 | * {@inheritdoc} |
||
| 415 | * |
||
| 416 | * @see CompositionDefinition |
||
| 417 | */ |
||
| 418 | protected function getMutator(string $field, string $mutator) |
||
| 419 | { |
||
| 420 | /** |
||
| 421 | * Every document composition is valid accessor but defined a bit differently. |
||
| 422 | */ |
||
| 423 | if (isset($this->documentSchema[self::SH_COMPOSITIONS][$field])) { |
||
| 424 | return $this->documentSchema[self::SH_COMPOSITIONS][$field]; |
||
| 425 | } |
||
| 426 | |||
| 427 | return parent::getMutator($field, $mutator); |
||
| 428 | } |
||
| 429 | |||
| 430 | /** |
||
| 431 | * {@inheritdoc} |
||
| 432 | */ |
||
| 433 | protected function isNullable(string $field): bool |
||
| 443 | |||
| 444 | /** |
||
| 445 | * {@inheritdoc} |
||
| 446 | * |
||
| 447 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
| 448 | * |
||
| 449 | * @see CompositionDefinition |
||
| 450 | */ |
||
| 451 | protected function createAccessor( |
||
| 473 | |||
| 474 | /** |
||
| 475 | * {@inheritdoc} |
||
| 476 | */ |
||
| 477 | protected function iocContainer() |
||
| 486 | } |