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 TTypeAssertExpressionType 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 TTypeAssertExpressionType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
16 | class TTypeAssertExpressionType extends IsOK |
||
17 | { |
||
18 | use TFacetAttributesTrait, TWrappedFunctionTypeTrait; |
||
19 | /** |
||
20 | * @property string $type |
||
21 | */ |
||
22 | private $type = null; |
||
23 | |||
24 | /** |
||
25 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TOperandType[] $operand |
||
26 | */ |
||
27 | private $operand = []; |
||
28 | |||
29 | /** |
||
30 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionTypeType[] $collectionType |
||
31 | */ |
||
32 | private $collectionType = []; |
||
33 | |||
34 | /** |
||
35 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TReferenceTypeType[] $referenceType |
||
36 | */ |
||
37 | private $referenceType = []; |
||
38 | |||
39 | /** |
||
40 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyType[] $rowType |
||
41 | */ |
||
42 | private $rowType = []; |
||
43 | |||
44 | /** |
||
45 | * Gets as type |
||
46 | * |
||
47 | * @return string |
||
48 | */ |
||
49 | public function getType() |
||
53 | |||
54 | /** |
||
55 | * Sets a new type |
||
56 | * |
||
57 | * @param string $type |
||
58 | * @return self |
||
59 | */ |
||
60 | public function setType($type) |
||
69 | |||
70 | /** |
||
71 | * Adds as operand |
||
72 | * |
||
73 | * @return self |
||
74 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TOperandType $operand |
||
75 | */ |
||
76 | View Code Duplication | public function addToOperand(TOperandType $operand) |
|
85 | |||
86 | /** |
||
87 | * isset operand |
||
88 | * |
||
89 | * @param scalar $index |
||
90 | * @return boolean |
||
91 | */ |
||
92 | public function issetOperand($index) |
||
96 | |||
97 | /** |
||
98 | * unset operand |
||
99 | * |
||
100 | * @param scalar $index |
||
101 | * @return void |
||
102 | */ |
||
103 | public function unsetOperand($index) |
||
107 | |||
108 | /** |
||
109 | * Gets as operand |
||
110 | * |
||
111 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TOperandType[] |
||
112 | */ |
||
113 | public function getOperand() |
||
117 | |||
118 | /** |
||
119 | * Sets a new operand |
||
120 | * |
||
121 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TOperandType[] $operand |
||
122 | * @return self |
||
123 | */ |
||
124 | public function setOperand(array $operand) |
||
132 | |||
133 | /** |
||
134 | * Adds as collectionType |
||
135 | * |
||
136 | * @return self |
||
137 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionTypeType $collectionType |
||
138 | */ |
||
139 | public function addToCollectionType(TCollectionTypeType $collectionType) |
||
148 | |||
149 | /** |
||
150 | * isset collectionType |
||
151 | * |
||
152 | * @param scalar $index |
||
153 | * @return boolean |
||
154 | */ |
||
155 | public function issetCollectionType($index) |
||
159 | |||
160 | /** |
||
161 | * unset collectionType |
||
162 | * |
||
163 | * @param scalar $index |
||
164 | * @return void |
||
165 | */ |
||
166 | public function unsetCollectionType($index) |
||
170 | |||
171 | /** |
||
172 | * Gets as collectionType |
||
173 | * |
||
174 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionTypeType[] |
||
175 | */ |
||
176 | public function getCollectionType() |
||
180 | |||
181 | /** |
||
182 | * Sets a new collectionType |
||
183 | * |
||
184 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionTypeType[] $collectionType |
||
185 | * @return self |
||
186 | */ |
||
187 | public function setCollectionType(array $collectionType) |
||
199 | |||
200 | /** |
||
201 | * Adds as referenceType |
||
202 | * |
||
203 | * @return self |
||
204 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TReferenceTypeType $referenceType |
||
205 | */ |
||
206 | public function addToReferenceType(TReferenceTypeType $referenceType) |
||
215 | |||
216 | /** |
||
217 | * isset referenceType |
||
218 | * |
||
219 | * @param scalar $index |
||
220 | * @return boolean |
||
221 | */ |
||
222 | public function issetReferenceType($index) |
||
226 | |||
227 | /** |
||
228 | * unset referenceType |
||
229 | * |
||
230 | * @param scalar $index |
||
231 | * @return void |
||
232 | */ |
||
233 | public function unsetReferenceType($index) |
||
237 | |||
238 | /** |
||
239 | * Gets as referenceType |
||
240 | * |
||
241 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TReferenceTypeType[] |
||
242 | */ |
||
243 | public function getReferenceType() |
||
247 | |||
248 | /** |
||
249 | * Sets a new referenceType |
||
250 | * |
||
251 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TReferenceTypeType[] $referenceType |
||
252 | * @return self |
||
253 | */ |
||
254 | public function setReferenceType(array $referenceType) |
||
266 | |||
267 | /** |
||
268 | * Adds as property |
||
269 | * |
||
270 | * @return self |
||
271 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyType $property |
||
272 | */ |
||
273 | public function addToRowType(TPropertyType $property) |
||
282 | |||
283 | /** |
||
284 | * isset rowType |
||
285 | * |
||
286 | * @param scalar $index |
||
287 | * @return boolean |
||
288 | */ |
||
289 | public function issetRowType($index) |
||
293 | |||
294 | /** |
||
295 | * unset rowType |
||
296 | * |
||
297 | * @param scalar $index |
||
298 | * @return void |
||
299 | */ |
||
300 | public function unsetRowType($index) |
||
304 | |||
305 | /** |
||
306 | * Gets as rowType |
||
307 | * |
||
308 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyType[] |
||
309 | */ |
||
310 | public function getRowType() |
||
314 | |||
315 | /** |
||
316 | * Sets a new rowType |
||
317 | * |
||
318 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyType[] $rowType |
||
319 | * @return self |
||
320 | */ |
||
321 | public function setRowType(array $rowType) |
||
333 | |||
334 | public function isOK(&$msg = null) |
||
377 | } |
||
378 |
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.