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 |
||
11 | class Customer |
||
12 | { |
||
13 | const TAG_NAME = 'customer'; |
||
14 | |||
15 | /** |
||
16 | * @var string |
||
17 | */ |
||
18 | private $name; |
||
19 | |||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $company; |
||
24 | |||
25 | /** |
||
26 | * @var Address |
||
27 | */ |
||
28 | private $address; |
||
29 | |||
30 | /** |
||
31 | * @var string |
||
32 | */ |
||
33 | private $emailAddress; |
||
34 | |||
35 | /** |
||
36 | * @var string |
||
37 | */ |
||
38 | private $phoneNumber; |
||
39 | |||
40 | /** |
||
41 | * @param \Bpost\BpostApiClient\Bpost\Order\Address $address |
||
42 | */ |
||
43 | 12 | public function setAddress($address) |
|
47 | |||
48 | /** |
||
49 | * @return \Bpost\BpostApiClient\Bpost\Order\Address |
||
50 | */ |
||
51 | 10 | public function getAddress() |
|
55 | |||
56 | /** |
||
57 | * @param string $company |
||
58 | */ |
||
59 | 12 | public function setCompany($company) |
|
63 | |||
64 | /** |
||
65 | * @return string |
||
66 | */ |
||
67 | 12 | public function getCompany() |
|
71 | |||
72 | /** |
||
73 | * @param string $emailAddress |
||
74 | * @throws BpostInvalidLengthException |
||
75 | */ |
||
76 | 15 | View Code Duplication | public function setEmailAddress($emailAddress) |
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | 10 | public function getEmailAddress() |
|
92 | |||
93 | /** |
||
94 | * @param string $name |
||
95 | */ |
||
96 | 12 | public function setName($name) |
|
100 | |||
101 | /** |
||
102 | * @return string |
||
103 | */ |
||
104 | 10 | public function getName() |
|
108 | |||
109 | /** |
||
110 | * @param string $phoneNumber |
||
111 | * @throws BpostInvalidLengthException |
||
112 | */ |
||
113 | 15 | View Code Duplication | public function setPhoneNumber($phoneNumber) |
121 | |||
122 | /** |
||
123 | * @return string |
||
124 | */ |
||
125 | 10 | public function getPhoneNumber() |
|
129 | |||
130 | /** |
||
131 | * Return the object as an array for usage in the XML |
||
132 | * |
||
133 | * @param \DomDocument $document |
||
134 | * @param string $prefix |
||
135 | * @return \DomElement |
||
136 | */ |
||
137 | 9 | public function toXML(\DomDocument $document, $prefix = null) |
|
186 | |||
187 | /** |
||
188 | * @param \SimpleXMLElement $xml |
||
189 | * @param Customer $instance |
||
190 | * |
||
191 | * @return Customer |
||
192 | * @throws BpostInvalidLengthException |
||
193 | */ |
||
194 | 3 | public static function createFromXMLHelper(\SimpleXMLElement $xml, Customer $instance) |
|
218 | } |
||
219 |
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.