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 TPropertyType 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 TPropertyType, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
15 | class TPropertyType extends IsOK |
||
16 | { |
||
17 | use TSimpleIdentifierTrait, TPropertyTypeTrait, GEmptyElementExtensibilityTrait; |
||
18 | /** |
||
19 | * @property string $name |
||
20 | */ |
||
21 | private $name = null; |
||
22 | |||
23 | /** |
||
24 | * @property string $type |
||
25 | */ |
||
26 | private $type = null; |
||
27 | |||
28 | /** |
||
29 | * @property boolean $nullable |
||
30 | */ |
||
31 | private $nullable = true; |
||
32 | |||
33 | /** |
||
34 | * @property string $defaultValue |
||
35 | */ |
||
36 | private $defaultValue = null; |
||
37 | |||
38 | /** |
||
39 | * @property string $maxLength |
||
40 | */ |
||
41 | private $maxLength = null; |
||
42 | |||
43 | /** |
||
44 | * @property boolean $fixedLength |
||
45 | */ |
||
46 | private $fixedLength = null; |
||
47 | |||
48 | /** |
||
49 | * @property integer $precision |
||
50 | */ |
||
51 | private $precision = null; |
||
52 | |||
53 | /** |
||
54 | * @property integer $scale |
||
55 | */ |
||
56 | private $scale = null; |
||
57 | |||
58 | /** |
||
59 | * @property boolean $unicode |
||
60 | */ |
||
61 | private $unicode = null; |
||
62 | |||
63 | /** |
||
64 | * @property string $collation |
||
65 | */ |
||
66 | private $collation = null; |
||
67 | |||
68 | /** |
||
69 | * @property string $sRID |
||
70 | */ |
||
71 | private $sRID = null; |
||
72 | |||
73 | /** |
||
74 | * Gets as name |
||
75 | * |
||
76 | * @return string |
||
77 | */ |
||
78 | public function getName() |
||
82 | |||
83 | /** |
||
84 | * Sets a new name |
||
85 | * |
||
86 | * @param string $name |
||
87 | * @return self |
||
88 | */ |
||
89 | View Code Duplication | public function setName($name) |
|
103 | |||
104 | /** |
||
105 | * Gets as type |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | public function getType() |
||
113 | |||
114 | /** |
||
115 | * Sets a new type |
||
116 | * |
||
117 | * @param string $type |
||
118 | * @return self |
||
119 | */ |
||
120 | View Code Duplication | public function setType($type) |
|
134 | |||
135 | /** |
||
136 | * Gets as nullable |
||
137 | * |
||
138 | * @return boolean |
||
139 | */ |
||
140 | public function getNullable() |
||
144 | |||
145 | /** |
||
146 | * Sets a new nullable |
||
147 | * |
||
148 | * @param boolean $nullable |
||
149 | * @return self |
||
150 | */ |
||
151 | public function setNullable($nullable) |
||
156 | |||
157 | /** |
||
158 | * Gets as defaultValue |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | public function getDefaultValue() |
||
166 | |||
167 | /** |
||
168 | * Sets a new defaultValue |
||
169 | * |
||
170 | * @param string $defaultValue |
||
171 | * @return self |
||
172 | */ |
||
173 | View Code Duplication | public function setDefaultValue($defaultValue) |
|
183 | |||
184 | /** |
||
185 | * Gets as maxLength |
||
186 | * |
||
187 | * @return string |
||
188 | */ |
||
189 | public function getMaxLength() |
||
193 | |||
194 | /** |
||
195 | * Sets a new maxLength |
||
196 | * |
||
197 | * @param string $maxLength |
||
198 | * @return self |
||
199 | */ |
||
200 | public function setMaxLength($maxLength) |
||
210 | |||
211 | /** |
||
212 | * Gets as fixedLength |
||
213 | * |
||
214 | * @return boolean |
||
215 | */ |
||
216 | public function getFixedLength() |
||
220 | |||
221 | /** |
||
222 | * Sets a new fixedLength |
||
223 | * |
||
224 | * @param boolean $fixedLength |
||
225 | * @return self |
||
226 | */ |
||
227 | public function setFixedLength($fixedLength) |
||
232 | |||
233 | /** |
||
234 | * Gets as precision |
||
235 | * |
||
236 | * @return integer |
||
237 | */ |
||
238 | public function getPrecision() |
||
242 | |||
243 | /** |
||
244 | * Sets a new precision |
||
245 | * |
||
246 | * @param integer $precision |
||
247 | * @return self |
||
248 | */ |
||
249 | View Code Duplication | public function setPrecision($precision) |
|
259 | |||
260 | /** |
||
261 | * Gets as scale |
||
262 | * |
||
263 | * @return integer |
||
264 | */ |
||
265 | public function getScale() |
||
269 | |||
270 | /** |
||
271 | * Sets a new scale |
||
272 | * |
||
273 | * @param integer $scale |
||
274 | * @return self |
||
275 | */ |
||
276 | View Code Duplication | public function setScale($scale) |
|
286 | |||
287 | /** |
||
288 | * Gets as unicode |
||
289 | * |
||
290 | * @return boolean |
||
291 | */ |
||
292 | public function getUnicode() |
||
296 | |||
297 | /** |
||
298 | * Sets a new unicode |
||
299 | * |
||
300 | * @param boolean $unicode |
||
301 | * @return self |
||
302 | */ |
||
303 | public function setUnicode($unicode) |
||
308 | |||
309 | /** |
||
310 | * Gets as collation |
||
311 | * |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getCollation() |
||
318 | |||
319 | /** |
||
320 | * Sets a new collation |
||
321 | * |
||
322 | * @param string $collation |
||
323 | * @return self |
||
324 | */ |
||
325 | View Code Duplication | public function setCollation($collation) |
|
335 | |||
336 | /** |
||
337 | * Gets as sRID |
||
338 | * |
||
339 | * @return string |
||
340 | */ |
||
341 | public function getSRID() |
||
345 | |||
346 | /** |
||
347 | * Sets a new sRID |
||
348 | * |
||
349 | * @param string $sRID |
||
350 | * @return self |
||
351 | */ |
||
352 | View Code Duplication | public function setSRID($sRID) |
|
362 | |||
363 | public function isOK(&$msg = null) |
||
410 | } |
||
411 |
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.