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 UnitOfWork 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 UnitOfWork, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 13 | class UnitOfWork |
||
| 14 | { |
||
| 15 | /** |
||
| 16 | * mapping |
||
| 17 | * |
||
| 18 | * @var Mapping |
||
| 19 | * @access private |
||
| 20 | */ |
||
| 21 | private $mapping; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * storage for every entity retrieved |
||
| 25 | * |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | private $storage; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Constructor. |
||
| 32 | */ |
||
| 33 | public function __construct(Mapping $mapping) |
||
| 38 | |||
| 39 | /** |
||
| 40 | * getDirtyData |
||
| 41 | * |
||
| 42 | * return the new serialized model with only needed fields to update |
||
| 43 | * |
||
| 44 | * @param array $newSerializedModel |
||
| 45 | * @param array $oldSerializedModel |
||
| 46 | * @param ClassMetadata $classMetadata |
||
| 47 | * @access public |
||
| 48 | * @return array |
||
| 49 | */ |
||
| 50 | public function getDirtyData(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata) |
||
| 54 | |||
| 55 | /** |
||
| 56 | * registerClean |
||
| 57 | * |
||
| 58 | * @param string $id |
||
| 59 | * @param object $entity |
||
| 60 | * @access public |
||
| 61 | * @return UnitOfWork |
||
| 62 | */ |
||
| 63 | public function registerClean($id, $entity) |
||
| 72 | |||
| 73 | /** |
||
| 74 | * getDirtyEntity |
||
| 75 | * |
||
| 76 | * @param string $id |
||
| 77 | * @access public |
||
| 78 | * @return mixed |
||
| 79 | */ |
||
| 80 | public function getDirtyEntity($id) |
||
| 87 | |||
| 88 | /** |
||
| 89 | * clear |
||
| 90 | * |
||
| 91 | * @param string $id |
||
| 92 | * @access public |
||
| 93 | * @return UnitOfWork |
||
| 94 | */ |
||
| 95 | public function clear($id) |
||
| 100 | |||
| 101 | /** |
||
| 102 | * getDirtyFields |
||
| 103 | * |
||
| 104 | * compares serialize object and returns only modified fields |
||
| 105 | * |
||
| 106 | * @param array $newSerializedModel |
||
| 107 | * @param array $oldSerializedModel |
||
| 108 | * @param ClassMetadata $classMetadata |
||
| 109 | * @access private |
||
| 110 | * @return array |
||
| 111 | */ |
||
| 112 | private function getDirtyFields(array $newSerializedModel, array $oldSerializedModel, ClassMetadata $classMetadata = null) |
||
| 190 | |||
| 191 | /** |
||
| 192 | * addIdentifiers |
||
| 193 | * |
||
| 194 | * add defined identifiers to given model |
||
| 195 | * |
||
| 196 | * @param array $newSerializedModel |
||
| 197 | * @param array $dirtyFields |
||
| 198 | * @access private |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | private function addIdentifiers($newSerializedModel, $dirtyFields, $idSerializedKey = null) |
||
| 213 | |||
| 214 | private function findOldRelation($relationValue, array $oldValue, ClassMetadata $classMetadata) |
||
| 230 | |||
| 231 | /** |
||
| 232 | * get entity id from string or array |
||
| 233 | * @param mixed $stringOrEntity |
||
| 234 | * @param string $idSerializedKey |
||
| 235 | */ |
||
| 236 | private static function getEntityId($stringOrEntity, $idSerializedKey) |
||
| 246 | } |
||
| 247 |
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.