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 $newArrayModel |
||
|
|||
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) |
||
188 | |||
189 | /** |
||
190 | * addIdentifiers |
||
191 | * |
||
192 | * add defined identifiers to given model |
||
193 | * |
||
194 | * @param array $newSerializedModel |
||
195 | * @param array $dirtyFields |
||
196 | * @access private |
||
197 | * @return array |
||
198 | */ |
||
199 | private function addIdentifiers($newSerializedModel, $dirtyFields, $idSerializedKey = null) |
||
211 | |||
212 | private function findOldRelation($relationValue, array $oldValue, ClassMetadata $classMetadata) |
||
233 | |||
234 | /** |
||
235 | * get entity id from string or array |
||
236 | * @param mixed $stringOrEntity |
||
237 | * @param string $idSerializedKey |
||
238 | */ |
||
239 | private static function getEntityId($stringOrEntity, $idSerializedKey) |
||
247 | } |
||
248 |
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italy
is not defined by the methodfinale(...)
.The most likely cause is that the parameter was removed, but the annotation was not.