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:
Complex classes like SoldTo often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use SoldTo, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class SoldTo 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 | * @var string |
||
91 | */ |
||
92 | private $option; |
||
93 | |||
94 | /** |
||
95 | * @param null|object $attributes |
||
96 | */ |
||
97 | public function __construct($attributes = null) |
||
98 | { |
||
99 | $this->address = new Address(); |
||
100 | |||
101 | if (null != $attributes) { |
||
102 | if (isset($attributes->LocationID)) { |
||
103 | $this->setLocationId($attributes->LocationID); |
||
104 | } |
||
105 | if (isset($attributes->ReceivingAddressName)) { |
||
106 | $this->setReceivingAddressName($attributes->ReceivingAddressName); |
||
107 | } |
||
108 | if (isset($attributes->Bookmark)) { |
||
109 | $this->setBookmark($attributes->Bookmark); |
||
110 | } |
||
111 | if (isset($attributes->ShipperAssignedIdentificationNumber)) { |
||
112 | $this->setShipperAssignedIdentificationNumber($attributes->ShipperAssignedIdentificationNumber); |
||
113 | } |
||
114 | if (isset($attributes->CompanyName)) { |
||
115 | $this->setCompanyName($attributes->CompanyName); |
||
116 | } |
||
117 | if (isset($attributes->AttentionName)) { |
||
118 | $this->setAttentionName($attributes->AttentionName); |
||
119 | } |
||
120 | if (isset($attributes->PhoneNumber)) { |
||
121 | $this->setPhoneNumber($attributes->PhoneNumber); |
||
122 | } |
||
123 | if (isset($attributes->TaxIdentificationNumber)) { |
||
124 | $this->setTaxIdentificationNumber($attributes->TaxIdentificationNumber); |
||
125 | } |
||
126 | if (isset($attributes->FaxNumber)) { |
||
127 | $this->setFaxNumber($attributes->FaxNumber); |
||
128 | } |
||
129 | if (isset($attributes->EMailAddress)) { |
||
130 | $this->setEmailAddress($attributes->EMailAddress); |
||
131 | } |
||
132 | if (isset($attributes->Address)) { |
||
133 | $this->setAddress(new Address($attributes->Address)); |
||
134 | } |
||
135 | if (isset($attributes->Option)) { |
||
136 | $this->setOption($attributes->Option); |
||
137 | } |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * @param null|DOMDocument $document |
||
143 | * |
||
144 | * @return DOMElement |
||
145 | */ |
||
146 | View Code Duplication | public function toNode(DOMDocument $document = null) |
|
|
|||
147 | { |
||
148 | if (null === $document) { |
||
149 | $document = new DOMDocument(); |
||
150 | } |
||
151 | |||
152 | $node = $document->createElement('ShipTo'); |
||
153 | $node->appendChild($document->createElement('CompanyName', $this->getCompanyName())); |
||
154 | $node->appendChild($document->createElement('AttentionName', $this->getAttentionName())); |
||
155 | |||
156 | $address = $this->getAddress(); |
||
157 | if (isset($address)) { |
||
158 | $node->appendChild($address->toNode($document)); |
||
159 | } |
||
160 | |||
161 | return $node; |
||
162 | } |
||
163 | |||
164 | /** |
||
165 | * @return Address |
||
166 | */ |
||
167 | public function getAddress() |
||
171 | |||
172 | /** |
||
173 | * @param Address $address |
||
174 | * |
||
175 | * @return SoldTo |
||
176 | */ |
||
177 | public function setAddress(Address $address) |
||
178 | { |
||
179 | $this->Address = $address; |
||
180 | $this->address = $address; |
||
181 | |||
182 | return $this; |
||
183 | } |
||
184 | |||
185 | /** |
||
186 | * @return string |
||
187 | */ |
||
188 | public function getAttentionName() |
||
192 | |||
193 | /** |
||
194 | * @param string $attentionName |
||
195 | * |
||
196 | * @return SoldTo |
||
197 | */ |
||
198 | public function setAttentionName($attentionName) |
||
199 | { |
||
200 | $this->AttentionName = $attentionName; |
||
201 | $this->attentionName = $attentionName; |
||
202 | |||
203 | return $this; |
||
204 | } |
||
205 | |||
206 | /** |
||
207 | * @return string |
||
208 | */ |
||
209 | public function getBookmark() |
||
213 | |||
214 | /** |
||
215 | * @param string $bookmark |
||
216 | * |
||
217 | * @return SoldTo |
||
218 | */ |
||
219 | public function setBookmark($bookmark) |
||
220 | { |
||
221 | $this->Bookmark = $bookmark; |
||
222 | $this->bookmark = $bookmark; |
||
223 | |||
224 | return $this; |
||
225 | } |
||
226 | |||
227 | /** |
||
228 | * @return string |
||
229 | */ |
||
230 | public function getCompanyName() |
||
234 | |||
235 | /** |
||
236 | * @param string $companyName |
||
237 | * |
||
238 | * @return SoldTo |
||
239 | */ |
||
240 | public function setCompanyName($companyName) |
||
241 | { |
||
242 | $this->CompanyName = $companyName; |
||
243 | $this->companyName = $companyName; |
||
244 | |||
245 | return $this; |
||
246 | } |
||
247 | |||
248 | /** |
||
249 | * @return string |
||
250 | */ |
||
251 | public function getEmailAddress() |
||
255 | |||
256 | /** |
||
257 | * @param string $emailAddress |
||
258 | * |
||
259 | * @return SoldTo |
||
260 | */ |
||
261 | public function setEmailAddress($emailAddress) |
||
262 | { |
||
263 | $this->EMailAddress = $emailAddress; |
||
264 | $this->emailAddress = $emailAddress; |
||
265 | |||
266 | return $this; |
||
267 | } |
||
268 | |||
269 | /** |
||
270 | * @return string |
||
271 | */ |
||
272 | public function getFaxNumber() |
||
276 | |||
277 | /** |
||
278 | * @param string $faxNumber |
||
279 | * |
||
280 | * @return SoldTo |
||
281 | */ |
||
282 | public function setFaxNumber($faxNumber) |
||
283 | { |
||
284 | $this->FaxNumber = $faxNumber; |
||
285 | $this->faxNumber = $faxNumber; |
||
286 | |||
287 | return $this; |
||
288 | } |
||
289 | |||
290 | /** |
||
291 | * @return string |
||
292 | */ |
||
293 | public function getLocationId() |
||
297 | |||
298 | /** |
||
299 | * @param string $locationId |
||
300 | * |
||
301 | * @return SoldTo |
||
302 | */ |
||
303 | public function setLocationId($locationId) |
||
304 | { |
||
305 | $this->LocationID = $locationId; |
||
306 | $this->locationId = $locationId; |
||
307 | |||
308 | return $this; |
||
309 | } |
||
310 | |||
311 | /** |
||
312 | * @return string |
||
313 | */ |
||
314 | public function getPhoneNumber() |
||
318 | |||
319 | /** |
||
320 | * @param string $phoneNumber |
||
321 | * |
||
322 | * @return SoldTo |
||
323 | */ |
||
324 | public function setPhoneNumber($phoneNumber) |
||
325 | { |
||
326 | $this->PhoneNumber = $phoneNumber; |
||
327 | $this->phoneNumber = $phoneNumber; |
||
328 | |||
329 | return $this; |
||
330 | } |
||
331 | |||
332 | /** |
||
333 | * @return string |
||
334 | */ |
||
335 | public function getReceivingAddressName() |
||
339 | |||
340 | /** |
||
341 | * @param string $receivingAddressName |
||
342 | * |
||
343 | * @return SoldTo |
||
344 | */ |
||
345 | public function setReceivingAddressName($receivingAddressName) |
||
346 | { |
||
347 | $this->ReceivingAddressName = $receivingAddressName; |
||
348 | $this->receivingAddressName = $receivingAddressName; |
||
349 | |||
350 | return $this; |
||
351 | } |
||
352 | |||
353 | /** |
||
354 | * @return string |
||
355 | */ |
||
356 | public function getShipperAssignedIdentificationNumber() |
||
360 | |||
361 | /** |
||
362 | * @param string $shipperAssignedIdentificationNumber |
||
363 | * |
||
364 | * @return SoldTo |
||
365 | */ |
||
366 | public function setShipperAssignedIdentificationNumber($shipperAssignedIdentificationNumber) |
||
367 | { |
||
368 | $this->ShipperAssignedIdentificationNumber = $shipperAssignedIdentificationNumber; |
||
369 | $this->shipperAssignedIdentificationNumber = $shipperAssignedIdentificationNumber; |
||
370 | |||
371 | return $this; |
||
372 | } |
||
373 | |||
374 | /** |
||
375 | * @return string |
||
376 | */ |
||
377 | public function getTaxIdentificationNumber() |
||
381 | |||
382 | /** |
||
383 | * @param string $taxIdentificationNumber |
||
384 | * |
||
385 | * @return SoldTo |
||
386 | */ |
||
387 | public function setTaxIdentificationNumber($taxIdentificationNumber) |
||
388 | { |
||
389 | $this->TaxIdentificationNumber = $taxIdentificationNumber; |
||
390 | $this->taxIdentificationNumber = $taxIdentificationNumber; |
||
391 | |||
392 | return $this; |
||
393 | } |
||
394 | |||
395 | /** |
||
396 | * @return string |
||
397 | */ |
||
398 | public function getOption() |
||
402 | |||
403 | /** |
||
404 | * @param string $option |
||
405 | * |
||
406 | * @return SoldTo |
||
407 | */ |
||
408 | public function setOption($option) |
||
409 | { |
||
410 | $this->option = $option; |
||
411 | |||
412 | return $this; |
||
413 | } |
||
414 | } |
||
415 |
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.