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 |
||
5 | View Code Duplication | class SpecialOfferByDay |
|
|
|||
6 | { |
||
7 | |||
8 | /** |
||
9 | * @var \DateTime $TheDate |
||
10 | */ |
||
11 | protected $TheDate = null; |
||
12 | |||
13 | /** |
||
14 | * @var int $ID_Code |
||
15 | */ |
||
16 | protected $ID_Code = null; |
||
17 | |||
18 | /** |
||
19 | * @var float $Amount |
||
20 | */ |
||
21 | protected $Amount = null; |
||
22 | |||
23 | /** |
||
24 | * @param \DateTime $TheDate |
||
25 | * @param int $ID_Code |
||
26 | * @param float $Amount |
||
27 | */ |
||
28 | public function __construct(\DateTime $TheDate, $ID_Code, $Amount) |
||
34 | |||
35 | /** |
||
36 | * @return \DateTime |
||
37 | */ |
||
38 | public function getTheDate() |
||
50 | |||
51 | /** |
||
52 | * @param \DateTime $TheDate |
||
53 | * @return \Gueststream\PMS\IQWare\API\SpecialOfferByDay |
||
54 | */ |
||
55 | public function setTheDate(\DateTime $TheDate) |
||
60 | |||
61 | /** |
||
62 | * @return int |
||
63 | */ |
||
64 | public function getID_Code() |
||
68 | |||
69 | /** |
||
70 | * @param int $ID_Code |
||
71 | * @return \Gueststream\PMS\IQWare\API\SpecialOfferByDay |
||
72 | */ |
||
73 | public function setID_Code($ID_Code) |
||
78 | |||
79 | /** |
||
80 | * @return float |
||
81 | */ |
||
82 | public function getAmount() |
||
86 | |||
87 | /** |
||
88 | * @param float $Amount |
||
89 | * @return \Gueststream\PMS\IQWare\API\SpecialOfferByDay |
||
90 | */ |
||
91 | public function setAmount($Amount) |
||
96 | } |
||
97 |
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.