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 |
||
14 | class Parser |
||
15 | { |
||
16 | /** |
||
17 | * @var string |
||
18 | */ |
||
19 | protected $whitespace = " \r\n\t"; |
||
20 | |||
21 | /** |
||
22 | * @var array |
||
23 | */ |
||
24 | protected $mappers = []; |
||
25 | |||
26 | /** |
||
27 | * @var array |
||
28 | */ |
||
29 | protected $languages = []; |
||
30 | |||
31 | /** |
||
32 | * @var array |
||
33 | */ |
||
34 | protected $nicknameDelimiters = []; |
||
35 | |||
36 | /** |
||
37 | * @var int |
||
38 | */ |
||
39 | protected $maxSalutationIndex = 0; |
||
40 | |||
41 | /** |
||
42 | * @var int |
||
43 | */ |
||
44 | protected $maxCombinedInitials = 2; |
||
45 | |||
46 | public function __construct(array $languages = []) |
||
54 | |||
55 | /** |
||
56 | * split full names into the following parts: |
||
57 | * - prefix / salutation (Mr., Mrs., etc) |
||
58 | * - given name / first name |
||
59 | * - middle initials |
||
60 | * - surname / last name |
||
61 | * - suffix (II, Phd, Jr, etc) |
||
62 | * |
||
63 | * @param string $name |
||
64 | * @return Name |
||
65 | */ |
||
66 | public function parse($name): Name |
||
84 | |||
85 | /** |
||
86 | * handles split-parsing of comma-separated name parts |
||
87 | * |
||
88 | * @param $left - the name part left of the comma |
||
89 | * @param $right - the name part right of the comma |
||
90 | * |
||
91 | * @return Name |
||
92 | */ |
||
93 | protected function parseSplitName($first, $second, $third): Name |
||
103 | |||
104 | /** |
||
105 | * @return Parser |
||
106 | */ |
||
107 | View Code Duplication | protected function getFirstSegmentParser(): Parser |
|
121 | |||
122 | /** |
||
123 | * @return Parser |
||
124 | */ |
||
125 | View Code Duplication | protected function getSecondSegmentParser(): Parser |
|
140 | |||
141 | protected function getThirdSegmentParser(): Parser |
||
151 | |||
152 | /** |
||
153 | * get the mappers for this parser |
||
154 | * |
||
155 | * @return array |
||
156 | */ |
||
157 | public function getMappers(): array |
||
173 | |||
174 | /** |
||
175 | * set the mappers for this parser |
||
176 | * |
||
177 | * @param array $mappers |
||
178 | * @return Parser |
||
179 | */ |
||
180 | public function setMappers(array $mappers): Parser |
||
186 | |||
187 | /** |
||
188 | * normalize the name |
||
189 | * |
||
190 | * @param string $name |
||
191 | * @return string |
||
192 | */ |
||
193 | protected function normalize(string $name): string |
||
201 | |||
202 | /** |
||
203 | * get a string of characters that are supposed to be treated as whitespace |
||
204 | * |
||
205 | * @return string |
||
206 | */ |
||
207 | public function getWhitespace(): string |
||
211 | |||
212 | /** |
||
213 | * set the string of characters that are supposed to be treated as whitespace |
||
214 | * |
||
215 | * @param $whitespace |
||
216 | * @return Parser |
||
217 | */ |
||
218 | public function setWhitespace($whitespace): Parser |
||
224 | |||
225 | /** |
||
226 | * @return array |
||
227 | */ |
||
228 | protected function getPrefixes() |
||
239 | |||
240 | /** |
||
241 | * @return array |
||
242 | */ |
||
243 | protected function getSuffixes() |
||
254 | |||
255 | /** |
||
256 | * @return array |
||
257 | */ |
||
258 | protected function getSalutations() |
||
269 | |||
270 | /** |
||
271 | * @return array |
||
272 | */ |
||
273 | public function getNicknameDelimiters(): array |
||
277 | |||
278 | /** |
||
279 | * @param array $nicknameDelimiters |
||
280 | * @return Parser |
||
281 | */ |
||
282 | public function setNicknameDelimiters(array $nicknameDelimiters): Parser |
||
288 | |||
289 | /** |
||
290 | * @return int |
||
291 | */ |
||
292 | public function getMaxSalutationIndex(): int |
||
296 | |||
297 | /** |
||
298 | * @param int $maxSalutationIndex |
||
299 | * @return Parser |
||
300 | */ |
||
301 | public function setMaxSalutationIndex(int $maxSalutationIndex): Parser |
||
307 | |||
308 | /** |
||
309 | * @return int |
||
310 | */ |
||
311 | public function getMaxCombinedInitials(): int |
||
315 | |||
316 | /** |
||
317 | * @param int $maxCombinedInitials |
||
318 | * @return Parser |
||
319 | */ |
||
320 | public function setMaxCombinedInitials(int $maxCombinedInitials): Parser |
||
326 | } |
||
327 |
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.