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 |
||
| 17 | View Code Duplication | class OpenClosePeriod |
|
|
|
|||
| 18 | { |
||
| 19 | /** |
||
| 20 | * @var int|null |
||
| 21 | */ |
||
| 22 | private $day; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * @var \DateTime|null |
||
| 26 | */ |
||
| 27 | private $time; |
||
| 28 | |||
| 29 | /** |
||
| 30 | * @return bool |
||
| 31 | */ |
||
| 32 | public function hasDay() |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @return int|null |
||
| 39 | */ |
||
| 40 | public function getDay() |
||
| 44 | |||
| 45 | /** |
||
| 46 | * @param int|null $day |
||
| 47 | */ |
||
| 48 | public function setDay($day) |
||
| 52 | |||
| 53 | /** |
||
| 54 | * @return bool |
||
| 55 | */ |
||
| 56 | public function hasTime() |
||
| 60 | |||
| 61 | /** |
||
| 62 | * @return \DateTime|null |
||
| 63 | */ |
||
| 64 | public function getTime() |
||
| 68 | |||
| 69 | /** |
||
| 70 | * @param \DateTime|null $time |
||
| 71 | */ |
||
| 72 | public function setTime(\DateTime $time = null) |
||
| 76 | } |
||
| 77 |
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.