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:
| 1 | <?php |
||
| 13 | trait TFunctionImportParameterAttributesTrait |
||
| 14 | { |
||
| 15 | use TSimpleIdentifierTrait, TParameterModeTrait, TFunctionImportParameterAndReturnTypeTrait, TMaxLengthFacetTrait, |
||
| 16 | TPrecisionFacetTrait, TScaleFacetTrait, TSridFacetTrait; |
||
| 17 | |||
| 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 string $mode |
||
| 30 | */ |
||
| 31 | private $mode = null; |
||
| 32 | |||
| 33 | /** |
||
| 34 | * @property string $maxLength |
||
| 35 | */ |
||
| 36 | private $maxLength = null; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * @property integer $precision |
||
| 40 | */ |
||
| 41 | private $precision = null; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * @property integer $scale |
||
| 45 | */ |
||
| 46 | private $scale = null; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * @property string $sRID |
||
| 50 | */ |
||
| 51 | private $sRID = null; |
||
| 52 | |||
| 53 | /** |
||
| 54 | * Gets as name |
||
| 55 | * |
||
| 56 | * @return string |
||
| 57 | */ |
||
| 58 | public function getName() |
||
| 62 | |||
| 63 | /** |
||
| 64 | * Sets a new name |
||
| 65 | * |
||
| 66 | * @param string $name |
||
| 67 | * @return self |
||
| 68 | */ |
||
| 69 | public function setName($name) |
||
| 74 | |||
| 75 | /** |
||
| 76 | * Gets as type |
||
| 77 | * |
||
| 78 | * @return string |
||
| 79 | */ |
||
| 80 | public function getType() |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Sets a new type |
||
| 87 | * |
||
| 88 | * @param string $type |
||
| 89 | * @return self |
||
| 90 | */ |
||
| 91 | public function setType($type) |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Gets as mode |
||
| 99 | * |
||
| 100 | * @return string |
||
| 101 | */ |
||
| 102 | public function getMode() |
||
| 106 | |||
| 107 | /** |
||
| 108 | * Sets a new mode |
||
| 109 | * |
||
| 110 | * @param string $mode |
||
| 111 | * @return self |
||
| 112 | */ |
||
| 113 | public function setMode($mode) |
||
| 118 | |||
| 119 | /** |
||
| 120 | * Gets as maxLength |
||
| 121 | * |
||
| 122 | * @return string |
||
| 123 | */ |
||
| 124 | public function getMaxLength() |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Sets a new maxLength |
||
| 131 | * |
||
| 132 | * @param string $maxLength |
||
| 133 | * @return self |
||
| 134 | */ |
||
| 135 | public function setMaxLength($maxLength) |
||
| 140 | |||
| 141 | /** |
||
| 142 | * Gets as precision |
||
| 143 | * |
||
| 144 | * @return integer |
||
| 145 | */ |
||
| 146 | public function getPrecision() |
||
| 150 | |||
| 151 | /** |
||
| 152 | * Sets a new precision |
||
| 153 | * |
||
| 154 | * @param integer $precision |
||
| 155 | * @return self |
||
| 156 | */ |
||
| 157 | public function setPrecision($precision) |
||
| 162 | |||
| 163 | /** |
||
| 164 | * Gets as scale |
||
| 165 | * |
||
| 166 | * @return integer |
||
| 167 | */ |
||
| 168 | public function getScale() |
||
| 172 | |||
| 173 | /** |
||
| 174 | * Sets a new scale |
||
| 175 | * |
||
| 176 | * @param integer $scale |
||
| 177 | * @return self |
||
| 178 | */ |
||
| 179 | public function setScale($scale) |
||
| 184 | |||
| 185 | /** |
||
| 186 | * Gets as sRID |
||
| 187 | * |
||
| 188 | * @return string |
||
| 189 | */ |
||
| 190 | public function getSRID() |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Sets a new sRID |
||
| 197 | * |
||
| 198 | * @param string $sRID |
||
| 199 | * @return self |
||
| 200 | */ |
||
| 201 | public function setSRID($sRID) |
||
| 206 | |||
| 207 | public function isTFunctionImportParameterAttributesValid(&$msg = null) |
||
| 240 | } |
||
| 241 |
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idableprovides a methodequalsIdthat in turn relies on the methodgetId(). If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()as an abstract method to the trait will make sure it is available.