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; |
||
7 | class StatusType extends \SimpleUPS\Model |
||
8 | { |
||
9 | |||
10 | private |
||
11 | $CODE_IN_TRANSIT = 'L', |
||
12 | $CODE_DELIVERED = 'D', |
||
13 | $CODE_EXCEPTION = 'X', |
||
14 | $CODE_PICKUP = 'P', |
||
15 | $CODE_MANIFEST_PICKUP = 'M'; |
||
16 | |||
17 | private |
||
18 | /* @var string $code */ |
||
19 | $code, |
||
20 | |||
21 | /* @var string $description */ |
||
22 | $description; |
||
23 | |||
24 | /** |
||
25 | * @internal |
||
26 | * |
||
27 | * @param string $code |
||
28 | * |
||
29 | * @return Status |
||
30 | */ |
||
31 | public function setCode($code) |
||
36 | |||
37 | /** |
||
38 | * @return string |
||
39 | */ |
||
40 | public function getCode() |
||
44 | |||
45 | /** |
||
46 | * @internal |
||
47 | * |
||
48 | * @param string $description |
||
49 | * |
||
50 | * @return Status |
||
51 | */ |
||
52 | public function setDescription($description) |
||
57 | |||
58 | /** |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getDescription() |
||
65 | |||
66 | /** |
||
67 | * @return bool |
||
68 | */ |
||
69 | public function isInTransit() |
||
73 | |||
74 | /** |
||
75 | * @return bool |
||
76 | */ |
||
77 | public function isDelivered() |
||
81 | |||
82 | /** |
||
83 | * @return bool |
||
84 | */ |
||
85 | public function isException() |
||
89 | |||
90 | /** |
||
91 | * @return bool |
||
92 | */ |
||
93 | public function isPickup() |
||
97 | |||
98 | /** |
||
99 | * @return bool |
||
100 | */ |
||
101 | public function isManifestPickup() |
||
105 | |||
106 | /** |
||
107 | * @internal |
||
108 | * |
||
109 | * @param \SimpleXMLElement $xml |
||
110 | * |
||
111 | * @return StatusType |
||
112 | */ |
||
113 | View Code Duplication | public static function fromXml(\SimpleXMLElement $xml) |
|
128 | } |
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.