Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
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 |
||
| 45 | abstract class DocumentEntity extends SchematicEntity implements CompositableInterface |
||
| 46 | { |
||
| 47 | use SaturateTrait, SolidStateTrait; |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Set of schema sections needed to describe entity behaviour. |
||
| 51 | */ |
||
| 52 | const SH_INSTANTIATION = 0; |
||
| 53 | const SH_DEFAULTS = 1; |
||
| 54 | const SH_COMPOSITIONS = 6; |
||
| 55 | const SH_AGGREGATIONS = 7; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Constants used to describe aggregation relations (also used internally to identify |
||
| 59 | * composition). |
||
| 60 | * |
||
| 61 | * Example: |
||
| 62 | * 'items' => [self::MANY => Item::class, ['parentID' => 'key::_id']] |
||
| 63 | * |
||
| 64 | * @see DocumentEntity::SCHEMA |
||
| 65 | */ |
||
| 66 | const MANY = 778; |
||
| 67 | const ONE = 899; |
||
| 68 | |||
| 69 | /** |
||
| 70 | * Class responsible for instance construction. |
||
| 71 | */ |
||
| 72 | const INSTANTIATOR = DocumentInstantiator::class; |
||
| 73 | |||
| 74 | /** |
||
| 75 | * Document fields, accessors and relations. ODM will generate setters and getters for some |
||
| 76 | * fields based on their types. |
||
| 77 | * |
||
| 78 | * Example, fields: |
||
| 79 | * const SCHEMA = [ |
||
| 80 | * '_id' => 'MongoId', //Primary key field |
||
| 81 | * 'value' => 'string', //Default string field |
||
| 82 | * 'values' => ['string'] //ScalarArray accessor will be applied for fields like that |
||
| 83 | * ]; |
||
| 84 | * |
||
| 85 | * Compositions: |
||
| 86 | * const SCHEMA = [ |
||
| 87 | * ..., |
||
| 88 | * 'child' => Child::class, //One document are composited, for example user Profile |
||
| 89 | * 'many' => [Child::class] //Compositor accessor will be applied, allows to |
||
| 90 | * //composite many document instances |
||
| 91 | * ]; |
||
| 92 | * |
||
| 93 | * Documents can extend each other, in this case schema will also be inherited. |
||
| 94 | * |
||
| 95 | * Attention, make sure you properly set FILLABLE option in parent class to use constructions |
||
| 96 | * like: |
||
| 97 | * $parent->child = [...]; |
||
| 98 | * |
||
| 99 | * or |
||
| 100 | * $parent->setFields(['child'=>[...]]); |
||
| 101 | * |
||
| 102 | * @var array |
||
| 103 | */ |
||
| 104 | const SCHEMA = []; |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Default field values. |
||
| 108 | * |
||
| 109 | * @var array |
||
| 110 | */ |
||
| 111 | const DEFAULTS = []; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Model behaviour configurations. |
||
| 115 | */ |
||
| 116 | const SECURED = '*'; |
||
| 117 | const HIDDEN = []; |
||
| 118 | const FILLABLE = []; |
||
| 119 | const SETTERS = []; |
||
| 120 | const GETTERS = []; |
||
| 121 | const ACCESSORS = []; |
||
| 122 | |||
| 123 | /** |
||
| 124 | * Document behaviour schema. |
||
| 125 | * |
||
| 126 | * @var array |
||
| 127 | */ |
||
| 128 | private $schema = []; |
||
|
|
|||
| 129 | |||
| 130 | /** |
||
| 131 | * Document field updates (changed values). |
||
| 132 | * |
||
| 133 | * @var array |
||
| 134 | */ |
||
| 135 | private $updates = []; |
||
| 136 | |||
| 137 | /** |
||
| 138 | * Parent ODM instance, responsible for aggregations and lazy loading operations. |
||
| 139 | * |
||
| 140 | * @invisible |
||
| 141 | * @var ODMInterface |
||
| 142 | */ |
||
| 143 | protected $odm; |
||
| 144 | |||
| 145 | /** |
||
| 146 | * {@inheritdoc} |
||
| 147 | * |
||
| 148 | * @param ODMInterface $odm To lazy create nested document ang aggregations. |
||
| 149 | * |
||
| 150 | * @throws ScopeException When no ODM instance can be resolved. |
||
| 151 | */ |
||
| 152 | public function __construct($fields = [], array $schema = null, ODMInterface $odm = null) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * {@inheritdoc} |
||
| 174 | * |
||
| 175 | * Tracks field changes. |
||
| 176 | */ |
||
| 177 | public function setField(string $name, $value, bool $filter = true) |
||
| 196 | |||
| 197 | /** |
||
| 198 | * {@inheritdoc} |
||
| 199 | * |
||
| 200 | * Will restore default value if presented. |
||
| 201 | */ |
||
| 202 | public function __unset($offset) |
||
| 210 | |||
| 211 | /** |
||
| 212 | * Provides ability to invoke document aggregation. |
||
| 213 | * |
||
| 214 | * @param string $method |
||
| 215 | * @param array $arguments |
||
| 216 | * |
||
| 217 | * @return mixed|null|AccessorInterface|CompositableInterface|Document|Entities\DocumentSelector |
||
| 218 | */ |
||
| 219 | public function __call(string $method, array $arguments) |
||
| 233 | |||
| 234 | /** |
||
| 235 | * {@inheritdoc} |
||
| 236 | * |
||
| 237 | * @param string $field Check once specific field changes. |
||
| 238 | */ |
||
| 239 | public function hasUpdates(string $field = null): bool |
||
| 270 | |||
| 271 | /** |
||
| 272 | * {@inheritdoc} |
||
| 273 | */ |
||
| 274 | public function buildAtomics(string $container = null): array |
||
| 318 | |||
| 319 | /** |
||
| 320 | * {@inheritdoc} |
||
| 321 | */ |
||
| 322 | public function flushUpdates() |
||
| 332 | |||
| 333 | /** |
||
| 334 | * {@inheritdoc} |
||
| 335 | * |
||
| 336 | * @param bool $includeID Set to false to exclude _id from packed fields. |
||
| 337 | */ |
||
| 338 | public function packValue(bool $includeID = true) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Since most of ODM documents might contain ObjectIDs and other fields we will try to normalize |
||
| 351 | * them into string values. |
||
| 352 | * |
||
| 353 | * @return array |
||
| 354 | */ |
||
| 355 | public function publicFields(): array |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Cloning will be called when object will be embedded into another document. |
||
| 370 | */ |
||
| 371 | public function __clone() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * @return array |
||
| 382 | */ |
||
| 383 | public function __debugInfo() |
||
| 390 | |||
| 391 | /** |
||
| 392 | * {@inheritdoc} |
||
| 393 | * |
||
| 394 | * @see CompositionDefinition |
||
| 395 | */ |
||
| 396 | View Code Duplication | protected function getMutator(string $field, string $mutator) |
|
| 407 | |||
| 408 | /** |
||
| 409 | * {@inheritdoc} |
||
| 410 | */ |
||
| 411 | protected function isNullable(string $field): bool |
||
| 421 | |||
| 422 | /** |
||
| 423 | * {@inheritdoc} |
||
| 424 | * |
||
| 425 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
| 426 | * |
||
| 427 | * @see CompositionDefinition |
||
| 428 | */ |
||
| 429 | protected function createAccessor( |
||
| 451 | |||
| 452 | /** |
||
| 453 | * {@inheritdoc} |
||
| 454 | */ |
||
| 455 | protected function iocContainer() |
||
| 464 | } |