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 |
||
| 18 | class Update |
||
| 19 | { |
||
| 20 | /** |
||
| 21 | * The temporary file path. |
||
| 22 | * |
||
| 23 | * @var string |
||
| 24 | */ |
||
| 25 | private $file; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * The name of the update file. |
||
| 29 | * |
||
| 30 | * @var string |
||
| 31 | */ |
||
| 32 | private $name; |
||
| 33 | |||
| 34 | /** |
||
| 35 | * The URL where the public key can be downloaded from. |
||
| 36 | * |
||
| 37 | * @var string |
||
| 38 | */ |
||
| 39 | private $publicKey; |
||
| 40 | |||
| 41 | /** |
||
| 42 | * The SHA1 file checksum. |
||
| 43 | * |
||
| 44 | * @var string |
||
| 45 | */ |
||
| 46 | private $sha1; |
||
| 47 | |||
| 48 | /** |
||
| 49 | * The URL where the update can be downloaded from. |
||
| 50 | * |
||
| 51 | * @var string |
||
| 52 | */ |
||
| 53 | private $url; |
||
| 54 | |||
| 55 | /** |
||
| 56 | * The version of the update. |
||
| 57 | * |
||
| 58 | * @var Version |
||
| 59 | */ |
||
| 60 | private $version; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * Sets the update information. |
||
| 64 | * |
||
| 65 | * @param string $name The name of the update file. |
||
| 66 | * @param string $sha1 The SHA1 file checksum. |
||
| 67 | * @param string $url The URL where the update can be downloaded from. |
||
| 68 | * @param Version $version The version of the update. |
||
| 69 | * @param string $key The URL where the public key can be downloaded |
||
| 70 | * from. |
||
| 71 | */ |
||
| 72 | public function __construct( |
||
| 85 | |||
| 86 | /** |
||
| 87 | * Copies the update file to the destination. |
||
| 88 | * |
||
| 89 | * @param string $file The target file. |
||
| 90 | * |
||
| 91 | * @throws Exception\Exception |
||
| 92 | * @throws FileException If the file could not be replaced. |
||
| 93 | */ |
||
| 94 | public function copyTo($file) |
||
| 128 | |||
| 129 | /** |
||
| 130 | * Cleans up by deleting the temporary update file. |
||
| 131 | * |
||
| 132 | * @throws FileException If the file could not be deleted. |
||
| 133 | */ |
||
| 134 | public function deleteFile() |
||
| 160 | |||
| 161 | /** |
||
| 162 | * Downloads the update file to a temporary location. |
||
| 163 | * |
||
| 164 | * @return string The temporary file path. |
||
| 165 | * |
||
| 166 | * @throws Exception\Exception |
||
| 167 | * @throws FileException If the SHA1 checksum differs. |
||
| 168 | * @throws UnexpectedValueException If the Phar is corrupt. |
||
| 169 | */ |
||
| 170 | public function getFile() |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Returns name of the update file. |
||
| 223 | * |
||
| 224 | * @return string The name. |
||
| 225 | */ |
||
| 226 | public function getName() |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Returns the URL where the public key can be downloaded from. |
||
| 233 | * |
||
| 234 | * @return string The URL. |
||
| 235 | */ |
||
| 236 | public function getPublicKey() |
||
| 240 | |||
| 241 | /** |
||
| 242 | * Returns the SHA1 file checksum. |
||
| 243 | * |
||
| 244 | * @return string The checksum. |
||
| 245 | */ |
||
| 246 | public function getSha1() |
||
| 250 | |||
| 251 | /** |
||
| 252 | * Returns the URL where the update can be downloaded from. |
||
| 253 | * |
||
| 254 | * @return string The URL. |
||
| 255 | */ |
||
| 256 | public function getUrl() |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Returns the version of the update. |
||
| 263 | * |
||
| 264 | * @return Version The version. |
||
| 265 | */ |
||
| 266 | public function getVersion() |
||
| 270 | |||
| 271 | /** |
||
| 272 | * Checks if this update is newer than the version given. |
||
| 273 | * |
||
| 274 | * @param Version $version The current version. |
||
| 275 | * |
||
| 276 | * @return boolean TRUE if the update is newer, FALSE if not. |
||
| 277 | */ |
||
| 278 | public function isNewer(Version $version) |
||
| 282 | } |
||
| 283 |