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; |
||
11 | class Address extends \SimpleUPS\Model |
||
12 | { |
||
13 | |||
14 | /** |
||
15 | * @var string |
||
16 | */ |
||
17 | private $street; |
||
18 | |||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | private $city; |
||
23 | |||
24 | /** |
||
25 | * @var string |
||
26 | */ |
||
27 | private $stateProvinceCode; |
||
28 | |||
29 | /** |
||
30 | * @var string |
||
31 | */ |
||
32 | private $postalCode; |
||
33 | |||
34 | /** |
||
35 | * @var string |
||
36 | */ |
||
37 | private $postalCodeExtended; |
||
38 | |||
39 | /** |
||
40 | * @var string |
||
41 | */ |
||
42 | private $countryCode; |
||
43 | |||
44 | /** |
||
45 | * Set the street |
||
46 | * |
||
47 | * @param string $street |
||
48 | * |
||
49 | * @return Address |
||
50 | */ |
||
51 | public function setStreet($street) |
||
56 | |||
57 | /** |
||
58 | * Street name and number (when applicable) |
||
59 | * @return string |
||
60 | */ |
||
61 | public function getStreet() |
||
65 | |||
66 | /** |
||
67 | * Set the city |
||
68 | * Must be between 1-40 characters |
||
69 | * |
||
70 | * @param string $city |
||
71 | * |
||
72 | * @throws \SimpleUPS\Api\InvalidParameterException |
||
73 | * @return Address |
||
74 | */ |
||
75 | View Code Duplication | public function setCity($city) |
|
84 | |||
85 | /** |
||
86 | * Get the city |
||
87 | * @return string |
||
88 | */ |
||
89 | public function getCity() |
||
93 | |||
94 | /** |
||
95 | * Set the state or province code |
||
96 | * Must be 2 characters |
||
97 | * |
||
98 | * @param string $stateProvinceCode |
||
99 | * |
||
100 | * @throws \SimpleUPS\Api\InvalidParameterException |
||
101 | * @return Address |
||
102 | */ |
||
103 | View Code Duplication | public function setStateProvinceCode($stateProvinceCode) |
|
111 | |||
112 | /** |
||
113 | * Get the state or province |
||
114 | * @return string |
||
115 | */ |
||
116 | public function getStateProvinceCode() |
||
120 | |||
121 | /** |
||
122 | * Set the postal code |
||
123 | * Must be between 1-9 characters |
||
124 | * @see setPostalCodeExtended |
||
125 | * |
||
126 | * @param string $postalCode |
||
127 | * |
||
128 | * @return Address |
||
129 | * @throws \SimpleUPS\Api\InvalidParameterException |
||
130 | */ |
||
131 | public function setPostalCode($postalCode) |
||
144 | |||
145 | /** |
||
146 | * Get the postal code |
||
147 | * @see getPostalCodeExtended |
||
148 | * @return string |
||
149 | */ |
||
150 | public function getPostalCode() |
||
154 | |||
155 | /** |
||
156 | * Set the extended postal code |
||
157 | * @link http://en.wikipedia.org/wiki/ZIP_code#ZIP.2B4 |
||
158 | * |
||
159 | * @param string $postalCodeExtended |
||
160 | * |
||
161 | * @return Address |
||
162 | */ |
||
163 | public function setPostalCodeExtended($postalCodeExtended) |
||
168 | |||
169 | /** |
||
170 | * The last segment of a zip code from the format xxxxx-xxxx |
||
171 | * @link http://en.wikipedia.org/wiki/ZIP_code#ZIP.2B4 |
||
172 | * @return string |
||
173 | */ |
||
174 | public function getPostalCodeExtended() |
||
178 | |||
179 | /** |
||
180 | * Set the country |
||
181 | * Must be 2 characters |
||
182 | * |
||
183 | * @param string $countryCode |
||
184 | * |
||
185 | * @return Address |
||
186 | * @throws \SimpleUPS\Api\InvalidParameterException |
||
187 | */ |
||
188 | View Code Duplication | public function setCountryCode($countryCode) |
|
196 | |||
197 | /** |
||
198 | * Get the country code |
||
199 | * @return string |
||
200 | */ |
||
201 | public function getCountryCode() |
||
205 | |||
206 | /** |
||
207 | * Create an address from XML. SimpleXMLElement passed must have immediate children like AddressLine1, City, etc. |
||
208 | * @internal |
||
209 | * |
||
210 | * @param \SimpleXMLElement $xml |
||
211 | * |
||
212 | * @return \SimpleUPS\Address |
||
213 | */ |
||
214 | public static function fromXml(\SimpleXMLElement $xml) |
||
244 | } |
||
245 |
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.