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 TFunctionType 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 TFunctionType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class TFunctionType extends IsOK |
||
18 | { |
||
19 | use TUndottedIdentifierTrait, TSimpleIdentifierTrait, TParameterTypeSemanticsTrait, TCommandTextTrait; |
||
20 | /** |
||
21 | * @property string $name |
||
22 | */ |
||
23 | private $name = null; |
||
24 | |||
25 | /** |
||
26 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TFunctionReturnTypeType[] $returnType |
||
27 | */ |
||
28 | private $returnType = []; |
||
29 | |||
30 | /** |
||
31 | * @property boolean $aggregate |
||
32 | */ |
||
33 | private $aggregate = null; |
||
34 | |||
35 | /** |
||
36 | * @property boolean $builtIn |
||
37 | */ |
||
38 | private $builtIn = null; |
||
39 | |||
40 | /** |
||
41 | * @property string $storeFunctionName |
||
42 | */ |
||
43 | private $storeFunctionName = null; |
||
44 | |||
45 | /** |
||
46 | * @property boolean $niladicFunction |
||
47 | */ |
||
48 | private $niladicFunction = null; |
||
49 | |||
50 | /** |
||
51 | * @property boolean $isComposable |
||
52 | */ |
||
53 | private $isComposable = null; |
||
54 | |||
55 | /** |
||
56 | * @property string $parameterTypeSemantics |
||
57 | */ |
||
58 | private $parameterTypeSemantics = "AllowImplicitConversion"; |
||
59 | |||
60 | /** |
||
61 | * @property string $schema |
||
62 | */ |
||
63 | private $schema = null; |
||
64 | |||
65 | /** |
||
66 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TDocumentationType $documentation |
||
67 | */ |
||
68 | private $documentation = null; |
||
69 | |||
70 | /** |
||
71 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TParameterType[] $parameter |
||
72 | */ |
||
73 | private $parameter = []; |
||
74 | |||
75 | /** |
||
76 | * @property string[] $commandText |
||
77 | */ |
||
78 | private $commandText = []; |
||
79 | |||
80 | /** |
||
81 | * Gets as name |
||
82 | * |
||
83 | * @return string |
||
84 | */ |
||
85 | public function getName() |
||
89 | |||
90 | /** |
||
91 | * Sets a new name |
||
92 | * |
||
93 | * @param string $name |
||
94 | * @return self |
||
95 | */ |
||
96 | View Code Duplication | public function setName($name) |
|
110 | |||
111 | /** |
||
112 | * Adds as returnType |
||
113 | * |
||
114 | * @return self |
||
115 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TFunctionReturnTypeType $returnType |
||
116 | */ |
||
117 | View Code Duplication | public function addToReturnType(TFunctionReturnTypeType $returnType) |
|
126 | |||
127 | /** |
||
128 | * isset returnType |
||
129 | * |
||
130 | * @param scalar $index |
||
131 | * @return boolean |
||
132 | */ |
||
133 | public function issetReturnType($index) |
||
137 | |||
138 | /** |
||
139 | * unset returnType |
||
140 | * |
||
141 | * @param scalar $index |
||
142 | * @return void |
||
143 | */ |
||
144 | public function unsetReturnType($index) |
||
148 | |||
149 | /** |
||
150 | * Gets as returnType |
||
151 | * |
||
152 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TFunctionReturnTypeType[] |
||
153 | */ |
||
154 | public function getReturnType() |
||
158 | |||
159 | /** |
||
160 | * Sets a new returnType |
||
161 | * |
||
162 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TFunctionReturnTypeType[] $returnType |
||
163 | * @return self |
||
164 | */ |
||
165 | View Code Duplication | public function setReturnType(array $returnType) |
|
178 | |||
179 | /** |
||
180 | * Gets as aggregate |
||
181 | * |
||
182 | * @return boolean |
||
183 | */ |
||
184 | public function getAggregate() |
||
188 | |||
189 | /** |
||
190 | * Sets a new aggregate |
||
191 | * |
||
192 | * @param boolean $aggregate |
||
193 | * @return self |
||
194 | */ |
||
195 | public function setAggregate($aggregate) |
||
200 | |||
201 | /** |
||
202 | * Gets as builtIn |
||
203 | * |
||
204 | * @return boolean |
||
205 | */ |
||
206 | public function getBuiltIn() |
||
210 | |||
211 | /** |
||
212 | * Sets a new builtIn |
||
213 | * |
||
214 | * @param boolean $builtIn |
||
215 | * @return self |
||
216 | */ |
||
217 | public function setBuiltIn($builtIn) |
||
222 | |||
223 | /** |
||
224 | * Gets as storeFunctionName |
||
225 | * |
||
226 | * @return string |
||
227 | */ |
||
228 | public function getStoreFunctionName() |
||
232 | |||
233 | /** |
||
234 | * Sets a new storeFunctionName |
||
235 | * |
||
236 | * @param string $storeFunctionName |
||
237 | * @return self |
||
238 | */ |
||
239 | public function setStoreFunctionName($storeFunctionName) |
||
244 | |||
245 | /** |
||
246 | * Gets as niladicFunction |
||
247 | * |
||
248 | * @return boolean |
||
249 | */ |
||
250 | public function getNiladicFunction() |
||
254 | |||
255 | /** |
||
256 | * Sets a new niladicFunction |
||
257 | * |
||
258 | * @param boolean $niladicFunction |
||
259 | * @return self |
||
260 | */ |
||
261 | public function setNiladicFunction($niladicFunction) |
||
266 | |||
267 | /** |
||
268 | * Gets as isComposable |
||
269 | * |
||
270 | * @return boolean |
||
271 | */ |
||
272 | public function getIsComposable() |
||
276 | |||
277 | /** |
||
278 | * Sets a new isComposable |
||
279 | * |
||
280 | * @param boolean $isComposable |
||
281 | * @return self |
||
282 | */ |
||
283 | public function setIsComposable($isComposable) |
||
288 | |||
289 | /** |
||
290 | * Gets as parameterTypeSemantics |
||
291 | * |
||
292 | * @return string |
||
293 | */ |
||
294 | public function getParameterTypeSemantics() |
||
298 | |||
299 | /** |
||
300 | * Sets a new parameterTypeSemantics |
||
301 | * |
||
302 | * @param string $parameterTypeSemantics |
||
303 | * @return self |
||
304 | */ |
||
305 | public function setParameterTypeSemantics($parameterTypeSemantics) |
||
320 | |||
321 | /** |
||
322 | * Gets as schema |
||
323 | * |
||
324 | * @return string |
||
325 | */ |
||
326 | public function getSchema() |
||
330 | |||
331 | /** |
||
332 | * Sets a new schema |
||
333 | * |
||
334 | * @param string $schema |
||
335 | * @return self |
||
336 | */ |
||
337 | public function setSchema($schema) |
||
351 | |||
352 | /** |
||
353 | * Gets as documentation |
||
354 | * |
||
355 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TDocumentationType |
||
356 | */ |
||
357 | public function getDocumentation() |
||
361 | |||
362 | /** |
||
363 | * Sets a new documentation |
||
364 | * |
||
365 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TDocumentationType $documentation |
||
366 | * @return self |
||
367 | */ |
||
368 | View Code Duplication | public function setDocumentation(TDocumentationType $documentation) |
|
377 | |||
378 | /** |
||
379 | * Adds as parameter |
||
380 | * |
||
381 | * @return self |
||
382 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TParameterType $parameter |
||
383 | */ |
||
384 | View Code Duplication | public function addToParameter(TParameterType $parameter) |
|
393 | |||
394 | /** |
||
395 | * isset parameter |
||
396 | * |
||
397 | * @param scalar $index |
||
398 | * @return boolean |
||
399 | */ |
||
400 | public function issetParameter($index) |
||
404 | |||
405 | /** |
||
406 | * unset parameter |
||
407 | * |
||
408 | * @param scalar $index |
||
409 | * @return void |
||
410 | */ |
||
411 | public function unsetParameter($index) |
||
415 | |||
416 | /** |
||
417 | * Gets as parameter |
||
418 | * |
||
419 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TParameterType[] |
||
420 | */ |
||
421 | public function getParameter() |
||
425 | |||
426 | /** |
||
427 | * Sets a new parameter |
||
428 | * |
||
429 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\ssdl\TParameterType[] $parameter |
||
430 | * @return self |
||
431 | */ |
||
432 | View Code Duplication | public function setParameter(array $parameter) |
|
445 | |||
446 | /** |
||
447 | * Adds as commandText |
||
448 | * |
||
449 | * @return self |
||
450 | * @param string $commandText |
||
451 | */ |
||
452 | View Code Duplication | public function addToCommandText($commandText) |
|
462 | |||
463 | /** |
||
464 | * isset commandText |
||
465 | * |
||
466 | * @param scalar $index |
||
467 | * @return boolean |
||
468 | */ |
||
469 | public function issetCommandText($index) |
||
473 | |||
474 | /** |
||
475 | * unset commandText |
||
476 | * |
||
477 | * @param scalar $index |
||
478 | * @return void |
||
479 | */ |
||
480 | public function unsetCommandText($index) |
||
484 | |||
485 | /** |
||
486 | * Gets as commandText |
||
487 | * |
||
488 | * @return string[] |
||
489 | */ |
||
490 | public function getCommandText() |
||
494 | |||
495 | /** |
||
496 | * Sets a new commandText |
||
497 | * |
||
498 | * @param string $commandText |
||
499 | * @return self |
||
500 | */ |
||
501 | View Code Duplication | public function setCommandText(array $commandText) |
|
512 | |||
513 | public function isOK(&$msg = null) |
||
570 | } |
||
571 |
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.