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 TCommonPropertyAttributesTrait 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 TCommonPropertyAttributesTrait, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
18 | trait TCommonPropertyAttributesTrait |
||
19 | { |
||
20 | use TSimpleIdentifierTrait, TMaxLengthFacetTrait, TIsFixedLengthFacetTrait, TPrecisionFacetTrait, TScaleFacetTrait, |
||
21 | TIsUnicodeFacetTrait, TCollationFacetTrait, TSridFacetTrait, TConcurrencyModeTrait, AccessTypeTraits, |
||
22 | IsOKToolboxTrait, TPropertyTypeTrait { |
||
23 | TMaxLengthFacetTrait::normaliseString insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
24 | TMaxLengthFacetTrait::preserveString insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
25 | TMaxLengthFacetTrait::replaceString insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
26 | TMaxLengthFacetTrait::collapseString insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
27 | TMaxLengthFacetTrait::token insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
28 | TMaxLengthFacetTrait::string insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
29 | TMaxLengthFacetTrait::integer insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
30 | TMaxLengthFacetTrait::nonNegativeInteger insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
31 | TMaxLengthFacetTrait::decimal insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
32 | TMaxLengthFacetTrait::double insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
33 | TMaxLengthFacetTrait::dateTime insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
34 | TMaxLengthFacetTrait::hexBinary insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
35 | TSimpleIdentifierTrait::isNCName insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait, |
||
36 | TMaxLengthFacetTrait, TPropertyTypeTrait; |
||
37 | TSimpleIdentifierTrait::matchesRegexPattern insteadof TPrecisionFacetTrait, TScaleFacetTrait, |
||
38 | TSridFacetTrait, TMaxLengthFacetTrait, TPropertyTypeTrait; |
||
39 | TSimpleIdentifierTrait::isName insteadof TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait, |
||
40 | TMaxLengthFacetTrait, TPropertyTypeTrait; |
||
41 | } |
||
42 | |||
43 | /** |
||
44 | * @property string $name |
||
45 | */ |
||
46 | private $name = null; |
||
47 | |||
48 | /** |
||
49 | * @property string $type |
||
50 | */ |
||
51 | private $type = null; |
||
52 | |||
53 | /** |
||
54 | * @property boolean $nullable |
||
55 | */ |
||
56 | private $nullable = true; |
||
57 | |||
58 | /** |
||
59 | * @property string $defaultValue |
||
60 | */ |
||
61 | private $defaultValue = null; |
||
62 | |||
63 | /** |
||
64 | * @property string $maxLength |
||
65 | */ |
||
66 | private $maxLength = null; |
||
67 | |||
68 | /** |
||
69 | * @property boolean $fixedLength |
||
70 | */ |
||
71 | private $fixedLength = null; |
||
72 | |||
73 | /** |
||
74 | * @property integer $precision |
||
75 | */ |
||
76 | private $precision = null; |
||
77 | |||
78 | /** |
||
79 | * @property integer $scale |
||
80 | */ |
||
81 | private $scale = null; |
||
82 | |||
83 | /** |
||
84 | * @property boolean $unicode |
||
85 | */ |
||
86 | private $unicode = null; |
||
87 | |||
88 | /** |
||
89 | * @property string $collation |
||
90 | */ |
||
91 | private $collation = null; |
||
92 | |||
93 | /** |
||
94 | * @property string $sRID |
||
95 | */ |
||
96 | private $sRID = null; |
||
97 | |||
98 | /** |
||
99 | * @property string $concurrencyMode |
||
100 | */ |
||
101 | private $concurrencyMode = null; |
||
102 | |||
103 | /** |
||
104 | * @property string $setterAccess |
||
105 | */ |
||
106 | private $setterAccess = null; |
||
107 | |||
108 | /** |
||
109 | * @property string $getterAccess |
||
110 | */ |
||
111 | private $getterAccess = null; |
||
112 | |||
113 | /** |
||
114 | * Gets as name |
||
115 | * |
||
116 | * @return string |
||
117 | */ |
||
118 | public function getName() |
||
122 | |||
123 | /** |
||
124 | * Sets a new name |
||
125 | * |
||
126 | * @param string $name |
||
127 | * @return self |
||
128 | */ |
||
129 | public function setName($name) |
||
130 | { |
||
131 | $msg = null; |
||
|
|||
132 | if (!$this->isTSimpleIdentifierValid($name)) { |
||
133 | $msg = "Name must be a valid TSimpleIdentifier"; |
||
134 | throw new \InvalidArgumentException($msg); |
||
135 | } |
||
136 | $this->name = $name; |
||
137 | return $this; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Gets as type |
||
142 | * |
||
143 | * @return string |
||
144 | */ |
||
145 | public function getType() |
||
149 | |||
150 | /** |
||
151 | * Sets a new type |
||
152 | * |
||
153 | * @param string $type |
||
154 | * @return self |
||
155 | */ |
||
156 | public function setType($type) |
||
166 | |||
167 | /** |
||
168 | * Gets as nullable |
||
169 | * |
||
170 | * @return boolean |
||
171 | */ |
||
172 | public function getNullable() |
||
176 | |||
177 | /** |
||
178 | * Sets a new nullable |
||
179 | * |
||
180 | * @param boolean $nullable |
||
181 | * @return self |
||
182 | */ |
||
183 | public function setNullable($nullable) |
||
188 | |||
189 | /** |
||
190 | * Gets as defaultValue |
||
191 | * |
||
192 | * @return string |
||
193 | */ |
||
194 | public function getDefaultValue() |
||
198 | |||
199 | /** |
||
200 | * Sets a new defaultValue |
||
201 | * |
||
202 | * @param string $defaultValue |
||
203 | * @return self |
||
204 | */ |
||
205 | public function setDefaultValue($defaultValue) |
||
218 | |||
219 | /** |
||
220 | * Gets as maxLength |
||
221 | * |
||
222 | * @return string |
||
223 | */ |
||
224 | public function getMaxLength() |
||
228 | |||
229 | /** |
||
230 | * Sets a new maxLength |
||
231 | * |
||
232 | * @param string $maxLength |
||
233 | * @return self |
||
234 | */ |
||
235 | View Code Duplication | public function setMaxLength($maxLength) |
|
245 | |||
246 | /** |
||
247 | * Gets as fixedLength |
||
248 | * |
||
249 | * @return boolean |
||
250 | */ |
||
251 | public function getFixedLength() |
||
255 | |||
256 | /** |
||
257 | * Sets a new fixedLength |
||
258 | * |
||
259 | * @param boolean $fixedLength |
||
260 | * @return self |
||
261 | */ |
||
262 | public function setFixedLength($fixedLength) |
||
267 | |||
268 | /** |
||
269 | * Gets as precision |
||
270 | * |
||
271 | * @return integer |
||
272 | */ |
||
273 | public function getPrecision() |
||
277 | |||
278 | /** |
||
279 | * Sets a new precision |
||
280 | * |
||
281 | * @param integer $precision |
||
282 | * @return self |
||
283 | */ |
||
284 | View Code Duplication | public function setPrecision($precision) |
|
294 | |||
295 | /** |
||
296 | * Gets as scale |
||
297 | * |
||
298 | * @return integer |
||
299 | */ |
||
300 | public function getScale() |
||
304 | |||
305 | /** |
||
306 | * Sets a new scale |
||
307 | * |
||
308 | * @param integer $scale |
||
309 | * @return self |
||
310 | */ |
||
311 | View Code Duplication | public function setScale($scale) |
|
321 | |||
322 | /** |
||
323 | * Gets as unicode |
||
324 | * |
||
325 | * @return boolean |
||
326 | */ |
||
327 | public function getUnicode() |
||
331 | |||
332 | /** |
||
333 | * Sets a new unicode |
||
334 | * |
||
335 | * @param boolean $unicode |
||
336 | * @return self |
||
337 | */ |
||
338 | View Code Duplication | public function setUnicode($unicode) |
|
348 | |||
349 | /** |
||
350 | * Gets as collation |
||
351 | * |
||
352 | * @return string |
||
353 | */ |
||
354 | public function getCollation() |
||
358 | |||
359 | /** |
||
360 | * Sets a new collation |
||
361 | * |
||
362 | * @param string $collation |
||
363 | * @return self |
||
364 | */ |
||
365 | public function setCollation($collation) |
||
370 | |||
371 | /** |
||
372 | * Gets as sRID |
||
373 | * |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getSRID() |
||
380 | |||
381 | /** |
||
382 | * Sets a new sRID |
||
383 | * |
||
384 | * @param string $sRID |
||
385 | * @return self |
||
386 | */ |
||
387 | View Code Duplication | public function setSRID($sRID) |
|
397 | |||
398 | /** |
||
399 | * Gets as concurrencyMode |
||
400 | * |
||
401 | * @return string |
||
402 | */ |
||
403 | public function getConcurrencyMode() |
||
407 | |||
408 | /** |
||
409 | * Sets a new concurrencyMode |
||
410 | * |
||
411 | * @param string $concurrencyMode |
||
412 | * @return self |
||
413 | */ |
||
414 | public function setConcurrencyMode($concurrencyMode) |
||
424 | |||
425 | /** |
||
426 | * Gets as setterAccess |
||
427 | * |
||
428 | * @return string |
||
429 | */ |
||
430 | public function getSetterAccess() |
||
434 | |||
435 | /** |
||
436 | * Sets a new setterAccess |
||
437 | * |
||
438 | * @param string $setterAccess |
||
439 | * @return self |
||
440 | */ |
||
441 | View Code Duplication | public function setSetterAccess($setterAccess) |
|
451 | |||
452 | /** |
||
453 | * Gets as getterAccess |
||
454 | * |
||
455 | * @return string |
||
456 | */ |
||
457 | public function getGetterAccess() |
||
461 | |||
462 | /** |
||
463 | * Sets a new getterAccess |
||
464 | * |
||
465 | * @param string $getterAccess |
||
466 | * @return self |
||
467 | */ |
||
468 | View Code Duplication | public function setGetterAccess($getterAccess) |
|
478 | |||
479 | public function isTCommonPropertyAttributesValid(&$msg = null) |
||
540 | } |
||
541 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVar
assignment in line 1 and the$higher
assignment in line 2 are dead. The first because$myVar
is never used and the second because$higher
is always overwritten for every possible time line.