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 GInlineExpressionsTrait 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 GInlineExpressionsTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | trait GInlineExpressionsTrait |
||
10 | { |
||
11 | use TQualifiedNameTrait, XSDTopLevelTrait, TGuidLiteralTrait; |
||
12 | |||
13 | /** |
||
14 | * @property string $string |
||
15 | */ |
||
16 | private $string = null; |
||
17 | |||
18 | /** |
||
19 | * @property mixed $binary |
||
20 | */ |
||
21 | private $binary = null; |
||
22 | |||
23 | /** |
||
24 | * @property integer $int |
||
25 | */ |
||
26 | private $int = null; |
||
27 | |||
28 | /** |
||
29 | * @property float $float |
||
30 | */ |
||
31 | private $float = null; |
||
32 | |||
33 | /** |
||
34 | * @property string $guid |
||
35 | */ |
||
36 | private $guid = null; |
||
37 | |||
38 | /** |
||
39 | * @property float $decimal |
||
40 | */ |
||
41 | private $decimal = null; |
||
42 | |||
43 | /** |
||
44 | * @property boolean $bool |
||
45 | */ |
||
46 | private $bool = null; |
||
47 | |||
48 | /** |
||
49 | * @property \DateTime $dateTime |
||
50 | */ |
||
51 | private $dateTime = null; |
||
52 | |||
53 | /** |
||
54 | * @property \DateTime $dateTimeOffset |
||
55 | */ |
||
56 | private $dateTimeOffset = null; |
||
57 | |||
58 | /** |
||
59 | * @property string $enum |
||
60 | */ |
||
61 | private $enum = null; |
||
62 | |||
63 | /** |
||
64 | * @property string $path |
||
65 | */ |
||
66 | private $path = null; |
||
67 | |||
68 | /** |
||
69 | * Gets as string |
||
70 | * |
||
71 | * @return string |
||
72 | */ |
||
73 | public function getString() |
||
77 | |||
78 | /** |
||
79 | * Sets a new string |
||
80 | * |
||
81 | * @param string $string |
||
82 | * @return self |
||
83 | */ |
||
84 | View Code Duplication | public function setString($string) |
|
94 | |||
95 | /** |
||
96 | * Gets as binary |
||
97 | * |
||
98 | * @return mixed |
||
99 | */ |
||
100 | public function getBinary() |
||
104 | |||
105 | /** |
||
106 | * Sets a new binary |
||
107 | * |
||
108 | * @param mixed $binary |
||
109 | * @return self |
||
110 | */ |
||
111 | public function setBinary($binary) |
||
121 | |||
122 | /** |
||
123 | * Gets as int |
||
124 | * |
||
125 | * @return integer |
||
126 | */ |
||
127 | public function getInt() |
||
131 | |||
132 | /** |
||
133 | * Sets a new int |
||
134 | * |
||
135 | * @param integer $int |
||
136 | * @return self |
||
137 | */ |
||
138 | public function setInt($int) |
||
148 | |||
149 | /** |
||
150 | * Gets as float |
||
151 | * |
||
152 | * @return float |
||
153 | */ |
||
154 | public function getFloat() |
||
158 | |||
159 | /** |
||
160 | * Sets a new float |
||
161 | * |
||
162 | * @param float $float |
||
163 | * @return self |
||
164 | */ |
||
165 | View Code Duplication | public function setFloat($float) |
|
175 | |||
176 | /** |
||
177 | * Gets as guid |
||
178 | * |
||
179 | * @return string |
||
180 | */ |
||
181 | public function getGuid() |
||
185 | |||
186 | /** |
||
187 | * Sets a new guid |
||
188 | * |
||
189 | * @param string $guid |
||
190 | * @return self |
||
191 | */ |
||
192 | View Code Duplication | public function setGuid($guid) |
|
202 | |||
203 | /** |
||
204 | * Gets as decimal |
||
205 | * |
||
206 | * @return float |
||
207 | */ |
||
208 | public function getDecimal() |
||
212 | |||
213 | /** |
||
214 | * Sets a new decimal |
||
215 | * |
||
216 | * @param float $decimal |
||
217 | * @return self |
||
218 | */ |
||
219 | View Code Duplication | public function setDecimal($decimal) |
|
229 | |||
230 | /** |
||
231 | * Gets as bool |
||
232 | * |
||
233 | * @return boolean |
||
234 | */ |
||
235 | public function getBool() |
||
239 | |||
240 | /** |
||
241 | * Sets a new bool |
||
242 | * |
||
243 | * @param boolean $bool |
||
244 | * @return self |
||
245 | */ |
||
246 | public function setBool($bool) |
||
256 | |||
257 | /** |
||
258 | * Gets as dateTime |
||
259 | * |
||
260 | * @return \DateTime |
||
261 | */ |
||
262 | public function getDateTime() |
||
266 | |||
267 | /** |
||
268 | * Sets a new dateTime |
||
269 | * |
||
270 | * @param \DateTime $dateTime |
||
271 | * @return self |
||
272 | */ |
||
273 | public function setDateTime(\DateTime $dateTime) |
||
283 | |||
284 | /** |
||
285 | * Gets as dateTimeOffset |
||
286 | * |
||
287 | * @return \DateTime |
||
288 | */ |
||
289 | public function getDateTimeOffset() |
||
293 | |||
294 | /** |
||
295 | * Sets a new dateTimeOffset |
||
296 | * |
||
297 | * @param \DateTime $dateTimeOffset |
||
298 | * @return self |
||
299 | */ |
||
300 | public function setDateTimeOffset(\DateTime $dateTimeOffset) |
||
310 | |||
311 | /** |
||
312 | * Gets as enum |
||
313 | * |
||
314 | * @return string |
||
315 | */ |
||
316 | public function getEnum() |
||
320 | |||
321 | /** |
||
322 | * Sets a new enum |
||
323 | * |
||
324 | * @param string $enum |
||
325 | * @return self |
||
326 | */ |
||
327 | View Code Duplication | public function setEnum($enum) |
|
337 | |||
338 | /** |
||
339 | * Gets as path |
||
340 | * |
||
341 | * @return string |
||
342 | */ |
||
343 | public function getPath() |
||
347 | |||
348 | /** |
||
349 | * Sets a new path |
||
350 | * |
||
351 | * @param string $path |
||
352 | * @return self |
||
353 | */ |
||
354 | View Code Duplication | public function setPath($path) |
|
364 | |||
365 | public function isGInlineExpressionsValid(&$msg = null) |
||
414 | } |
||
415 |
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.