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; |
||
7 | class Service extends Model |
||
8 | { |
||
9 | |||
10 | const |
||
11 | NEXT_DAY_AIR_EARLY_AM = '14', |
||
12 | NEXT_DAY_AIR = '01', |
||
13 | NEXT_DAY_AIR_SAVER = '13', |
||
14 | SECOND_DAY_AIR_AM = '59', |
||
15 | SECOND_DAY_AIR = '02', |
||
16 | THREE_DAY_SELECT = '12', |
||
17 | GROUND = '03', |
||
18 | |||
19 | INT_STANDARD = '11', |
||
20 | INT_WORLDWIDE_EXPRESS = '07', |
||
21 | INT_WORLDWIDE_EXPRESS_PLUS = '54', |
||
22 | INT_WORLDWIDE_EXPEDITED = '08', |
||
23 | INT_SAVER = '65', |
||
24 | |||
25 | POLAND_TODAY_STANDARD = '82', |
||
26 | POLAND_TODAY_DEDICATED_COURIER = '83', |
||
27 | POLAND_TODAY_INTERCITY = '84', |
||
28 | POLAND_TODAY_EXPRESS = '85', |
||
29 | POLAND_TODAY_EXPRESS_SAVER = '86', |
||
30 | POLAND_WORLDWIDE_EXPRESS_FREIGHT = '96'; |
||
31 | |||
32 | private |
||
33 | /* @var string $code */ |
||
34 | $code, |
||
35 | |||
36 | /* @var string $description */ |
||
37 | $description, |
||
38 | |||
39 | $descriptions = array( |
||
40 | '14' => 'Next Day Air Early AM', |
||
41 | '01' => 'Next Day Air', |
||
42 | '13' => 'Next Day Air Saver', |
||
43 | '59' => 'Second Day Air AM', |
||
44 | '02' => 'Second Day Air', |
||
45 | '12' => 'Three Day Select', |
||
46 | '03' => 'Ground', |
||
47 | '11' => 'International Standard', |
||
48 | '07' => 'International Worldwide Express', |
||
49 | '54' => 'International Worldwide Express Plus', |
||
50 | '08' => 'International Worldwide Expedited', |
||
51 | '65' => 'International Saver', |
||
52 | '82' => 'Poland Today Standard', |
||
53 | '83' => 'Poland Today Dedicated Courier', |
||
54 | '84' => 'Poland Today Intercity', |
||
55 | '85' => 'Poland Today Express', |
||
56 | '86' => 'Poland Today Express Saver', |
||
57 | '96' => 'Poland Worldwide Express Freight' |
||
58 | ); |
||
59 | |||
60 | /** |
||
61 | * Set the service code |
||
62 | * @internal |
||
63 | * |
||
64 | * @param string $code |
||
65 | * |
||
66 | * @return Service |
||
67 | */ |
||
68 | public function setCode($code) |
||
78 | |||
79 | /** |
||
80 | * Get the service code |
||
81 | * @return string |
||
82 | */ |
||
83 | public function getCode() |
||
87 | |||
88 | /** |
||
89 | * Set the service description |
||
90 | * @internal |
||
91 | * |
||
92 | * @param string $description |
||
93 | * |
||
94 | * @return Service |
||
95 | */ |
||
96 | public function setDescription($description) |
||
101 | |||
102 | /** |
||
103 | * Get the service description |
||
104 | * @return string |
||
105 | */ |
||
106 | public function getDescription() |
||
110 | |||
111 | /** |
||
112 | * Get service as XML |
||
113 | * @internal |
||
114 | * |
||
115 | * @param \DomDocument $dom |
||
116 | * |
||
117 | * @return \DOMElement |
||
118 | */ |
||
119 | public function toXml(\DomDocument $dom) |
||
133 | |||
134 | /** |
||
135 | * Get this object from XML |
||
136 | * @internal |
||
137 | * |
||
138 | * @param \SimpleXMLElement $xml |
||
139 | * |
||
140 | * @return Service |
||
141 | */ |
||
142 | View Code Duplication | public static function fromXml(\SimpleXMLElement $xml) |
|
154 | } |
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.