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 |
||
| 20 | class Version |
||
| 21 | { |
||
| 22 | |||
| 23 | const RELEASE_TYPE_RC = 'rc'; |
||
| 24 | |||
| 25 | const RELEASE_TYPE_BETA = 'beta'; |
||
| 26 | |||
| 27 | const RELEASE_TYPE_DECAF = 'decaf'; |
||
| 28 | |||
| 29 | const RELEASE_TYPE_PROD = 'p'; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * @var int $major |
||
| 33 | */ |
||
| 34 | private $major; |
||
| 35 | |||
| 36 | /** |
||
| 37 | * @var int $minor |
||
| 38 | */ |
||
| 39 | private $minor; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * @var int $patch |
||
| 43 | */ |
||
| 44 | private $patch; |
||
| 45 | |||
| 46 | /** |
||
| 47 | * @var string $release |
||
| 48 | */ |
||
| 49 | private $release; |
||
| 50 | |||
| 51 | /** |
||
| 52 | * @var int $build |
||
| 53 | */ |
||
| 54 | private $build; |
||
| 55 | |||
| 56 | |||
| 57 | /** |
||
| 58 | * Version constructor. |
||
| 59 | * |
||
| 60 | * @param int $major |
||
| 61 | * @param int $minor |
||
| 62 | * @param int $patch |
||
| 63 | * @param string $release |
||
| 64 | * @param int $build |
||
| 65 | * @throws InvalidDataTypeException |
||
| 66 | * @throws InvalidArgumentException |
||
| 67 | */ |
||
| 68 | public function __construct($major, $minor, $patch, $release = Version::RELEASE_TYPE_PROD, $build = 0) |
||
| 76 | |||
| 77 | |||
| 78 | /** |
||
| 79 | * @param string $version_string |
||
| 80 | * @return Version |
||
| 81 | * @throws InvalidArgumentException |
||
| 82 | */ |
||
| 83 | public static function fromString($version_string) |
||
| 120 | |||
| 121 | |||
| 122 | /** |
||
| 123 | * @return int |
||
| 124 | */ |
||
| 125 | public function major() |
||
| 129 | |||
| 130 | |||
| 131 | /** |
||
| 132 | * @param int|string $major |
||
| 133 | * @throws InvalidDataTypeException |
||
| 134 | */ |
||
| 135 | View Code Duplication | private function setMajor($major) |
|
| 146 | |||
| 147 | |||
| 148 | /** |
||
| 149 | * @return int |
||
| 150 | */ |
||
| 151 | public function minor() |
||
| 155 | |||
| 156 | |||
| 157 | /** |
||
| 158 | * @param int|string $minor |
||
| 159 | * @throws InvalidDataTypeException |
||
| 160 | */ |
||
| 161 | View Code Duplication | private function setMinor($minor) |
|
| 172 | |||
| 173 | |||
| 174 | /** |
||
| 175 | * @return int |
||
| 176 | */ |
||
| 177 | public function patch() |
||
| 181 | |||
| 182 | |||
| 183 | /** |
||
| 184 | * @param int|string $patch |
||
| 185 | * @throws InvalidDataTypeException |
||
| 186 | */ |
||
| 187 | View Code Duplication | private function setPatch($patch) |
|
| 198 | |||
| 199 | |||
| 200 | /** |
||
| 201 | * @return string |
||
| 202 | */ |
||
| 203 | public function release() |
||
| 207 | |||
| 208 | |||
| 209 | /** |
||
| 210 | * @param string $release |
||
| 211 | * @throws InvalidArgumentException |
||
| 212 | */ |
||
| 213 | private function setRelease($release) |
||
| 235 | |||
| 236 | |||
| 237 | /** |
||
| 238 | * @return int |
||
| 239 | */ |
||
| 240 | public function build() |
||
| 244 | |||
| 245 | |||
| 246 | /** |
||
| 247 | * @param int|string $build |
||
| 248 | * @throws InvalidDataTypeException |
||
| 249 | */ |
||
| 250 | View Code Duplication | private function setBuild($build) |
|
| 261 | |||
| 262 | |||
| 263 | /** |
||
| 264 | * @param Version $other_version |
||
| 265 | * @return int |
||
| 266 | */ |
||
| 267 | public function compare(Version $other_version) |
||
| 271 | |||
| 272 | |||
| 273 | /** |
||
| 274 | * @param Version $other_version |
||
| 275 | * @return bool |
||
| 276 | */ |
||
| 277 | public function equals(Version $other_version) |
||
| 281 | |||
| 282 | |||
| 283 | /** |
||
| 284 | * @param Version $other_version |
||
| 285 | * @return bool |
||
| 286 | */ |
||
| 287 | public function newerThan(Version $other_version) |
||
| 291 | |||
| 292 | |||
| 293 | /** |
||
| 294 | * @param Version $other_version |
||
| 295 | * @return bool |
||
| 296 | */ |
||
| 297 | public function olderThan(Version $other_version) |
||
| 301 | |||
| 302 | |||
| 303 | /** |
||
| 304 | * @return string |
||
| 305 | */ |
||
| 306 | public function __toString() |
||
| 314 | |||
| 315 | |||
| 316 | |||
| 317 | } |
||
| 318 | // Location: Version.php |
||
| 319 |