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 |
||
26 | class Charge implements ChargeInterface |
||
27 | { |
||
28 | /** |
||
29 | * @var int |
||
30 | */ |
||
31 | protected $id; |
||
32 | |||
33 | /** |
||
34 | * @var ActionInterface |
||
35 | */ |
||
36 | protected $action; |
||
37 | |||
38 | /** |
||
39 | * @var PriceInterface |
||
40 | */ |
||
41 | protected $price; |
||
42 | |||
43 | /** |
||
44 | * @var QuantityInterface |
||
45 | */ |
||
46 | protected $usage; |
||
47 | |||
48 | /** |
||
49 | * @var Money |
||
50 | */ |
||
51 | protected $sum; |
||
52 | |||
53 | /** |
||
54 | * @var BillInterface |
||
55 | */ |
||
56 | protected $bill; |
||
57 | |||
58 | /** |
||
59 | * @var string |
||
60 | */ |
||
61 | protected $comment; |
||
62 | |||
63 | 8 | public function __construct( |
|
78 | |||
79 | public function getId() |
||
83 | |||
84 | 6 | public function getAction() |
|
88 | |||
89 | /** |
||
90 | * @return PriceInterface |
||
91 | */ |
||
92 | 6 | public function getPrice() |
|
96 | |||
97 | 8 | public function getUsage() |
|
101 | |||
102 | 8 | public function getSum() |
|
106 | |||
107 | public function calculatePrice() |
||
113 | |||
114 | public function getBill() |
||
118 | |||
119 | public function hasBill() |
||
123 | |||
124 | public function setBill(BillInterface $bill) |
||
131 | |||
132 | public function getComment() |
||
136 | |||
137 | public function setComment(string $comment) |
||
141 | |||
142 | View Code Duplication | public function setId($id) |
|
152 | |||
153 | public function jsonSerialize() |
||
157 | } |
||
158 |
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.