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 |
||
29 | abstract class AbstractAction implements ActionInterface |
||
30 | { |
||
31 | /** |
||
32 | * @var int |
||
33 | */ |
||
34 | protected $id; |
||
35 | |||
36 | /** |
||
37 | * @var TypeInterface |
||
38 | */ |
||
39 | protected $type; |
||
40 | |||
41 | /** |
||
42 | * @var TargetInterface |
||
43 | */ |
||
44 | protected $target; |
||
45 | |||
46 | /** |
||
47 | * @var QuantityInterface |
||
48 | */ |
||
49 | protected $quantity; |
||
50 | |||
51 | /** |
||
52 | * @var CustomerInterface |
||
53 | */ |
||
54 | protected $customer; |
||
55 | |||
56 | /** |
||
57 | * @var SaleInterface |
||
58 | */ |
||
59 | protected $sale; |
||
60 | |||
61 | /** |
||
62 | * @var DateTime |
||
63 | */ |
||
64 | protected $time; |
||
65 | |||
66 | /** |
||
67 | * @param TypeInterface $type |
||
68 | * @param TargetInterface $target |
||
69 | * @param QuantityInterface $quantity |
||
70 | * @param CustomerInterface $customer |
||
71 | * @param SaleInterface $sale |
||
72 | * @param DateTime $time |
||
73 | */ |
||
74 | 4 | View Code Duplication | public function __construct( |
91 | |||
92 | /** |
||
93 | * {@inheritdoc} |
||
94 | */ |
||
95 | public function getId() |
||
99 | |||
100 | /** |
||
101 | * {@inheritdoc} |
||
102 | */ |
||
103 | public function getCustomer() |
||
107 | |||
108 | /** |
||
109 | * {@inheritdoc} |
||
110 | */ |
||
111 | 3 | public function getTarget() |
|
115 | |||
116 | /** |
||
117 | * {@inheritdoc} |
||
118 | */ |
||
119 | 2 | public function getType() |
|
123 | |||
124 | /** |
||
125 | * {@inheritdoc} |
||
126 | */ |
||
127 | 4 | public function getQuantity() |
|
131 | |||
132 | /** |
||
133 | * {@inheritdoc} |
||
134 | */ |
||
135 | public function getSale() |
||
139 | |||
140 | /** |
||
141 | * {@inheritdoc} |
||
142 | */ |
||
143 | public function getTime() |
||
147 | |||
148 | View Code Duplication | public function setId($id) |
|
158 | |||
159 | public function setSale(SaleInterface $sale) |
||
166 | |||
167 | /** |
||
168 | * {@inheritdoc} |
||
169 | */ |
||
170 | public function jsonSerialize() |
||
174 | |||
175 | /** |
||
176 | * {@inheritdoc} |
||
177 | */ |
||
178 | 4 | public function calculateCharge(PriceInterface $price) |
|
196 | |||
197 | /** |
||
198 | * {@inheritdoc} |
||
199 | */ |
||
200 | abstract public function isApplicable(PriceInterface $price); |
||
201 | } |
||
202 |
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.