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) |
||
| 192 | |||
| 193 | /** |
||
| 194 | * addIdentifiers |
||
| 195 | * |
||
| 196 | * add defined identifiers to given model |
||
| 197 | * |
||
| 198 | * @param array $newSerializedModel |
||
| 199 | * @param array $dirtyFields |
||
| 200 | * @access private |
||
| 201 | * @return array |
||
| 202 | */ |
||
| 203 | private function addIdentifiers($newSerializedModel, $dirtyFields, $idSerializedKey = null) |
||
| 215 | |||
| 216 | private function findOldRelation($relationValue, array $oldValue, ClassMetadata $classMetadata) |
||
| 232 | |||
| 233 | /** |
||
| 234 | * get entity id from string or array |
||
| 235 | * @param mixed $stringOrEntity |
||
| 236 | * @param string $idSerializedKey |
||
| 237 | */ |
||
| 238 | private static function getEntityId($stringOrEntity, $idSerializedKey) |
||
| 248 | } |
||
| 249 |