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 Address 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 Address, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
11 | class Address |
||
12 | { |
||
13 | const TAG_NAME = 'common:address'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $streetName; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $number; |
||
24 | |||
25 | /** |
||
26 | * @var string |
||
27 | */ |
||
28 | private $box; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $postalCode; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $locality; |
||
39 | |||
40 | /** |
||
41 | * @var string |
||
42 | */ |
||
43 | private $countryCode = 'BE'; |
||
44 | |||
45 | /** |
||
46 | * @param string $box |
||
47 | * @throws BpostInvalidLengthException |
||
48 | */ |
||
49 | 16 | View Code Duplication | public function setBox($box) |
57 | |||
58 | /** |
||
59 | * @return string |
||
60 | */ |
||
61 | 15 | public function getBox() |
|
65 | |||
66 | /** |
||
67 | * @param string $countryCode |
||
68 | * @throws BpostInvalidLengthException |
||
69 | */ |
||
70 | 21 | View Code Duplication | public function setCountryCode($countryCode) |
78 | |||
79 | /** |
||
80 | * @return string |
||
81 | */ |
||
82 | 15 | public function getCountryCode() |
|
86 | |||
87 | /** |
||
88 | * @param string $locality |
||
89 | * @throws BpostInvalidLengthException |
||
90 | */ |
||
91 | 21 | View Code Duplication | public function setLocality($locality) |
99 | |||
100 | /** |
||
101 | * @return string |
||
102 | */ |
||
103 | 15 | public function getLocality() |
|
107 | |||
108 | /** |
||
109 | * @param string $number |
||
110 | * @throws BpostInvalidLengthException |
||
111 | */ |
||
112 | 21 | View Code Duplication | public function setNumber($number) |
120 | |||
121 | /** |
||
122 | * @return string |
||
123 | */ |
||
124 | 15 | public function getNumber() |
|
128 | |||
129 | /** |
||
130 | * @param string $postalCode |
||
131 | * @throws BpostInvalidLengthException |
||
132 | */ |
||
133 | 21 | View Code Duplication | public function setPostalCode($postalCode) |
141 | |||
142 | /** |
||
143 | * @return string |
||
144 | */ |
||
145 | 15 | public function getPostalCode() |
|
149 | |||
150 | /** |
||
151 | * @param string $streetName |
||
152 | * @throws BpostInvalidLengthException |
||
153 | */ |
||
154 | 21 | View Code Duplication | public function setStreetName($streetName) |
162 | |||
163 | /** |
||
164 | * @return string |
||
165 | */ |
||
166 | 16 | public function getStreetName() |
|
170 | |||
171 | /** |
||
172 | * @param string $streetName |
||
173 | * @param string $number |
||
174 | * @param string $box |
||
175 | * @param string $postalCode |
||
176 | * @param string $locality |
||
177 | * @param string $countryCode |
||
178 | * |
||
179 | * @throws BpostInvalidLengthException |
||
180 | */ |
||
181 | 21 | public function __construct( |
|
208 | |||
209 | /** |
||
210 | * Return the object as an array for usage in the XML |
||
211 | * |
||
212 | * @param \DOMDocument $document |
||
213 | * @param string $prefix |
||
214 | * @return \DOMElement |
||
215 | */ |
||
216 | 14 | public function toXML(\DOMDocument $document, $prefix = 'common') |
|
297 | |||
298 | /** |
||
299 | * @param \SimpleXMLElement $xml |
||
300 | * @return Address |
||
301 | */ |
||
302 | 4 | public static function createFromXML(\SimpleXMLElement $xml) |
|
327 | } |
||
328 |
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.