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 |
||
| 39 | abstract class DocumentEntity extends SchematicEntity implements CompositableInterface |
||
| 40 | { |
||
| 41 | use SaturateTrait, SolidStateTrait; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * Set of schema sections needed to describe entity behaviour. |
||
| 45 | */ |
||
| 46 | const SH_INSTANTIATION = 0; |
||
| 47 | const SH_DEFAULTS = 1; |
||
| 48 | const SH_COMPOSITIONS = 6; |
||
| 49 | const SH_AGGREGATIONS = 7; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * Constants used to describe aggregation relations (also used internally to identify |
||
| 53 | * composition). |
||
| 54 | * |
||
| 55 | * Example: |
||
| 56 | * 'items' => [self::MANY => Item::class, ['parentID' => 'key::_id']] |
||
| 57 | * |
||
| 58 | * @see DocumentEntity::SCHEMA |
||
| 59 | */ |
||
| 60 | const MANY = 778; |
||
| 61 | const ONE = 899; |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Class responsible for instance construction. |
||
| 65 | */ |
||
| 66 | const INSTANTIATOR = DocumentInstantiator::class; |
||
| 67 | |||
| 68 | /** |
||
| 69 | * Document fields, accessors and relations. ODM will generate setters and getters for some |
||
| 70 | * fields based on their types. |
||
| 71 | * |
||
| 72 | * Example, fields: |
||
| 73 | * const SCHEMA = [ |
||
| 74 | * '_id' => 'MongoId', //Primary key field |
||
| 75 | * 'value' => 'string', //Default string field |
||
| 76 | * 'values' => ['string'] //ScalarArray accessor will be applied for fields like that |
||
| 77 | * ]; |
||
| 78 | * |
||
| 79 | * Compositions: |
||
| 80 | * const SCHEMA = [ |
||
| 81 | * ..., |
||
| 82 | * 'child' => Child::class, //One document are composited, for example user Profile |
||
| 83 | * 'many' => [Child::class] //Compositor accessor will be applied, allows to |
||
| 84 | * //composite many document instances |
||
| 85 | * ]; |
||
| 86 | * |
||
| 87 | * Documents can extend each other, in this case schema will also be inherited. |
||
| 88 | * |
||
| 89 | * Attention, make sure you properly set FILLABLE option in parent class to use constructions |
||
| 90 | * like: |
||
| 91 | * $parent->child = [...]; |
||
| 92 | * |
||
| 93 | * or |
||
| 94 | * $parent->setFields(['child'=>[...]]); |
||
| 95 | * |
||
| 96 | * @var array |
||
| 97 | */ |
||
| 98 | const SCHEMA = []; |
||
| 99 | |||
| 100 | /** |
||
| 101 | * Default field values. |
||
| 102 | * |
||
| 103 | * @var array |
||
| 104 | */ |
||
| 105 | const DEFAULTS = []; |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Model behaviour configurations. |
||
| 109 | */ |
||
| 110 | const SECURED = '*'; |
||
| 111 | const HIDDEN = []; |
||
| 112 | const FILLABLE = []; |
||
| 113 | const SETTERS = []; |
||
| 114 | const GETTERS = []; |
||
| 115 | const ACCESSORS = []; |
||
| 116 | |||
| 117 | /** |
||
| 118 | * Document behaviour schema. |
||
| 119 | * |
||
| 120 | * @var array |
||
| 121 | */ |
||
| 122 | private $schema = []; |
||
|
|
|||
| 123 | |||
| 124 | /** |
||
| 125 | * Document field updates (changed values). |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private $updates = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * Parent ODM instance, responsible for aggregations and lazy loading operations. |
||
| 133 | * |
||
| 134 | * @invisible |
||
| 135 | * @var ODMInterface |
||
| 136 | */ |
||
| 137 | protected $odm; |
||
| 138 | |||
| 139 | /** |
||
| 140 | * {@inheritdoc} |
||
| 141 | * |
||
| 142 | * @param ODMInterface $odm To lazy create nested document ang aggregations. |
||
| 143 | * |
||
| 144 | * @throws ScopeException When no ODM instance can be resolved. |
||
| 145 | */ |
||
| 146 | public function __construct($fields = [], array $schema = null, ODMInterface $odm = null) |
||
| 165 | |||
| 166 | /** |
||
| 167 | * {@inheritdoc} |
||
| 168 | * |
||
| 169 | * Tracks field changes. |
||
| 170 | */ |
||
| 171 | public function setField(string $name, $value, bool $filter = true) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * {@inheritdoc} |
||
| 193 | * |
||
| 194 | * @param string $field Check once specific field changes. |
||
| 195 | */ |
||
| 196 | public function hasUpdates(string $field = null): bool |
||
| 227 | |||
| 228 | /** |
||
| 229 | * {@inheritdoc} |
||
| 230 | */ |
||
| 231 | public function buildAtomics(string $container = null): array |
||
| 275 | |||
| 276 | /** |
||
| 277 | * {@inheritdoc} |
||
| 278 | */ |
||
| 279 | public function flushUpdates() |
||
| 289 | |||
| 290 | /** |
||
| 291 | * {@inheritdoc} |
||
| 292 | * |
||
| 293 | * @param bool $includeID Set to false to exclude _id from packed fields. |
||
| 294 | */ |
||
| 295 | public function packValue(bool $includeID = true) |
||
| 305 | |||
| 306 | /** |
||
| 307 | * Since most of ODM documents might contain ObjectIDs and other fields we will try to normalize |
||
| 308 | * them into string values. |
||
| 309 | * |
||
| 310 | * @return array |
||
| 311 | */ |
||
| 312 | public function publicFields(): array |
||
| 316 | |||
| 317 | /** |
||
| 318 | * Cloning will be called when object will be embedded into another document. |
||
| 319 | */ |
||
| 320 | public function __clone() |
||
| 328 | |||
| 329 | /** |
||
| 330 | * @return array |
||
| 331 | */ |
||
| 332 | public function __debugInfo() |
||
| 339 | |||
| 340 | /** |
||
| 341 | * {@inheritdoc} |
||
| 342 | */ |
||
| 343 | View Code Duplication | protected function getMutator(string $field, string $mutator) |
|
| 354 | |||
| 355 | /** |
||
| 356 | * {@inheritdoc} |
||
| 357 | */ |
||
| 358 | protected function isNullable(string $field): bool |
||
| 368 | |||
| 369 | /** |
||
| 370 | * {@inheritdoc} |
||
| 371 | * |
||
| 372 | * DocumentEntity will pass ODM instance as part of accessor context. |
||
| 373 | */ |
||
| 374 | protected function createAccessor( |
||
| 396 | |||
| 397 | /** |
||
| 398 | * {@inheritdoc} |
||
| 399 | */ |
||
| 400 | protected function iocContainer() |
||
| 409 | } |