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 |
||
16 | class CustomsInfo |
||
17 | { |
||
18 | |||
19 | const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTA = 'RTA'; |
||
20 | const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_RTS = 'RTS'; |
||
21 | const CUSTOM_INFO_PARCEL_RETURN_INSTRUCTION_ABANDONED = 'ABANDONED'; |
||
22 | |||
23 | const CUSTOM_INFO_SHIPMENT_TYPE_SAMPLE = 'SAMPLE'; |
||
24 | const CUSTOM_INFO_SHIPMENT_TYPE_GIFT = 'GIFT'; |
||
25 | const CUSTOM_INFO_SHIPMENT_TYPE_GOODS = 'GOODS'; |
||
26 | const CUSTOM_INFO_SHIPMENT_TYPE_DOCUMENTS = 'DOCUMENTS'; |
||
27 | const CUSTOM_INFO_SHIPMENT_TYPE_OTHER = 'OTHER'; |
||
28 | |||
29 | /** |
||
30 | * @var int |
||
31 | */ |
||
32 | private $parcelValue; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $contentDescription; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $shipmentType; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $parcelReturnInstructions; |
||
48 | |||
49 | /** |
||
50 | * @var bool |
||
51 | */ |
||
52 | private $privateAddress; |
||
53 | |||
54 | /** |
||
55 | * @param string $contentDescription |
||
56 | * @throws BpostInvalidLengthException |
||
57 | 4 | */ |
|
58 | View Code Duplication | public function setContentDescription($contentDescription) |
|
67 | |||
68 | /** |
||
69 | * @return string |
||
70 | 3 | */ |
|
71 | public function getContentDescription() |
||
75 | |||
76 | /** |
||
77 | * @param string $parcelReturnInstructions |
||
78 | * @throws BpostInvalidValueException |
||
79 | 3 | */ |
|
80 | View Code Duplication | public function setParcelReturnInstructions($parcelReturnInstructions) |
|
94 | |||
95 | /** |
||
96 | * @return string |
||
97 | 3 | */ |
|
98 | public function getParcelReturnInstructions() |
||
102 | |||
103 | /** |
||
104 | * @return array |
||
105 | 3 | */ |
|
106 | public static function getPossibleParcelReturnInstructionValues() |
||
114 | |||
115 | /** |
||
116 | * @param int $parcelValue |
||
117 | 3 | */ |
|
118 | public function setParcelValue($parcelValue) |
||
122 | |||
123 | /** |
||
124 | * @return int |
||
125 | 3 | */ |
|
126 | public function getParcelValue() |
||
130 | |||
131 | /** |
||
132 | * @param boolean $privateAddress |
||
133 | 3 | */ |
|
134 | public function setPrivateAddress($privateAddress) |
||
138 | |||
139 | /** |
||
140 | * @return boolean |
||
141 | 3 | */ |
|
142 | public function getPrivateAddress() |
||
146 | |||
147 | /** |
||
148 | * @param string $shipmentType |
||
149 | * @throws BpostInvalidValueException |
||
150 | 4 | */ |
|
151 | View Code Duplication | public function setShipmentType($shipmentType) |
|
161 | |||
162 | /** |
||
163 | * @return string |
||
164 | 3 | */ |
|
165 | public function getShipmentType() |
||
169 | |||
170 | /** |
||
171 | * @return array |
||
172 | 4 | */ |
|
173 | public static function getPossibleShipmentTypeValues() |
||
183 | |||
184 | /** |
||
185 | * Return the object as an array for usage in the XML |
||
186 | * |
||
187 | * @param \DomDocument $document |
||
188 | * @param string $prefix |
||
189 | * @return \DomElement |
||
190 | 2 | */ |
|
191 | public function toXML(\DOMDocument $document, $prefix = null) |
||
268 | |||
269 | /** |
||
270 | * @param \SimpleXMLElement $xml |
||
271 | * |
||
272 | * @return CustomsInfo |
||
273 | * @throws BpostInvalidLengthException |
||
274 | * @throws BpostInvalidValueException |
||
275 | 1 | */ |
|
276 | public static function createFromXML(\SimpleXMLElement $xml) |
||
308 | } |
||
309 |
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.