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 TComplexTypeMappingType 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 TComplexTypeMappingType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
14 | class TComplexTypeMappingType extends IsOK |
||
15 | { |
||
16 | |||
17 | /** |
||
18 | * @property string $typeName |
||
19 | */ |
||
20 | private $typeName = null; |
||
21 | |||
22 | /** |
||
23 | * @property boolean $isPartial |
||
24 | */ |
||
25 | private $isPartial = null; |
||
26 | |||
27 | /** |
||
28 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TScalarPropertyType[] $scalarProperty |
||
29 | */ |
||
30 | private $scalarProperty = []; |
||
31 | |||
32 | /** |
||
33 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TComplexPropertyType[] $complexProperty |
||
34 | */ |
||
35 | private $complexProperty = []; |
||
36 | |||
37 | /** |
||
38 | * @property \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TConditionType[] $condition |
||
39 | */ |
||
40 | private $condition = []; |
||
41 | |||
42 | /** |
||
43 | * Gets as typeName |
||
44 | * |
||
45 | * @return string |
||
46 | */ |
||
47 | public function getTypeName() |
||
51 | |||
52 | /** |
||
53 | * Sets a new typeName |
||
54 | * |
||
55 | * @param string $typeName |
||
56 | * @return self |
||
57 | */ |
||
58 | View Code Duplication | public function setTypeName($typeName) |
|
69 | |||
70 | /** |
||
71 | * Gets as isPartial |
||
72 | * |
||
73 | * @return boolean |
||
74 | */ |
||
75 | public function getIsPartial() |
||
79 | |||
80 | /** |
||
81 | * Sets a new isPartial |
||
82 | * |
||
83 | * @param boolean $isPartial |
||
84 | * @return self |
||
85 | */ |
||
86 | public function setIsPartial($isPartial) |
||
91 | |||
92 | /** |
||
93 | * Adds as scalarProperty |
||
94 | * |
||
95 | * @return self |
||
96 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TScalarPropertyType $scalarProperty |
||
97 | */ |
||
98 | View Code Duplication | public function addToScalarProperty(TScalarPropertyType $scalarProperty) |
|
107 | |||
108 | /** |
||
109 | * isset scalarProperty |
||
110 | * |
||
111 | * @param scalar $index |
||
112 | * @return boolean |
||
113 | */ |
||
114 | public function issetScalarProperty($index) |
||
118 | |||
119 | /** |
||
120 | * unset scalarProperty |
||
121 | * |
||
122 | * @param scalar $index |
||
123 | * @return void |
||
124 | */ |
||
125 | public function unsetScalarProperty($index) |
||
129 | |||
130 | /** |
||
131 | * Gets as scalarProperty |
||
132 | * |
||
133 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TScalarPropertyType[] |
||
134 | */ |
||
135 | public function getScalarProperty() |
||
139 | |||
140 | /** |
||
141 | * Sets a new scalarProperty |
||
142 | * |
||
143 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TScalarPropertyType[] $scalarProperty |
||
144 | * @return self |
||
145 | */ |
||
146 | View Code Duplication | public function setScalarProperty(array $scalarProperty) |
|
163 | |||
164 | /** |
||
165 | * Adds as complexProperty |
||
166 | * |
||
167 | * @return self |
||
168 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TComplexPropertyType $complexProperty |
||
169 | */ |
||
170 | View Code Duplication | public function addToComplexProperty(TComplexPropertyType $complexProperty) |
|
179 | |||
180 | /** |
||
181 | * isset complexProperty |
||
182 | * |
||
183 | * @param scalar $index |
||
184 | * @return boolean |
||
185 | */ |
||
186 | public function issetComplexProperty($index) |
||
190 | |||
191 | /** |
||
192 | * unset complexProperty |
||
193 | * |
||
194 | * @param scalar $index |
||
195 | * @return void |
||
196 | */ |
||
197 | public function unsetComplexProperty($index) |
||
201 | |||
202 | /** |
||
203 | * Gets as complexProperty |
||
204 | * |
||
205 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TComplexPropertyType[] |
||
206 | */ |
||
207 | public function getComplexProperty() |
||
211 | |||
212 | /** |
||
213 | * Sets a new complexProperty |
||
214 | * |
||
215 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TComplexPropertyType[] $complexProperty |
||
216 | * @return self |
||
217 | */ |
||
218 | View Code Duplication | public function setComplexProperty(array $complexProperty) |
|
235 | |||
236 | /** |
||
237 | * Adds as condition |
||
238 | * |
||
239 | * @return self |
||
240 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TConditionType $condition |
||
241 | */ |
||
242 | View Code Duplication | public function addToCondition(TConditionType $condition) |
|
251 | |||
252 | /** |
||
253 | * isset condition |
||
254 | * |
||
255 | * @param scalar $index |
||
256 | * @return boolean |
||
257 | */ |
||
258 | public function issetCondition($index) |
||
262 | |||
263 | /** |
||
264 | * unset condition |
||
265 | * |
||
266 | * @param scalar $index |
||
267 | * @return void |
||
268 | */ |
||
269 | public function unsetCondition($index) |
||
273 | |||
274 | /** |
||
275 | * Gets as condition |
||
276 | * |
||
277 | * @return \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TConditionType[] |
||
278 | */ |
||
279 | public function getCondition() |
||
283 | |||
284 | /** |
||
285 | * Sets a new condition |
||
286 | * |
||
287 | * @param \AlgoWeb\ODataMetadata\MetadataV3\mapping\cs\TConditionType[] $condition |
||
288 | * @return self |
||
289 | */ |
||
290 | View Code Duplication | public function setCondition(array $condition) |
|
307 | |||
308 | public function isOK(&$msg = null) |
||
357 | } |
||
358 |
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.