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 namespace SimpleUPS\Track\SmallPackage; |
||
6 | View Code Duplication | class ShipmentType extends \SimpleUPS\Model |
|
|
|||
7 | { |
||
8 | |||
9 | private |
||
10 | /* @var string $code */ |
||
11 | $code, |
||
12 | |||
13 | /* @var string $description */ |
||
14 | $description; |
||
15 | |||
16 | /** |
||
17 | * @internal |
||
18 | * |
||
19 | * @param string $code |
||
20 | * |
||
21 | * @return ShipmentType |
||
22 | */ |
||
23 | public function setCode($code) |
||
28 | |||
29 | /** |
||
30 | * Code indicating the type of the Product |
||
31 | * @return string |
||
32 | */ |
||
33 | public function getCode() |
||
37 | |||
38 | /** |
||
39 | * @internal |
||
40 | * |
||
41 | * @param string $description |
||
42 | * |
||
43 | * @return ShipmentType |
||
44 | */ |
||
45 | public function setDescription($description) |
||
50 | |||
51 | /** |
||
52 | * Description of the type of the Product. |
||
53 | * Valid Value: “World Ease” (when a shipment with single/multiple packages is associated with World Ease movement). |
||
54 | * @return string |
||
55 | */ |
||
56 | public function getDescription() |
||
60 | |||
61 | /** |
||
62 | * @return bool |
||
63 | */ |
||
64 | public function isWorldEase() |
||
68 | |||
69 | /** |
||
70 | * @internal |
||
71 | * |
||
72 | * @param \SimpleXMLElement $xml |
||
73 | * |
||
74 | * @return ProductType |
||
75 | */ |
||
76 | public static function fromXml(\SimpleXMLElement $xml) |
||
86 | } |
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.