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 TEntitySetMappingType 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 TEntitySetMappingType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class TEntitySetMappingType extends IsOK |
||
15 | { |
||
16 | use TSimpleIdentifierTrait; |
||
17 | /** |
||
18 | * @property string $name |
||
19 | */ |
||
20 | private $name = null; |
||
21 | |||
22 | /** |
||
23 | * @property string $typeName |
||
24 | */ |
||
25 | private $typeName = null; |
||
26 | |||
27 | /** |
||
28 | * @property string $storeEntitySet |
||
29 | */ |
||
30 | private $storeEntitySet = null; |
||
31 | |||
32 | /** |
||
33 | * @property boolean $makeColumnsDistinct |
||
34 | */ |
||
35 | private $makeColumnsDistinct = null; |
||
36 | |||
37 | /** |
||
38 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TQueryViewType[] $queryView |
||
39 | */ |
||
40 | private $queryView = []; |
||
41 | |||
42 | /** |
||
43 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TEntityTypeMappingType[] $entityTypeMapping |
||
44 | */ |
||
45 | private $entityTypeMapping = []; |
||
46 | |||
47 | /** |
||
48 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TMappingFragmentType[] $mappingFragment |
||
49 | */ |
||
50 | private $mappingFragment = []; |
||
51 | |||
52 | /** |
||
53 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TComplexPropertyType $complexProperty |
||
54 | */ |
||
55 | private $complexProperty = null; |
||
56 | |||
57 | /** |
||
58 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TScalarPropertyType $scalarProperty |
||
59 | */ |
||
60 | private $scalarProperty = null; |
||
61 | |||
62 | /** |
||
63 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TConditionType $condition |
||
64 | */ |
||
65 | private $condition = null; |
||
66 | |||
67 | /** |
||
68 | * Gets as name |
||
69 | * |
||
70 | * @return string |
||
71 | */ |
||
72 | public function getName() |
||
76 | |||
77 | /** |
||
78 | * Sets a new name |
||
79 | * |
||
80 | * @param string $name |
||
81 | * @return self |
||
82 | */ |
||
83 | public function setName($name) |
||
88 | |||
89 | /** |
||
90 | * Gets as typeName |
||
91 | * |
||
92 | * @return string |
||
93 | */ |
||
94 | public function getTypeName() |
||
98 | |||
99 | /** |
||
100 | * Sets a new typeName |
||
101 | * |
||
102 | * @param string $typeName |
||
103 | * @return self |
||
104 | */ |
||
105 | public function setTypeName($typeName) |
||
110 | |||
111 | /** |
||
112 | * Gets as storeEntitySet |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getStoreEntitySet() |
||
120 | |||
121 | /** |
||
122 | * Sets a new storeEntitySet |
||
123 | * |
||
124 | * @param string $storeEntitySet |
||
125 | * @return self |
||
126 | */ |
||
127 | public function setStoreEntitySet($storeEntitySet) |
||
132 | |||
133 | /** |
||
134 | * Gets as makeColumnsDistinct |
||
135 | * |
||
136 | * @return boolean |
||
137 | */ |
||
138 | public function getMakeColumnsDistinct() |
||
142 | |||
143 | /** |
||
144 | * Sets a new makeColumnsDistinct |
||
145 | * |
||
146 | * @param boolean $makeColumnsDistinct |
||
147 | * @return self |
||
148 | */ |
||
149 | public function setMakeColumnsDistinct($makeColumnsDistinct) |
||
154 | |||
155 | /** |
||
156 | * Adds as queryView |
||
157 | * |
||
158 | * @return self |
||
159 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TQueryViewType $queryView |
||
160 | */ |
||
161 | public function addToQueryView(TQueryViewType $queryView) |
||
166 | |||
167 | /** |
||
168 | * isset queryView |
||
169 | * |
||
170 | * @param scalar $index |
||
171 | * @return boolean |
||
172 | */ |
||
173 | public function issetQueryView($index) |
||
177 | |||
178 | /** |
||
179 | * unset queryView |
||
180 | * |
||
181 | * @param scalar $index |
||
182 | * @return void |
||
183 | */ |
||
184 | public function unsetQueryView($index) |
||
188 | |||
189 | /** |
||
190 | * Gets as queryView |
||
191 | * |
||
192 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TQueryViewType[] |
||
193 | */ |
||
194 | public function getQueryView() |
||
198 | |||
199 | /** |
||
200 | * Sets a new queryView |
||
201 | * |
||
202 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TQueryViewType[] $queryView |
||
203 | * @return self |
||
204 | */ |
||
205 | public function setQueryView(array $queryView) |
||
210 | |||
211 | /** |
||
212 | * Adds as entityTypeMapping |
||
213 | * |
||
214 | * @return self |
||
215 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TEntityTypeMappingType $entityTypeMapping |
||
216 | */ |
||
217 | public function addToEntityTypeMapping(TEntityTypeMappingType $entityTypeMapping) |
||
222 | |||
223 | /** |
||
224 | * isset entityTypeMapping |
||
225 | * |
||
226 | * @param scalar $index |
||
227 | * @return boolean |
||
228 | */ |
||
229 | public function issetEntityTypeMapping($index) |
||
233 | |||
234 | /** |
||
235 | * unset entityTypeMapping |
||
236 | * |
||
237 | * @param scalar $index |
||
238 | * @return void |
||
239 | */ |
||
240 | public function unsetEntityTypeMapping($index) |
||
244 | |||
245 | /** |
||
246 | * Gets as entityTypeMapping |
||
247 | * |
||
248 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TEntityTypeMappingType[] |
||
249 | */ |
||
250 | public function getEntityTypeMapping() |
||
254 | |||
255 | /** |
||
256 | * Sets a new entityTypeMapping |
||
257 | * |
||
258 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TEntityTypeMappingType[] $entityTypeMapping |
||
259 | * @return self |
||
260 | */ |
||
261 | public function setEntityTypeMapping(array $entityTypeMapping) |
||
266 | |||
267 | /** |
||
268 | * Adds as mappingFragment |
||
269 | * |
||
270 | * @return self |
||
271 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TMappingFragmentType $mappingFragment |
||
272 | */ |
||
273 | public function addToMappingFragment(TMappingFragmentType $mappingFragment) |
||
278 | |||
279 | /** |
||
280 | * isset mappingFragment |
||
281 | * |
||
282 | * @param scalar $index |
||
283 | * @return boolean |
||
284 | */ |
||
285 | public function issetMappingFragment($index) |
||
289 | |||
290 | /** |
||
291 | * unset mappingFragment |
||
292 | * |
||
293 | * @param scalar $index |
||
294 | * @return void |
||
295 | */ |
||
296 | public function unsetMappingFragment($index) |
||
300 | |||
301 | /** |
||
302 | * Gets as mappingFragment |
||
303 | * |
||
304 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TMappingFragmentType[] |
||
305 | */ |
||
306 | public function getMappingFragment() |
||
310 | |||
311 | /** |
||
312 | * Sets a new mappingFragment |
||
313 | * |
||
314 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TMappingFragmentType[] $mappingFragment |
||
315 | * @return self |
||
316 | */ |
||
317 | public function setMappingFragment(array $mappingFragment) |
||
322 | |||
323 | /** |
||
324 | * Gets as complexProperty |
||
325 | * |
||
326 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TComplexPropertyType |
||
327 | */ |
||
328 | public function getComplexProperty() |
||
332 | |||
333 | /** |
||
334 | * Sets a new complexProperty |
||
335 | * |
||
336 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TComplexPropertyType $complexProperty |
||
337 | * @return self |
||
338 | */ |
||
339 | public function setComplexProperty(TComplexPropertyType $complexProperty) |
||
344 | |||
345 | /** |
||
346 | * Gets as scalarProperty |
||
347 | * |
||
348 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TScalarPropertyType |
||
349 | */ |
||
350 | public function getScalarProperty() |
||
354 | |||
355 | /** |
||
356 | * Sets a new scalarProperty |
||
357 | * |
||
358 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TScalarPropertyType $scalarProperty |
||
359 | * @return self |
||
360 | */ |
||
361 | public function setScalarProperty(TScalarPropertyType $scalarProperty) |
||
366 | |||
367 | /** |
||
368 | * Gets as condition |
||
369 | * |
||
370 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TConditionType |
||
371 | */ |
||
372 | public function getCondition() |
||
376 | |||
377 | /** |
||
378 | * Sets a new condition |
||
379 | * |
||
380 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TConditionType $condition |
||
381 | * @return self |
||
382 | */ |
||
383 | public function setCondition(TConditionType $condition) |
||
388 | |||
389 | public function isOK(&$msg = null) |
||
445 | } |
||
446 |
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.