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:
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) |
||
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) |
||
228 | |||
229 | /** |
||
230 | * get entity id from string or array |
||
231 | * @param mixed $stringOrEntity |
||
232 | * @param string $idSerializedKey |
||
233 | */ |
||
234 | private static function getEntityId($stringOrEntity, $idSerializedKey) |
||
242 | } |
||
243 |
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.