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 |
||
| 4 | class Version |
||
| 5 | { |
||
| 6 | /** |
||
| 7 | * |
||
| 8 | * @var integer |
||
| 9 | */ |
||
| 10 | private $major; |
||
| 11 | |||
| 12 | /** |
||
| 13 | * |
||
| 14 | * @var integer |
||
| 15 | */ |
||
| 16 | private $minor; |
||
| 17 | |||
| 18 | /** |
||
| 19 | * |
||
| 20 | * @var integer |
||
| 21 | */ |
||
| 22 | private $patch; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * |
||
| 26 | * @var string |
||
| 27 | */ |
||
| 28 | private $alias; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * |
||
| 32 | * @var string |
||
| 33 | */ |
||
| 34 | private $complete; |
||
| 35 | |||
| 36 | private static $notAllowedAlias = [ |
||
| 37 | 'a', |
||
| 38 | 'alpha', |
||
| 39 | 'prealpha', |
||
| 40 | |||
| 41 | 'b', |
||
| 42 | 'beta', |
||
| 43 | 'prebeta', |
||
| 44 | |||
| 45 | 'rc', |
||
| 46 | ]; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * |
||
| 50 | * @param integer $major |
||
| 51 | */ |
||
| 52 | 8 | public function setMajor($major) |
|
| 62 | |||
| 63 | /** |
||
| 64 | * |
||
| 65 | * @return integer |
||
| 66 | */ |
||
| 67 | 8 | public function getMajor() |
|
| 71 | |||
| 72 | /** |
||
| 73 | * |
||
| 74 | * @param integer $minor |
||
| 75 | */ |
||
| 76 | 8 | public function setMinor($minor) |
|
| 86 | |||
| 87 | /** |
||
| 88 | * |
||
| 89 | * @return integer |
||
| 90 | */ |
||
| 91 | 7 | public function getMinor() |
|
| 95 | |||
| 96 | /** |
||
| 97 | * |
||
| 98 | * @param integer $patch |
||
| 99 | */ |
||
| 100 | 8 | public function setPatch($patch) |
|
| 110 | |||
| 111 | /** |
||
| 112 | * |
||
| 113 | * @return integer |
||
| 114 | */ |
||
| 115 | 7 | public function getPatch() |
|
| 119 | |||
| 120 | /** |
||
| 121 | * |
||
| 122 | * @param string $alias |
||
| 123 | */ |
||
| 124 | 8 | public function setAlias($alias) |
|
| 130 | |||
| 131 | /** |
||
| 132 | * |
||
| 133 | * @return string |
||
| 134 | */ |
||
| 135 | 8 | public function getAlias() |
|
| 139 | |||
| 140 | /** |
||
| 141 | * Set from the complete version string. |
||
| 142 | * |
||
| 143 | * @param string $complete |
||
| 144 | */ |
||
| 145 | 7 | public function setComplete($complete) |
|
| 158 | |||
| 159 | /** |
||
| 160 | * |
||
| 161 | * @return string |
||
| 162 | */ |
||
| 163 | 7 | public function getComplete() |
|
| 167 | |||
| 168 | /** |
||
| 169 | * |
||
| 170 | * @return array |
||
| 171 | */ |
||
| 172 | 1 | public function toArray() |
|
| 184 | |||
| 185 | /** |
||
| 186 | * |
||
| 187 | * @return string |
||
| 188 | */ |
||
| 189 | 8 | private function hydrateComplete() |
|
| 211 | |||
| 212 | 7 | private function hydrateFromComplete($complete) |
|
| 221 | |||
| 222 | /** |
||
| 223 | * |
||
| 224 | * @return array |
||
| 225 | */ |
||
| 226 | 7 | private function getCompleteParts($complete) |
|
| 271 | } |
||
| 272 |
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.