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 |
||
| 22 | class Request extends DataObject |
||
| 23 | { |
||
| 24 | private $email; |
||
| 25 | private $ip; |
||
| 26 | private $name; |
||
| 27 | /** @var string|null */ |
||
| 28 | private $comment; |
||
| 29 | private $status = "Open"; |
||
| 30 | private $date; |
||
| 31 | private $emailsent = 0; |
||
| 32 | private $emailconfirm; |
||
| 33 | private $reserved = 0; |
||
| 34 | private $useragent; |
||
| 35 | private $forwardedip; |
||
| 36 | private $hasComments = false; |
||
| 37 | private $hasCommentsResolved = false; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * @throws Exception |
||
| 41 | */ |
||
| 42 | public function save() |
||
| 107 | |||
| 108 | /** |
||
| 109 | * @return string |
||
| 110 | */ |
||
| 111 | public function getIp() |
||
| 115 | |||
| 116 | /** |
||
| 117 | * @param string $ip |
||
| 118 | */ |
||
| 119 | 1 | public function setIp($ip) |
|
| 123 | |||
| 124 | /** |
||
| 125 | * @return string |
||
| 126 | */ |
||
| 127 | 1 | public function getName() |
|
| 131 | |||
| 132 | /** |
||
| 133 | * @param string $name |
||
| 134 | */ |
||
| 135 | 1 | public function setName($name) |
|
| 139 | |||
| 140 | /** |
||
| 141 | * @return string|null |
||
| 142 | */ |
||
| 143 | public function getComment() |
||
| 147 | |||
| 148 | /** |
||
| 149 | * @param string $comment |
||
| 150 | */ |
||
| 151 | public function setComment($comment) |
||
| 155 | |||
| 156 | /** |
||
| 157 | * @return string |
||
| 158 | */ |
||
| 159 | public function getStatus() |
||
| 163 | |||
| 164 | /** |
||
| 165 | * @param string $status |
||
| 166 | */ |
||
| 167 | public function setStatus($status) |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Returns the time the request was first submitted |
||
| 174 | * |
||
| 175 | * @return DateTimeImmutable |
||
| 176 | */ |
||
| 177 | public function getDate() |
||
| 181 | |||
| 182 | /** |
||
| 183 | * @return bool |
||
| 184 | */ |
||
| 185 | public function getEmailSent() |
||
| 189 | |||
| 190 | /** |
||
| 191 | * @param bool $emailSent |
||
| 192 | */ |
||
| 193 | public function setEmailSent($emailSent) |
||
| 197 | |||
| 198 | /** |
||
| 199 | * @todo allow this to return null instead |
||
| 200 | * @return int |
||
| 201 | */ |
||
| 202 | public function getReserved() |
||
| 206 | |||
| 207 | /** |
||
| 208 | * @param int|null $reserved |
||
| 209 | */ |
||
| 210 | public function setReserved($reserved) |
||
| 219 | |||
| 220 | /** |
||
| 221 | * @return string |
||
| 222 | */ |
||
| 223 | public function getUserAgent() |
||
| 227 | |||
| 228 | /** |
||
| 229 | * @param string $useragent |
||
| 230 | */ |
||
| 231 | public function setUserAgent($useragent) |
||
| 235 | |||
| 236 | /** |
||
| 237 | * @return string|null |
||
| 238 | */ |
||
| 239 | public function getForwardedIp() |
||
| 243 | |||
| 244 | /** |
||
| 245 | * @param string|null $forwardedip |
||
| 246 | */ |
||
| 247 | public function setForwardedIp($forwardedip) |
||
| 251 | |||
| 252 | /** |
||
| 253 | * @return bool |
||
| 254 | */ |
||
| 255 | public function hasComments() |
||
| 278 | |||
| 279 | /** |
||
| 280 | * @return string |
||
| 281 | */ |
||
| 282 | public function getEmailConfirm() |
||
| 286 | |||
| 287 | /** |
||
| 288 | * @param string $emailconfirm |
||
| 289 | */ |
||
| 290 | public function setEmailConfirm($emailconfirm) |
||
| 294 | |||
| 295 | public function generateEmailConfirmationHash() |
||
| 299 | |||
| 300 | /** |
||
| 301 | * @return string|null |
||
| 302 | */ |
||
| 303 | 1 | public function getEmail() |
|
| 307 | |||
| 308 | /** |
||
| 309 | * @param string|null $email |
||
| 310 | */ |
||
| 311 | 1 | public function setEmail($email) |
|
| 315 | |||
| 316 | /** |
||
| 317 | * @return string |
||
| 318 | * @throws Exception |
||
| 319 | */ |
||
| 320 | public function getClosureReason() |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Gets a value indicating whether the request was closed as created or not. |
||
| 347 | */ |
||
| 348 | public function getWasCreated() |
||
| 378 | |||
| 379 | /** |
||
| 380 | * @return DateTime |
||
| 381 | */ |
||
| 382 | View Code Duplication | public function getClosureDate() |
|
| 397 | } |
||
| 398 |
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.