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 |
||
15 | class Messaging extends Option |
||
16 | { |
||
17 | |||
18 | const MESSAGING_LANGUAGE_EN = 'EN'; |
||
19 | const MESSAGING_LANGUAGE_NL = 'NL'; |
||
20 | const MESSAGING_LANGUAGE_FR = 'FR'; |
||
21 | const MESSAGING_LANGUAGE_DE = 'DE'; |
||
22 | |||
23 | const MESSAGING_TYPE_INFO_DISTRIBUTED = 'infoDistributed'; |
||
24 | const MESSAGING_TYPE_INFO_NEXT_DAY = 'infoNextDay'; |
||
25 | const MESSAGING_TYPE_INFO_REMINDER = 'infoReminder'; |
||
26 | const MESSAGING_TYPE_KEEP_ME_INFORMED = 'keepMeInformed'; |
||
27 | |||
28 | /** |
||
29 | * @var string |
||
30 | */ |
||
31 | private $type; |
||
32 | |||
33 | /** |
||
34 | * @var string |
||
35 | */ |
||
36 | private $language; |
||
37 | |||
38 | /** |
||
39 | * @var string |
||
40 | */ |
||
41 | private $emailAddress; |
||
42 | |||
43 | /** |
||
44 | * @var string |
||
45 | */ |
||
46 | private $mobilePhone; |
||
47 | |||
48 | /** |
||
49 | * @param string $emailAddress |
||
50 | * @throws BpostInvalidLengthException |
||
51 | */ |
||
52 | 5 | View Code Duplication | public function setEmailAddress($emailAddress) |
61 | |||
62 | /** |
||
63 | * @return string |
||
64 | */ |
||
65 | 4 | public function getEmailAddress() |
|
69 | |||
70 | /** |
||
71 | * @param string $language |
||
72 | * @throws BpostInvalidValueException |
||
73 | */ |
||
74 | 7 | View Code Duplication | public function setLanguage($language) |
84 | |||
85 | /** |
||
86 | * @return string |
||
87 | */ |
||
88 | 4 | public function getLanguage() |
|
92 | |||
93 | /** |
||
94 | * @return array |
||
95 | */ |
||
96 | 7 | public static function getPossibleLanguageValues() |
|
105 | |||
106 | /** |
||
107 | * @param string $mobilePhone |
||
108 | * @throws BpostInvalidLengthException |
||
109 | */ |
||
110 | 5 | View Code Duplication | public function setMobilePhone($mobilePhone) |
119 | |||
120 | /** |
||
121 | * @return string |
||
122 | */ |
||
123 | 4 | public function getMobilePhone() |
|
127 | |||
128 | /** |
||
129 | * @return array |
||
130 | */ |
||
131 | 7 | public static function getPossibleTypeValues() |
|
140 | |||
141 | /** |
||
142 | * @param string $type |
||
143 | * @throws BpostInvalidValueException |
||
144 | */ |
||
145 | 7 | public function setType($type) |
|
154 | |||
155 | /** |
||
156 | * @return string |
||
157 | */ |
||
158 | 4 | public function getType() |
|
162 | |||
163 | /** |
||
164 | * @param string $type |
||
165 | * @param string $language |
||
166 | * @param string|null $emailAddress |
||
167 | * @param string|null $mobilePhone |
||
168 | * |
||
169 | * @throws BpostInvalidLengthException |
||
170 | * @throws BpostInvalidValueException |
||
171 | */ |
||
172 | 7 | public function __construct($type, $language, $emailAddress = null, $mobilePhone = null) |
|
184 | |||
185 | /** |
||
186 | * Return the object as an array for usage in the XML |
||
187 | * |
||
188 | * @param \DomDocument $document |
||
189 | * @param string $prefix |
||
190 | * @return \DomElement |
||
191 | */ |
||
192 | 4 | public function toXML(\DOMDocument $document, $prefix = 'common') |
|
229 | |||
230 | /** |
||
231 | * @param \SimpleXMLElement $xml |
||
232 | * |
||
233 | * @return Messaging |
||
234 | * @throws BpostInvalidLengthException |
||
235 | */ |
||
236 | 2 | public static function createFromXML(\SimpleXMLElement $xml) |
|
252 | } |
||
253 |
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.