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 Package 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 Package, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
9 | class Package implements NodeInterface |
||
10 | { |
||
11 | const PKG_OVERSIZE1 = '1'; |
||
12 | const PKG_OVERSIZE2 = '2'; |
||
13 | const PKG_LARGE = '4'; |
||
14 | |||
15 | /** |
||
16 | * @var PackagingType |
||
17 | */ |
||
18 | private $packagingType; |
||
19 | |||
20 | /** |
||
21 | * @var PackageWeight |
||
22 | */ |
||
23 | private $packageWeight; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $description; |
||
29 | |||
30 | /** |
||
31 | * @var PackageServiceOptions |
||
32 | */ |
||
33 | private $packageServiceOptions; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $upsPremiumCareIndicator; |
||
39 | |||
40 | /** |
||
41 | * @var ReferenceNumber |
||
42 | */ |
||
43 | private $referenceNumber; |
||
44 | |||
45 | /** |
||
46 | * @var ReferenceNumber |
||
47 | */ |
||
48 | private $referenceNumber2; |
||
49 | |||
50 | /** |
||
51 | * @var string |
||
52 | */ |
||
53 | private $trackingNumber; |
||
54 | |||
55 | /** |
||
56 | * @var bool |
||
57 | */ |
||
58 | private $isLargePackage; |
||
59 | |||
60 | /** |
||
61 | * @var bool |
||
62 | */ |
||
63 | private $additionalHandling; |
||
64 | |||
65 | /** |
||
66 | * @var Dimensions|null |
||
67 | */ |
||
68 | private $dimensions; |
||
69 | |||
70 | /** |
||
71 | * @var Activity[] |
||
72 | */ |
||
73 | private $activities = []; |
||
74 | |||
75 | /** |
||
76 | * @param null|object $attributes |
||
77 | */ |
||
78 | 1 | public function __construct($attributes = null) |
|
129 | |||
130 | /** |
||
131 | * @param null|DOMDocument $document |
||
132 | * |
||
133 | * @return DOMElement |
||
134 | */ |
||
135 | 1 | public function toNode(DOMDocument $document = null) |
|
182 | |||
183 | /** |
||
184 | * @return Activity[] |
||
185 | */ |
||
186 | public function getActivities() |
||
190 | |||
191 | /** |
||
192 | * @param Activity[] $activities |
||
193 | * |
||
194 | * @return Package |
||
195 | */ |
||
196 | public function setActivities(array $activities) |
||
202 | |||
203 | /** |
||
204 | * @return string |
||
205 | */ |
||
206 | 1 | public function getDescription() |
|
210 | |||
211 | /** |
||
212 | * @param string $description |
||
213 | * |
||
214 | * @return Package |
||
215 | */ |
||
216 | public function setDescription($description) |
||
222 | |||
223 | /** |
||
224 | * @return Dimensions|null |
||
225 | */ |
||
226 | 1 | public function getDimensions() |
|
230 | |||
231 | /** |
||
232 | * @param Dimensions $dimensions |
||
233 | * |
||
234 | * @return Package |
||
235 | */ |
||
236 | public function setDimensions(Dimensions $dimensions) |
||
242 | |||
243 | /** |
||
244 | * @return bool |
||
245 | */ |
||
246 | 1 | public function isLargePackage() |
|
250 | |||
251 | /** |
||
252 | * @param bool $largePackage |
||
253 | * |
||
254 | * @return Package |
||
255 | */ |
||
256 | public function setLargePackage($largePackage) |
||
262 | |||
263 | /** |
||
264 | * @return PackageServiceOptions |
||
265 | */ |
||
266 | 1 | public function getPackageServiceOptions() |
|
270 | |||
271 | /** |
||
272 | * @param PackageServiceOptions $packageServiceOptions |
||
273 | * |
||
274 | * @return Package |
||
275 | */ |
||
276 | 1 | public function setPackageServiceOptions(PackageServiceOptions $packageServiceOptions) |
|
282 | |||
283 | /** |
||
284 | * @return PackageWeight |
||
285 | */ |
||
286 | 1 | public function getPackageWeight() |
|
290 | |||
291 | /** |
||
292 | * @param PackageWeight $packageWeight |
||
293 | * |
||
294 | * @return Package |
||
295 | */ |
||
296 | 1 | public function setPackageWeight(PackageWeight $packageWeight) |
|
302 | |||
303 | /** |
||
304 | * @return PackagingType |
||
305 | */ |
||
306 | 1 | public function getPackagingType() |
|
310 | |||
311 | /** |
||
312 | * @param PackagingType $packagingType |
||
313 | * |
||
314 | * @return Package |
||
315 | */ |
||
316 | 1 | public function setPackagingType(PackagingType $packagingType) |
|
322 | |||
323 | /** |
||
324 | * @return ReferenceNumber |
||
325 | */ |
||
326 | 1 | public function getReferenceNumber() |
|
330 | |||
331 | /** |
||
332 | * @param ReferenceNumber $referenceNumber |
||
333 | * |
||
334 | * @return Package |
||
335 | */ |
||
336 | 1 | public function setReferenceNumber(ReferenceNumber $referenceNumber) |
|
342 | |||
343 | public function removeReferenceNumber() |
||
347 | |||
348 | /** |
||
349 | * @return ReferenceNumber |
||
350 | */ |
||
351 | 1 | public function getReferenceNumber2() |
|
355 | |||
356 | /** |
||
357 | * @param ReferenceNumber $referenceNumber |
||
358 | * |
||
359 | * @return Package |
||
360 | */ |
||
361 | 1 | public function setReferenceNumber2(ReferenceNumber $referenceNumber) |
|
367 | |||
368 | public function removeReferenceNumber2() |
||
372 | |||
373 | /** |
||
374 | * @return string |
||
375 | */ |
||
376 | public function getTrackingNumber() |
||
380 | |||
381 | /** |
||
382 | * @param string $trackingNumber |
||
383 | * |
||
384 | * @return Package |
||
385 | */ |
||
386 | public function setTrackingNumber($trackingNumber) |
||
392 | |||
393 | /** |
||
394 | * @return string |
||
395 | */ |
||
396 | public function getUpsPremiumCareIndicator() |
||
400 | |||
401 | /** |
||
402 | * @param string $upsPremiumCareIndicator |
||
403 | * |
||
404 | * @return Package |
||
405 | */ |
||
406 | public function setUpsPremiumCareIndicator($upsPremiumCareIndicator) |
||
412 | |||
413 | /** |
||
414 | * @return boolean |
||
415 | */ |
||
416 | 1 | public function getAdditionalHandling() |
|
420 | |||
421 | /** |
||
422 | * @param boolean $additionalHandling |
||
423 | */ |
||
424 | public function setAdditionalHandling($additionalHandling) |
||
428 | } |
||
429 |
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.