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) |
||
| 206 | { |
||
| 207 | if (is_numeric($defaultValue)) { |
||
| 208 | $defaultValue = strval($defaultValue); |
||
| 209 | } |
||
| 210 | if (null !== $defaultValue && !is_string($defaultValue)) { |
||
| 211 | $msg = "Default value must be resolvable to a string"; |
||
| 212 | throw new \InvalidArgumentException($msg); |
||
| 213 | } |
||
| 214 | $this->defaultValue = $defaultValue; |
||
| 215 | return $this; |
||
| 216 | } |
||
| 217 | |||
| 218 | /** |
||
| 219 | * Gets as maxLength |
||
| 220 | * |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getMaxLength() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * Sets a new maxLength |
||
| 230 | * |
||
| 231 | * @param string $maxLength |
||
| 232 | * @return self |
||
| 233 | */ |
||
| 234 | View Code Duplication | public function setMaxLength($maxLength) |
|
| 244 | |||
| 245 | /** |
||
| 246 | * Gets as fixedLength |
||
| 247 | * |
||
| 248 | * @return boolean |
||
| 249 | */ |
||
| 250 | public function getFixedLength() |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Sets a new fixedLength |
||
| 257 | * |
||
| 258 | * @param boolean $fixedLength |
||
| 259 | * @return self |
||
| 260 | */ |
||
| 261 | public function setFixedLength($fixedLength) |
||
| 266 | |||
| 267 | /** |
||
| 268 | * Gets as precision |
||
| 269 | * |
||
| 270 | * @return integer |
||
| 271 | */ |
||
| 272 | public function getPrecision() |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Sets a new precision |
||
| 279 | * |
||
| 280 | * @param integer $precision |
||
| 281 | * @return self |
||
| 282 | */ |
||
| 283 | View Code Duplication | public function setPrecision($precision) |
|
| 293 | |||
| 294 | /** |
||
| 295 | * Gets as scale |
||
| 296 | * |
||
| 297 | * @return integer |
||
| 298 | */ |
||
| 299 | public function getScale() |
||
| 303 | |||
| 304 | /** |
||
| 305 | * Sets a new scale |
||
| 306 | * |
||
| 307 | * @param integer $scale |
||
| 308 | * @return self |
||
| 309 | */ |
||
| 310 | View Code Duplication | public function setScale($scale) |
|
| 320 | |||
| 321 | /** |
||
| 322 | * Gets as unicode |
||
| 323 | * |
||
| 324 | * @return boolean |
||
| 325 | */ |
||
| 326 | public function getUnicode() |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Sets a new unicode |
||
| 333 | * |
||
| 334 | * @param boolean $unicode |
||
| 335 | * @return self |
||
| 336 | */ |
||
| 337 | View Code Duplication | public function setUnicode($unicode) |
|
| 347 | |||
| 348 | /** |
||
| 349 | * Gets as collation |
||
| 350 | * |
||
| 351 | * @return string |
||
| 352 | */ |
||
| 353 | public function getCollation() |
||
| 357 | |||
| 358 | /** |
||
| 359 | * Sets a new collation |
||
| 360 | * |
||
| 361 | * @param string $collation |
||
| 362 | * @return self |
||
| 363 | */ |
||
| 364 | public function setCollation($collation) |
||
| 369 | |||
| 370 | /** |
||
| 371 | * Gets as sRID |
||
| 372 | * |
||
| 373 | * @return string |
||
| 374 | */ |
||
| 375 | public function getSRID() |
||
| 379 | |||
| 380 | /** |
||
| 381 | * Sets a new sRID |
||
| 382 | * |
||
| 383 | * @param string $sRID |
||
| 384 | * @return self |
||
| 385 | */ |
||
| 386 | View Code Duplication | public function setSRID($sRID) |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Gets as concurrencyMode |
||
| 399 | * |
||
| 400 | * @return string |
||
| 401 | */ |
||
| 402 | public function getConcurrencyMode() |
||
| 406 | |||
| 407 | /** |
||
| 408 | * Sets a new concurrencyMode |
||
| 409 | * |
||
| 410 | * @param string $concurrencyMode |
||
| 411 | * @return self |
||
| 412 | */ |
||
| 413 | public function setConcurrencyMode($concurrencyMode) |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Gets as setterAccess |
||
| 426 | * |
||
| 427 | * @return string |
||
| 428 | */ |
||
| 429 | public function getSetterAccess() |
||
| 433 | |||
| 434 | /** |
||
| 435 | * Sets a new setterAccess |
||
| 436 | * |
||
| 437 | * @param string $setterAccess |
||
| 438 | * @return self |
||
| 439 | */ |
||
| 440 | View Code Duplication | public function setSetterAccess($setterAccess) |
|
| 450 | |||
| 451 | /** |
||
| 452 | * Gets as getterAccess |
||
| 453 | * |
||
| 454 | * @return string |
||
| 455 | */ |
||
| 456 | public function getGetterAccess() |
||
| 460 | |||
| 461 | /** |
||
| 462 | * Sets a new getterAccess |
||
| 463 | * |
||
| 464 | * @param string $getterAccess |
||
| 465 | * @return self |
||
| 466 | */ |
||
| 467 | View Code Duplication | public function setGetterAccess($getterAccess) |
|
| 477 | |||
| 478 | public function isTCommonPropertyAttributesValid(&$msg = null) |
||
| 539 | } |
||
| 540 |
This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.
Both the
$myVarassignment in line 1 and the$higherassignment in line 2 are dead. The first because$myVaris never used and the second because$higheris always overwritten for every possible time line.