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 |
||
13 | class CreateObject extends CommonParameter |
||
14 | { |
||
15 | use ReadWriteAttributes { |
||
16 | toArray as public __toArray; |
||
17 | } |
||
18 | |||
19 | const OBJECT_PURPOSE_QUOTE = 'QUOTE'; |
||
20 | const OBJECT_PURPOSE_PURCHASE = 'PURCHASE'; |
||
21 | const SUBMISSION_TYPE_DROPOFF = 'DROPOFF'; |
||
22 | const SUBMISSION_TYPE_PICKUP = 'PICKUP'; |
||
23 | |||
24 | /** |
||
25 | * Quote Shipment can only be used to obtain quote Rates; |
||
26 | * at the same time, they accept any valid or completed Address object. |
||
27 | * Purchase Shipment can be used to obtain Rates and purchase Labels, |
||
28 | * but only accept purchase Address that have been fully entered. |
||
29 | * |
||
30 | * Required |
||
31 | * |
||
32 | * @return string |
||
33 | * @throws InvalidAttributeException |
||
34 | */ |
||
35 | View Code Duplication | public function getObjectPurpose() |
|
43 | |||
44 | /** |
||
45 | * ID of the Address object that should be used as sender Address. |
||
46 | * |
||
47 | * Required |
||
48 | * |
||
49 | * @return string |
||
50 | * @throws InvalidAttributeException |
||
51 | */ |
||
52 | public function getAddressFrom() |
||
56 | |||
57 | /** |
||
58 | * ID of the Address object that should be used as recipient Address. |
||
59 | * |
||
60 | * Required |
||
61 | * |
||
62 | * @return string |
||
63 | * @throws InvalidAttributeException |
||
64 | */ |
||
65 | public function getAddressTo() |
||
69 | |||
70 | /** |
||
71 | * ID of the Parcel object to be shipped. |
||
72 | * |
||
73 | * Required |
||
74 | * |
||
75 | * @return string |
||
76 | * @throws InvalidAttributeException |
||
77 | */ |
||
78 | public function getParcel() |
||
82 | |||
83 | /** |
||
84 | * Indicates whether the parcel will be dropped off at a carrier location or will be picked up. |
||
85 | * Selecting 'PICKUP' does not request a carrier pickup. |
||
86 | * A pickup always needs to be requested separately. |
||
87 | * |
||
88 | * Optional, defaults to DROPOFF |
||
89 | * |
||
90 | * @return string |
||
91 | * @throws InvalidAttributeException |
||
92 | */ |
||
93 | public function getSubmissionType() |
||
106 | |||
107 | /** |
||
108 | * ID of the Transaction object of the outbound shipment. |
||
109 | * This field triggers the creation of a scan-based return shipments. |
||
110 | * See the Create a return shipment section for more details. |
||
111 | * |
||
112 | * Required for return labels |
||
113 | * |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getReturnOf() |
||
120 | |||
121 | /** |
||
122 | * Desired pickup date. Must be in the format "2014-01-18T00:35:03.463Z" (ISO 8601 date). |
||
123 | * Please note that for some carriers, not all pickup dates are available. |
||
124 | * The API will return the corresponding error message if not available. |
||
125 | * If no pickup_date is given, the value will default to tomorrow's date |
||
126 | * (including Saturday or Sunday, which can lead to no carrier availability). |
||
127 | * |
||
128 | * Required if submission_type is 'PICKUP'. Defaults to current date (UTC) for 'DROPOFF' |
||
129 | * |
||
130 | * @return string |
||
131 | * @throws InvalidAttributeException |
||
132 | */ |
||
133 | public function getSubmissionDate() |
||
151 | |||
152 | /** |
||
153 | * ID of the Address object where the shipment will be sent back to if it is not delivered |
||
154 | * (Only available for UPS, USPS, and Fedex shipments). |
||
155 | * If this field is not set, your shipments will be returned to the address_form. |
||
156 | * |
||
157 | * Optional |
||
158 | * |
||
159 | * @return string |
||
160 | */ |
||
161 | public function getAddressReturn() |
||
165 | |||
166 | /** |
||
167 | * ID of the Customs Declarations object for an international shipment. |
||
168 | * |
||
169 | * Optional |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function getCustomsDeclaration() |
||
177 | |||
178 | /** |
||
179 | * Total Parcel value to be insured. |
||
180 | * Please note that you need to specify the "insurance_currency" as well as the "insurance_content" |
||
181 | * (via the extra field below, if your package content is not general cargo) as well. |
||
182 | * |
||
183 | * Optional |
||
184 | * |
||
185 | * @return float |
||
186 | */ |
||
187 | public function getInsuranceAmount() |
||
191 | |||
192 | /** |
||
193 | * Currency used for insurance_amount. |
||
194 | * The official ISO 4217 currency codes are used, e.g. "USD" or "EUR". |
||
195 | * |
||
196 | * Required if insurance_amount is set |
||
197 | * |
||
198 | * @return string |
||
199 | * @throws InvalidAttributeException |
||
200 | */ |
||
201 | public function getInsuranceCurrency() |
||
209 | |||
210 | /** |
||
211 | * An array of extra services to be requested. |
||
212 | * The following services are currently available. We are continuously adding new ones. |
||
213 | * - signature_confirmation (string, "standard" for standard signature confirmation, "adult" for adult signature) |
||
214 | * - insurance_content (string, specify package content for insurance) |
||
215 | * - saturday_delivery (boolean, marks shipment as to be delivered on a Saturday) |
||
216 | * - bypass_address_validation (boolean, bypassed address validation, if applicable) |
||
217 | * - use_manifests (boolean, shipment to be linked with a manifest (Canada Post only), if applicable) |
||
218 | * - saturday_delivery (boolean, marks shipment as to be delivered on a Saturday. Available for UPS and FedEx.) |
||
219 | * - bypass_address_validation (boolean, bypassed address validation. Available for USPS, FedEx and UPS.) |
||
220 | * - request_retail_rates (boolean, request retail/list rates. Available for FedEx.) |
||
221 | * - use_manifests (boolean, shipment to be linked with a manifest (Canada Post only), if applicable) |
||
222 | * |
||
223 | * Optional |
||
224 | * |
||
225 | * @return array |
||
226 | */ |
||
227 | public function getExtra() |
||
231 | |||
232 | /** |
||
233 | * Optional text to be printed on the shipping label. |
||
234 | * |
||
235 | * Optional |
||
236 | * |
||
237 | * @return string |
||
238 | */ |
||
239 | public function getReference1() |
||
243 | |||
244 | /** |
||
245 | * Optional text to be printed on the shipping label. |
||
246 | * |
||
247 | * Optional |
||
248 | * |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getReference2() |
||
255 | |||
256 | /** |
||
257 | * An array of object_ids of the carrier account objects to be used for getting shipping rates for this shipment. |
||
258 | * If no carrier account object_ids are set in this field, |
||
259 | * Shippo will attempt to generate rates using all the carrier accounts that have the 'active' field set. |
||
260 | * |
||
261 | * @return string |
||
262 | */ |
||
263 | public function getCarrierAccounts() |
||
267 | |||
268 | public function toArray() |
||
272 | } |
||
273 |
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.