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 |
||
| 43 | abstract class DocumentEntity extends SchematicEntity implements CompositableInterface |
||
| 44 | { |
||
| 45 | use SaturateTrait; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * Helper constant to identify atomic SET operations. |
||
| 49 | */ |
||
| 50 | const ATOMIC_SET = '$set'; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * Tells ODM component that Document class must be resolved using document fields. ODM must |
||
| 54 | * match fields to every child of this documents and find best match. This is default definition |
||
| 55 | * behaviour. |
||
| 56 | * |
||
| 57 | * Example: |
||
| 58 | * > Class A: _id, name, address |
||
| 59 | * > Class B extends A: _id, name, address, email |
||
| 60 | * < Class B will be used to represent all documents with existed email field. |
||
| 61 | * |
||
| 62 | * @see DocumentSchema |
||
| 63 | */ |
||
| 64 | const DEFINITION_FIELDS = 1; |
||
| 65 | |||
| 66 | /** |
||
| 67 | * Tells ODM that logical method (defineClass) must be used to define document class. Method |
||
| 68 | * will receive document fields as input and must return document class name. |
||
| 69 | * |
||
| 70 | * Example: |
||
| 71 | * > Class A: _id, name, type (a) |
||
| 72 | * > Class B extends A: _id, name, type (b) |
||
| 73 | * > Class C extends B: _id, name, type (c) |
||
| 74 | * < Static method in class A (parent) should return A, B or C based on type field value (as |
||
| 75 | * example). |
||
| 76 | * |
||
| 77 | * Attention, ODM will always ask TOP PARENT (in collection) to define class when you loading |
||
| 78 | * documents from collections. |
||
| 79 | * |
||
| 80 | * @see defineClass($fields) |
||
| 81 | * @see DocumentSchema |
||
| 82 | */ |
||
| 83 | const DEFINITION_LOGICAL = 2; |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Indication to ODM component of method to resolve Document class using it's fieldset. This |
||
| 87 | * constant is required due Document can inherit another Document. |
||
| 88 | */ |
||
| 89 | const DEFINITION = self::DEFINITION_FIELDS; |
||
| 90 | |||
| 91 | /** |
||
| 92 | * Automatically convert "_id" to "id" in publicFields() method. |
||
| 93 | */ |
||
| 94 | const REMOVE_ID_UNDERSCORE = true; |
||
| 95 | |||
| 96 | /** |
||
| 97 | * Constants used to describe aggregation relations. |
||
| 98 | * |
||
| 99 | * Example: |
||
| 100 | * 'items' => [self::MANY => 'Models\Database\Item', [ |
||
| 101 | * 'parentID' => 'key::_id' |
||
| 102 | * ]] |
||
| 103 | * |
||
| 104 | * @see odmSchema::$schema |
||
| 105 | */ |
||
| 106 | const MANY = 778; |
||
| 107 | const ONE = 899; |
||
| 108 | |||
| 109 | /** |
||
| 110 | * Errors in nested documents and accessors. |
||
| 111 | * |
||
| 112 | * @var array |
||
| 113 | */ |
||
| 114 | private $innerErrors = []; |
||
|
|
|||
| 115 | |||
| 116 | /** |
||
| 117 | * SolidState will force document to be saved as one big data set without any atomic operations |
||
| 118 | * (dirty fields). |
||
| 119 | * |
||
| 120 | * @var bool |
||
| 121 | */ |
||
| 122 | private $solidState = false; |
||
| 123 | |||
| 124 | /** |
||
| 125 | * Document field updates (changed values). |
||
| 126 | * |
||
| 127 | * @var array |
||
| 128 | */ |
||
| 129 | private $updates = []; |
||
| 130 | |||
| 131 | /** |
||
| 132 | * User specified set of atomic operation to be applied to document on save() call. |
||
| 133 | * |
||
| 134 | * @var array |
||
| 135 | */ |
||
| 136 | private $atomics = []; |
||
| 137 | |||
| 138 | /** |
||
| 139 | * @var ODMInterface|ODM |
||
| 140 | */ |
||
| 141 | protected $odm = null; |
||
| 142 | |||
| 143 | /** |
||
| 144 | * Model schema provided by ODM component. |
||
| 145 | * |
||
| 146 | * @var array |
||
| 147 | */ |
||
| 148 | protected $odmSchema = []; |
||
| 149 | |||
| 150 | /** |
||
| 151 | * {@inheritdoc} |
||
| 152 | * |
||
| 153 | * @param array|null $schema |
||
| 154 | */ |
||
| 155 | public function __construct( |
||
| 174 | |||
| 175 | /** |
||
| 176 | * Change document solid state. SolidState will force document to be saved as one big data set |
||
| 177 | * without any atomic operations (dirty fields). |
||
| 178 | * |
||
| 179 | * @param bool $solidState |
||
| 180 | * @param bool $forceUpdate Mark all fields as changed to force update later. |
||
| 181 | * |
||
| 182 | * @return $this |
||
| 183 | */ |
||
| 184 | public function solidState($solidState, $forceUpdate = false) |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Is document is solid state? |
||
| 197 | * |
||
| 198 | * @see solidState() |
||
| 199 | * |
||
| 200 | * @return bool |
||
| 201 | */ |
||
| 202 | public function isSolid() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * Check if document has parent. |
||
| 209 | * |
||
| 210 | * @return bool |
||
| 211 | */ |
||
| 212 | public function isEmbedded() |
||
| 216 | |||
| 217 | /** |
||
| 218 | * {@inheritdoc} |
||
| 219 | * |
||
| 220 | * @todo change to clone |
||
| 221 | */ |
||
| 222 | public function __clone() |
||
| 241 | |||
| 242 | /** |
||
| 243 | * {@inheritdoc} |
||
| 244 | */ |
||
| 245 | public function setValue($data) |
||
| 249 | |||
| 250 | /** |
||
| 251 | * {@inheritdoc} |
||
| 252 | * |
||
| 253 | * Must track field updates. |
||
| 254 | */ |
||
| 255 | public function setField($name, $value, $filter = true) |
||
| 272 | |||
| 273 | /** |
||
| 274 | * {@inheritdoc} |
||
| 275 | * |
||
| 276 | * Will restore default value if presented. |
||
| 277 | */ |
||
| 278 | public function __unset($offset) |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Alias for atomic operation $set. Attention, this operation is not identical to setField() |
||
| 295 | * method, it performs low level operation and can be used only on simple fields. No filters |
||
| 296 | * will be applied to field! |
||
| 297 | * |
||
| 298 | * @param string $field |
||
| 299 | * @param mixed $value |
||
| 300 | * |
||
| 301 | * @return $this |
||
| 302 | * |
||
| 303 | * @throws DocumentException |
||
| 304 | */ |
||
| 305 | public function set($field, $value) |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Alias for atomic operation $inc. |
||
| 323 | * |
||
| 324 | * @param string $field |
||
| 325 | * @param string $value |
||
| 326 | * |
||
| 327 | * @return $this |
||
| 328 | * |
||
| 329 | * @throws DocumentException |
||
| 330 | */ |
||
| 331 | public function inc($field, $value) |
||
| 348 | |||
| 349 | /** |
||
| 350 | * {@inheritdoc} |
||
| 351 | */ |
||
| 352 | public function defaultValue() |
||
| 356 | |||
| 357 | /** |
||
| 358 | * {@inheritdoc} |
||
| 359 | * |
||
| 360 | * Include every composition public data into result. |
||
| 361 | */ |
||
| 362 | public function publicFields() |
||
| 403 | |||
| 404 | /** |
||
| 405 | * {@inheritdoc} |
||
| 406 | * |
||
| 407 | * @param string $field Specific field name to check for updates. |
||
| 408 | * @param bool $atomicsOnly Check if field has any atomic operation associated with. |
||
| 409 | */ |
||
| 410 | public function hasUpdates($field = null, $atomicsOnly = false) |
||
| 448 | |||
| 449 | /** |
||
| 450 | * {@inheritdoc} |
||
| 451 | */ |
||
| 452 | public function flushUpdates() |
||
| 462 | |||
| 463 | /** |
||
| 464 | * {@inheritdoc} |
||
| 465 | */ |
||
| 466 | public function buildAtomics($container = '') |
||
| 526 | |||
| 527 | /** |
||
| 528 | * @return array |
||
| 529 | */ |
||
| 530 | public function __debugInfo() |
||
| 537 | |||
| 538 | /** |
||
| 539 | * {@inheritdoc} |
||
| 540 | * |
||
| 541 | * Accessor options include field type resolved by DocumentSchema. |
||
| 542 | * |
||
| 543 | * @throws ODMException |
||
| 544 | * @throws DefinitionException |
||
| 545 | */ |
||
| 546 | protected function createAccessor($accessor, $value) |
||
| 561 | |||
| 562 | /** |
||
| 563 | * {@inheritdoc} |
||
| 564 | */ |
||
| 565 | View Code Duplication | protected function iocContainer() |
|
| 573 | |||
| 574 | /** |
||
| 575 | * Create document entity using given ODM instance or load parent ODM via shared container. |
||
| 576 | * |
||
| 577 | * @see Component::staticContainer() |
||
| 578 | * |
||
| 579 | * @param array $fields Model fields to set, will be passed thought filters. |
||
| 580 | * @param ODMInterface $odm ODMInterface component, global container will be called if not |
||
| 581 | * instance provided. |
||
| 582 | * |
||
| 583 | * @return DocumentEntity |
||
| 584 | * |
||
| 585 | * @event created($document) |
||
| 586 | */ |
||
| 587 | View Code Duplication | public static function create($fields = [], ODMInterface $odm = null) |
|
| 599 | |||
| 600 | /** |
||
| 601 | * Called by ODM with set of loaded fields. Must return name of appropriate class. |
||
| 602 | * |
||
| 603 | * @param array $fields |
||
| 604 | * @param ODMInterface $odm |
||
| 605 | * |
||
| 606 | * @return string |
||
| 607 | * |
||
| 608 | * @throws DefinitionException |
||
| 609 | */ |
||
| 610 | public static function defineClass(array $fields, ODMInterface $odm) |
||
| 614 | } |
This check marks private properties in classes that are never used. Those properties can be removed.