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 |
||
9 | class ShipTo implements NodeInterface |
||
10 | { |
||
11 | /** @deprecated */ |
||
12 | public $LocationID; |
||
13 | /** @deprecated */ |
||
14 | public $ReceivingAddressName; |
||
15 | /** @deprecated */ |
||
16 | public $Bookmark; |
||
17 | /** @deprecated */ |
||
18 | public $ShipperAssignedIdentificationNumber; |
||
19 | /** @deprecated */ |
||
20 | public $CompanyName; |
||
21 | /** @deprecated */ |
||
22 | public $AttentionName; |
||
23 | /** @deprecated */ |
||
24 | public $PhoneNumber; |
||
25 | /** @deprecated */ |
||
26 | public $TaxIdentificationNumber; |
||
27 | /** @deprecated */ |
||
28 | public $FaxNumber; |
||
29 | /** @deprecated */ |
||
30 | public $EMailAddress; |
||
31 | /** @deprecated */ |
||
32 | public $Address; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $locationId; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $receivingAddressName; |
||
43 | |||
44 | /** |
||
45 | * @var string |
||
46 | */ |
||
47 | private $bookmark; |
||
48 | |||
49 | /** |
||
50 | * @var string |
||
51 | */ |
||
52 | private $shipperAssignedIdentificationNumber; |
||
53 | |||
54 | /** |
||
55 | * @var string |
||
56 | */ |
||
57 | private $companyName; |
||
58 | |||
59 | /** |
||
60 | * @var string |
||
61 | */ |
||
62 | private $attentionName; |
||
63 | |||
64 | /** |
||
65 | * @var string |
||
66 | */ |
||
67 | private $phoneNumber; |
||
68 | |||
69 | /** |
||
70 | * @var string |
||
71 | */ |
||
72 | private $taxIdentificationNumber; |
||
73 | |||
74 | /** |
||
75 | * @var string |
||
76 | */ |
||
77 | private $faxNumber; |
||
78 | |||
79 | /** |
||
80 | * @var string |
||
81 | */ |
||
82 | private $emailAddress; |
||
83 | |||
84 | /** |
||
85 | * @var Address |
||
86 | */ |
||
87 | private $address; |
||
88 | |||
89 | /** |
||
90 | * @param null|object $attributes |
||
91 | */ |
||
92 | 1 | public function __construct($attributes = null) |
|
93 | { |
||
94 | 1 | $this->address = new Address(); |
|
95 | |||
96 | 1 | if (null != $attributes) { |
|
97 | if (isset($attributes->LocationID)) { |
||
98 | $this->setLocationId($attributes->LocationID); |
||
99 | } |
||
100 | if (isset($attributes->ReceivingAddressName)) { |
||
101 | $this->setReceivingAddressName($attributes->ReceivingAddressName); |
||
102 | } |
||
103 | if (isset($attributes->Bookmark)) { |
||
104 | $this->setBookmark($attributes->Bookmark); |
||
105 | } |
||
106 | if (isset($attributes->ShipperAssignedIdentificationNumber)) { |
||
107 | $this->setShipperAssignedIdentificationNumber($attributes->ShipperAssignedIdentificationNumber); |
||
108 | } |
||
109 | if (isset($attributes->CompanyName)) { |
||
110 | $this->setCompanyName($attributes->CompanyName); |
||
111 | } |
||
112 | if (isset($attributes->AttentionName)) { |
||
113 | $this->setAttentionName($attributes->AttentionName); |
||
114 | } |
||
115 | if (isset($attributes->PhoneNumber)) { |
||
116 | $this->setPhoneNumber($attributes->PhoneNumber); |
||
117 | } |
||
118 | if (isset($attributes->TaxIdentificationNumber)) { |
||
119 | $this->setTaxIdentificationNumber($attributes->TaxIdentificationNumber); |
||
120 | } |
||
121 | if (isset($attributes->FaxNumber)) { |
||
122 | $this->setFaxNumber($attributes->FaxNumber); |
||
123 | } |
||
124 | if (isset($attributes->EMailAddress)) { |
||
125 | $this->setEmailAddress($attributes->EMailAddress); |
||
126 | } |
||
127 | if (isset($attributes->Address)) { |
||
128 | $this->setAddress(new Address($attributes->Address)); |
||
129 | } |
||
130 | } |
||
131 | 1 | } |
|
132 | |||
133 | /** |
||
134 | * @param null|DOMDocument $document |
||
135 | * |
||
136 | * @return DOMElement |
||
137 | */ |
||
138 | View Code Duplication | public function toNode(DOMDocument $document = null) |
|
|
|||
139 | { |
||
140 | if (null === $document) { |
||
141 | $document = new DOMDocument(); |
||
142 | } |
||
143 | |||
144 | $node = $document->createElement('ShipTo'); |
||
145 | $node->appendChild($document->createElement('CompanyName', $this->getCompanyName())); |
||
146 | $node->appendChild($document->createElement('AttentionName', $this->getAttentionName())); |
||
147 | |||
148 | $address = $this->getAddress(); |
||
149 | if (isset($address)) { |
||
150 | $node->appendChild($address->toNode($document)); |
||
151 | } |
||
152 | |||
153 | return $node; |
||
154 | } |
||
155 | |||
156 | /** |
||
157 | * @return Address |
||
158 | */ |
||
159 | public function getAddress() |
||
163 | |||
164 | /** |
||
165 | * @param Address $address |
||
166 | * |
||
167 | * @return $this |
||
168 | */ |
||
169 | public function setAddress(Address $address) |
||
170 | { |
||
171 | $this->Address = $address; |
||
172 | $this->address = $address; |
||
173 | |||
174 | return $this; |
||
175 | } |
||
176 | |||
177 | /** |
||
178 | * @return string |
||
179 | */ |
||
180 | public function getAttentionName() |
||
184 | |||
185 | /** |
||
186 | * @param string $attentionName |
||
187 | * |
||
188 | * @return $this |
||
189 | */ |
||
190 | public function setAttentionName($attentionName) |
||
191 | { |
||
192 | $this->AttentionName = $attentionName; |
||
193 | $this->attentionName = $attentionName; |
||
194 | |||
195 | return $this; |
||
196 | } |
||
197 | |||
198 | /** |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getBookmark() |
||
205 | |||
206 | /** |
||
207 | * @param string $bookmark |
||
208 | * |
||
209 | * @return $this |
||
210 | */ |
||
211 | public function setBookmark($bookmark) |
||
212 | { |
||
213 | $this->Bookmark = $bookmark; |
||
214 | $this->bookmark = $bookmark; |
||
215 | |||
216 | return $this; |
||
217 | } |
||
218 | |||
219 | /** |
||
220 | * @return string |
||
221 | */ |
||
222 | public function getCompanyName() |
||
226 | |||
227 | /** |
||
228 | * @param string $companyName |
||
229 | * |
||
230 | * @return $this |
||
231 | */ |
||
232 | public function setCompanyName($companyName) |
||
233 | { |
||
234 | $this->CompanyName = $companyName; |
||
235 | $this->companyName = $companyName; |
||
236 | |||
237 | return $this; |
||
238 | } |
||
239 | |||
240 | /** |
||
241 | * @return string |
||
242 | */ |
||
243 | public function getEmailAddress() |
||
247 | |||
248 | /** |
||
249 | * @param string $emailAddress |
||
250 | * |
||
251 | * @return $this |
||
252 | */ |
||
253 | public function setEmailAddress($emailAddress) |
||
254 | { |
||
255 | $this->EMailAddress = $emailAddress; |
||
256 | $this->emailAddress = $emailAddress; |
||
257 | |||
258 | return $this; |
||
259 | } |
||
260 | |||
261 | /** |
||
262 | * @return string |
||
263 | */ |
||
264 | public function getFaxNumber() |
||
268 | |||
269 | /** |
||
270 | * @param string $faxNumber |
||
271 | * |
||
272 | * @return $this |
||
273 | */ |
||
274 | public function setFaxNumber($faxNumber) |
||
275 | { |
||
276 | $this->FaxNumber = $faxNumber; |
||
277 | $this->faxNumber = $faxNumber; |
||
278 | |||
279 | return $this; |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * @return string |
||
284 | */ |
||
285 | public function getLocationId() |
||
289 | |||
290 | /** |
||
291 | * @param string $locationId |
||
292 | * |
||
293 | * @return $this |
||
294 | */ |
||
295 | public function setLocationId($locationId) |
||
296 | { |
||
297 | $this->LocationID = $locationId; |
||
298 | $this->locationId = $locationId; |
||
299 | |||
300 | return $this; |
||
301 | } |
||
302 | |||
303 | /** |
||
304 | * @return string |
||
305 | */ |
||
306 | public function getPhoneNumber() |
||
310 | |||
311 | /** |
||
312 | * @param string $phoneNumber |
||
313 | * |
||
314 | * @return $this |
||
315 | */ |
||
316 | public function setPhoneNumber($phoneNumber) |
||
317 | { |
||
318 | $this->PhoneNumber = $phoneNumber; |
||
319 | $this->phoneNumber = $phoneNumber; |
||
320 | |||
321 | return $this; |
||
322 | } |
||
323 | |||
324 | /** |
||
325 | * @return string |
||
326 | */ |
||
327 | public function getReceivingAddressName() |
||
331 | |||
332 | /** |
||
333 | * @param string $receivingAddressName |
||
334 | * |
||
335 | * @return $this |
||
336 | */ |
||
337 | public function setReceivingAddressName($receivingAddressName) |
||
338 | { |
||
339 | $this->ReceivingAddressName = $receivingAddressName; |
||
340 | $this->receivingAddressName = $receivingAddressName; |
||
341 | |||
342 | return $this; |
||
343 | } |
||
344 | |||
345 | /** |
||
346 | * @return string |
||
347 | */ |
||
348 | public function getShipperAssignedIdentificationNumber() |
||
352 | |||
353 | /** |
||
354 | * @param string $shipperAssignedIdentificationNumber |
||
355 | * |
||
356 | * @return $this |
||
357 | */ |
||
358 | public function setShipperAssignedIdentificationNumber($shipperAssignedIdentificationNumber) |
||
359 | { |
||
360 | $this->ShipperAssignedIdentificationNumber = $shipperAssignedIdentificationNumber; |
||
361 | $this->shipperAssignedIdentificationNumber = $shipperAssignedIdentificationNumber; |
||
362 | |||
363 | return $this; |
||
364 | } |
||
365 | |||
366 | /** |
||
367 | * @return string |
||
368 | */ |
||
369 | public function getTaxIdentificationNumber() |
||
373 | |||
374 | /** |
||
375 | * @param string $taxIdentificationNumber |
||
376 | * |
||
377 | * @return $this |
||
378 | */ |
||
379 | public function setTaxIdentificationNumber($taxIdentificationNumber) |
||
380 | { |
||
381 | $this->TaxIdentificationNumber = $taxIdentificationNumber; |
||
382 | $this->taxIdentificationNumber = $taxIdentificationNumber; |
||
383 | |||
384 | return $this; |
||
385 | } |
||
386 | } |
||
387 |
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.