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 GExpressionTrait 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 GExpressionTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
20 | trait GExpressionTrait |
||
21 | { |
||
22 | use XSDTopLevelTrait, IsOKToolboxTrait; |
||
23 | |||
24 | private $gExpressionMinimum = -1; |
||
25 | private $gExpressionMaximum = -1; |
||
26 | |||
27 | private $gExpressionSimpleFieldNames = ['string' => 'string', 'binary' => 'hexBinary', 'int' => 'integer', |
||
28 | 'float' => 'double', 'guid' => null, 'decimal' => 'decimal', 'bool' => null, 'dateTime' => 'dateTime', |
||
29 | 'dateTimeOffset' => 'dateTime', 'enum' => null, 'path' => null]; |
||
30 | private $gExpressionObjectFieldTypes = [ |
||
31 | 'if' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TIfExpressionType', |
||
32 | 'record' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TRecordExpressionType', |
||
33 | 'collection' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionExpressionType', |
||
34 | 'typeAssert' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeAssertExpressionType', |
||
35 | 'typeTest' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeTestExpressionType', |
||
36 | 'functionReference' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReferenceExpressionType', |
||
37 | 'entitySetReference' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TEntitySetReferenceExpressionType', |
||
38 | 'anonymousFunction' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TAnonymousFunctionExpressionType', |
||
39 | 'parameterReference' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TParameterReferenceExpressionType', |
||
40 | 'apply' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TApplyExpressionType', |
||
41 | 'propertyReference' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyReferenceExpressionType', |
||
42 | 'valueTermReference' => '\AlgoWeb\ODataMetadata\MetadataV3\edm\TValueTermReferenceExpressionType' |
||
43 | ]; |
||
44 | |||
45 | /** |
||
46 | * @property string $string |
||
47 | */ |
||
48 | private $string = null; |
||
49 | |||
50 | /** |
||
51 | * @property mixed $binary |
||
52 | */ |
||
53 | private $binary = null; |
||
54 | |||
55 | /** |
||
56 | * @property integer $int |
||
57 | */ |
||
58 | private $int = null; |
||
59 | |||
60 | /** |
||
61 | * @property float $float |
||
62 | */ |
||
63 | private $float = null; |
||
64 | |||
65 | /** |
||
66 | * @property string $guid |
||
67 | */ |
||
68 | private $guid = null; |
||
69 | |||
70 | /** |
||
71 | * @property float $decimal |
||
72 | */ |
||
73 | private $decimal = null; |
||
74 | |||
75 | /** |
||
76 | * @property boolean $bool |
||
77 | */ |
||
78 | private $bool = null; |
||
79 | |||
80 | /** |
||
81 | * @property \DateTime $dateTime |
||
82 | */ |
||
83 | private $dateTime = null; |
||
84 | |||
85 | /** |
||
86 | * @property \DateTime $dateTimeOffset |
||
87 | */ |
||
88 | private $dateTimeOffset = null; |
||
89 | |||
90 | /** |
||
91 | * @property string $enum |
||
92 | */ |
||
93 | private $enum = null; |
||
94 | |||
95 | /** |
||
96 | * @property string $path |
||
97 | */ |
||
98 | private $path = null; |
||
99 | |||
100 | /** |
||
101 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TIfExpressionType $if |
||
102 | */ |
||
103 | private $if = null; |
||
104 | |||
105 | /** |
||
106 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TRecordExpressionType $record |
||
107 | */ |
||
108 | private $record = null; |
||
109 | |||
110 | /** |
||
111 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionExpressionType $collection |
||
112 | */ |
||
113 | private $collection = null; |
||
114 | |||
115 | /** |
||
116 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeAssertExpressionType $typeAssert |
||
117 | */ |
||
118 | private $typeAssert = null; |
||
119 | |||
120 | /** |
||
121 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeTestExpressionType $typeTest |
||
122 | */ |
||
123 | private $typeTest = null; |
||
124 | |||
125 | /** |
||
126 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReferenceExpressionType $functionReference |
||
127 | */ |
||
128 | private $functionReference = null; |
||
129 | |||
130 | /** |
||
131 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntitySetReferenceExpressionType $entitySetReference |
||
132 | */ |
||
133 | private $entitySetReference = null; |
||
134 | |||
135 | /** |
||
136 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TAnonymousFunctionExpressionType $anonymousFunction |
||
137 | */ |
||
138 | private $anonymousFunction = null; |
||
139 | |||
140 | /** |
||
141 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TParameterReferenceExpressionType $parameterReference |
||
142 | */ |
||
143 | private $parameterReference = null; |
||
144 | |||
145 | /** |
||
146 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TApplyExpressionType $apply |
||
147 | */ |
||
148 | private $apply = null; |
||
149 | |||
150 | /** |
||
151 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyReferenceExpressionType $propertyReference |
||
152 | */ |
||
153 | private $propertyReference = null; |
||
154 | |||
155 | /** |
||
156 | * @property \AlgoWeb\ODataMetadata\MetadataV3\edm\TValueTermReferenceExpressionType $valueTermReference |
||
157 | */ |
||
158 | private $valueTermReference = null; |
||
159 | |||
160 | /** |
||
161 | * Gets as string |
||
162 | * |
||
163 | * @return string |
||
164 | */ |
||
165 | public function getString() |
||
169 | |||
170 | /** |
||
171 | * Sets a new string |
||
172 | * |
||
173 | * @param string $string |
||
174 | * @return self |
||
175 | */ |
||
176 | public function setString($string) |
||
181 | |||
182 | /** |
||
183 | * Gets as binary |
||
184 | * |
||
185 | * @return mixed |
||
186 | */ |
||
187 | public function getBinary() |
||
191 | |||
192 | /** |
||
193 | * Sets a new binary |
||
194 | * |
||
195 | * @param mixed $binary |
||
196 | * @return self |
||
197 | */ |
||
198 | public function setBinary($binary) |
||
203 | |||
204 | /** |
||
205 | * Gets as int |
||
206 | * |
||
207 | * @return integer |
||
208 | */ |
||
209 | public function getInt() |
||
213 | |||
214 | /** |
||
215 | * Sets a new int |
||
216 | * |
||
217 | * @param integer $int |
||
218 | * @return self |
||
219 | */ |
||
220 | public function setInt($int) |
||
225 | |||
226 | /** |
||
227 | * Gets as float |
||
228 | * |
||
229 | * @return float |
||
230 | */ |
||
231 | public function getFloat() |
||
235 | |||
236 | /** |
||
237 | * Sets a new float |
||
238 | * |
||
239 | * @param float $float |
||
240 | * @return self |
||
241 | */ |
||
242 | public function setFloat($float) |
||
247 | |||
248 | /** |
||
249 | * Gets as guid |
||
250 | * |
||
251 | * @return string |
||
252 | */ |
||
253 | public function getGuid() |
||
257 | |||
258 | /** |
||
259 | * Sets a new guid |
||
260 | * |
||
261 | * @param string $guid |
||
262 | * @return self |
||
263 | */ |
||
264 | View Code Duplication | public function setGuid($guid) |
|
274 | |||
275 | /** |
||
276 | * Gets as decimal |
||
277 | * |
||
278 | * @return float |
||
279 | */ |
||
280 | public function getDecimal() |
||
284 | |||
285 | /** |
||
286 | * Sets a new decimal |
||
287 | * |
||
288 | * @param float $decimal |
||
289 | * @return self |
||
290 | */ |
||
291 | public function setDecimal($decimal) |
||
296 | |||
297 | /** |
||
298 | * Gets as bool |
||
299 | * |
||
300 | * @return boolean |
||
301 | */ |
||
302 | public function getBool() |
||
306 | |||
307 | /** |
||
308 | * Sets a new bool |
||
309 | * |
||
310 | * @param boolean $bool |
||
311 | * @return self |
||
312 | */ |
||
313 | public function setBool($bool) |
||
318 | |||
319 | /** |
||
320 | * Gets as dateTime |
||
321 | * |
||
322 | * @return \DateTime |
||
323 | */ |
||
324 | public function getDateTime() |
||
328 | |||
329 | /** |
||
330 | * Sets a new dateTime |
||
331 | * |
||
332 | * @param \DateTime $dateTime |
||
333 | * @return self |
||
334 | */ |
||
335 | public function setDateTime(\DateTime $dateTime) |
||
340 | |||
341 | /** |
||
342 | * Gets as dateTimeOffset |
||
343 | * |
||
344 | * @return \DateTime |
||
345 | */ |
||
346 | public function getDateTimeOffset() |
||
350 | |||
351 | /** |
||
352 | * Sets a new dateTimeOffset |
||
353 | * |
||
354 | * @param \DateTime $dateTimeOffset |
||
355 | * @return self |
||
356 | */ |
||
357 | public function setDateTimeOffset(\DateTime $dateTimeOffset) |
||
362 | |||
363 | /** |
||
364 | * Gets as enum |
||
365 | * |
||
366 | * @return string |
||
367 | */ |
||
368 | public function getEnum() |
||
372 | |||
373 | /** |
||
374 | * Sets a new enum |
||
375 | * |
||
376 | * @param string $enum |
||
377 | * @return self |
||
378 | */ |
||
379 | View Code Duplication | public function setEnum($enum) |
|
389 | |||
390 | /** |
||
391 | * Gets as path |
||
392 | * |
||
393 | * @return string |
||
394 | */ |
||
395 | public function getPath() |
||
399 | |||
400 | /** |
||
401 | * Sets a new path |
||
402 | * |
||
403 | * @param string $path |
||
404 | * @return self |
||
405 | */ |
||
406 | View Code Duplication | public function setPath($path) |
|
416 | |||
417 | /** |
||
418 | * Gets as if |
||
419 | * |
||
420 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TIfExpressionType |
||
421 | */ |
||
422 | public function getIf() |
||
426 | |||
427 | /** |
||
428 | * Sets a new if |
||
429 | * |
||
430 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TIfExpressionType $if |
||
431 | * @return self |
||
432 | */ |
||
433 | public function setIf(TIfExpressionType $if) |
||
442 | |||
443 | /** |
||
444 | * Gets as record |
||
445 | * |
||
446 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TRecordExpressionType |
||
447 | */ |
||
448 | public function getRecord() |
||
452 | |||
453 | /** |
||
454 | * Sets a new record |
||
455 | * |
||
456 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TRecordExpressionType $record |
||
457 | * @return self |
||
458 | */ |
||
459 | public function setRecord(TRecordExpressionType $record) |
||
468 | |||
469 | /** |
||
470 | * Gets as collection |
||
471 | * |
||
472 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionExpressionType |
||
473 | */ |
||
474 | public function getCollection() |
||
478 | |||
479 | /** |
||
480 | * Sets a new collection |
||
481 | * |
||
482 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TCollectionExpressionType $collection |
||
483 | * @return self |
||
484 | */ |
||
485 | public function setCollection(TCollectionExpressionType $collection) |
||
494 | |||
495 | /** |
||
496 | * Gets as typeAssert |
||
497 | * |
||
498 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeAssertExpressionType |
||
499 | */ |
||
500 | public function getTypeAssert() |
||
504 | |||
505 | /** |
||
506 | * Sets a new typeAssert |
||
507 | * |
||
508 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeAssertExpressionType $typeAssert |
||
509 | * @return self |
||
510 | */ |
||
511 | public function setTypeAssert(TTypeAssertExpressionType $typeAssert) |
||
520 | |||
521 | /** |
||
522 | * Gets as typeTest |
||
523 | * |
||
524 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeTestExpressionType |
||
525 | */ |
||
526 | public function getTypeTest() |
||
530 | |||
531 | /** |
||
532 | * Sets a new typeTest |
||
533 | * |
||
534 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TTypeTestExpressionType $typeTest |
||
535 | * @return self |
||
536 | */ |
||
537 | public function setTypeTest(TTypeTestExpressionType $typeTest) |
||
546 | |||
547 | /** |
||
548 | * Gets as functionReference |
||
549 | * |
||
550 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReferenceExpressionType |
||
551 | */ |
||
552 | public function getFunctionReference() |
||
556 | |||
557 | /** |
||
558 | * Sets a new functionReference |
||
559 | * |
||
560 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TFunctionReferenceExpressionType $functionReference |
||
561 | * @return self |
||
562 | */ |
||
563 | public function setFunctionReference(TFunctionReferenceExpressionType $functionReference) |
||
572 | |||
573 | /** |
||
574 | * Gets as entitySetReference |
||
575 | * |
||
576 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntitySetReferenceExpressionType |
||
577 | */ |
||
578 | public function getEntitySetReference() |
||
582 | |||
583 | /** |
||
584 | * Sets a new entitySetReference |
||
585 | * |
||
586 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TEntitySetReferenceExpressionType $entitySetReference |
||
587 | * @return self |
||
588 | */ |
||
589 | public function setEntitySetReference(TEntitySetReferenceExpressionType $entitySetReference) |
||
598 | |||
599 | /** |
||
600 | * Gets as anonymousFunction |
||
601 | * |
||
602 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TAnonymousFunctionExpressionType |
||
603 | */ |
||
604 | public function getAnonymousFunction() |
||
608 | |||
609 | /** |
||
610 | * Sets a new anonymousFunction |
||
611 | * |
||
612 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TAnonymousFunctionExpressionType $anonymousFunction |
||
613 | * @return self |
||
614 | */ |
||
615 | public function setAnonymousFunction(TAnonymousFunctionExpressionType $anonymousFunction) |
||
624 | |||
625 | /** |
||
626 | * Gets as parameterReference |
||
627 | * |
||
628 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TParameterReferenceExpressionType |
||
629 | */ |
||
630 | public function getParameterReference() |
||
634 | |||
635 | /** |
||
636 | * Sets a new parameterReference |
||
637 | * |
||
638 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TParameterReferenceExpressionType $parameterReference |
||
639 | * @return self |
||
640 | */ |
||
641 | public function setParameterReference(TParameterReferenceExpressionType $parameterReference) |
||
650 | |||
651 | /** |
||
652 | * Gets as apply |
||
653 | * |
||
654 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TApplyExpressionType |
||
655 | */ |
||
656 | public function getApply() |
||
660 | |||
661 | /** |
||
662 | * Sets a new apply |
||
663 | * |
||
664 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TApplyExpressionType $apply |
||
665 | * @return self |
||
666 | */ |
||
667 | public function setApply(TApplyExpressionType $apply) |
||
676 | |||
677 | /** |
||
678 | * Gets as propertyReference |
||
679 | * |
||
680 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyReferenceExpressionType |
||
681 | */ |
||
682 | public function getPropertyReference() |
||
686 | |||
687 | /** |
||
688 | * Sets a new propertyReference |
||
689 | * |
||
690 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TPropertyReferenceExpressionType $propertyReference |
||
691 | * @return self |
||
692 | */ |
||
693 | public function setPropertyReference(TPropertyReferenceExpressionType $propertyReference) |
||
702 | |||
703 | /** |
||
704 | * Gets as valueTermReference |
||
705 | * |
||
706 | * @return \AlgoWeb\ODataMetadata\MetadataV3\edm\TValueTermReferenceExpressionType |
||
707 | */ |
||
708 | public function getValueTermReference() |
||
712 | |||
713 | /** |
||
714 | * Sets a new valueTermReference |
||
715 | * |
||
716 | * @param \AlgoWeb\ODataMetadata\MetadataV3\edm\TValueTermReferenceExpressionType $valueTermReference |
||
717 | * @return self |
||
718 | */ |
||
719 | public function setValueTermReference(TValueTermReferenceExpressionType $valueTermReference) |
||
728 | |||
729 | public function isGExpressionValid(&$msg = null) |
||
783 | } |
||
784 |
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.