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 |
||
17 | class Parser |
||
18 | { |
||
19 | /** |
||
20 | * @var string |
||
21 | */ |
||
22 | protected $whitespace = " \r\n\t"; |
||
23 | |||
24 | /** |
||
25 | * @var array |
||
26 | */ |
||
27 | protected $mappers = []; |
||
28 | |||
29 | /** |
||
30 | * @var array |
||
31 | */ |
||
32 | protected $languages = []; |
||
33 | |||
34 | /** |
||
35 | * @var array |
||
36 | */ |
||
37 | protected $nicknameDelimiters = []; |
||
38 | |||
39 | /** |
||
40 | * @var int |
||
41 | */ |
||
42 | protected $maxSalutationIndex = 0; |
||
43 | |||
44 | /** |
||
45 | * @var int |
||
46 | */ |
||
47 | protected $maxCombinedInitials = 2; |
||
48 | |||
49 | public function __construct(array $languages = []) |
||
57 | |||
58 | /** |
||
59 | * split full names into the following parts: |
||
60 | * - prefix / salutation (Mr., Mrs., etc) |
||
61 | * - given name / first name |
||
62 | * - middle initials |
||
63 | * - surname / last name |
||
64 | * - suffix (II, Phd, Jr, etc) |
||
65 | * - extension (Germany: nobility predicate is part of lastname) |
||
66 | * - title (Germany: academic titles are usually used as name parts between salutation and given name) |
||
67 | * - company (the string contains typical characteristics for a company name and is returned identically) |
||
68 | * |
||
69 | * @param string $name |
||
70 | * @return Name |
||
71 | */ |
||
72 | public function parse($name): Name |
||
95 | |||
96 | /** |
||
97 | * handles split-parsing of comma-separated name parts |
||
98 | * |
||
99 | * @param string $first - the name part left of the comma |
||
100 | * @param string $second - the name part right of the comma |
||
101 | * @param string $third |
||
102 | * @return Name |
||
103 | */ |
||
104 | protected function parseSplitName($first, $second, $third): Name |
||
114 | |||
115 | /** |
||
116 | * @return Parser |
||
117 | */ |
||
118 | View Code Duplication | protected function getFirstSegmentParser(): Parser |
|
135 | |||
136 | /** |
||
137 | * @return Parser |
||
138 | */ |
||
139 | View Code Duplication | protected function getSecondSegmentParser(): Parser |
|
157 | |||
158 | protected function getThirdSegmentParser(): Parser |
||
168 | |||
169 | /** |
||
170 | * get the mappers for this parser |
||
171 | * |
||
172 | * @return array |
||
173 | */ |
||
174 | public function getMappers(): array |
||
193 | |||
194 | /** |
||
195 | * get name as company if parts matches company identifiers |
||
196 | * |
||
197 | * @param string $name |
||
198 | * @return array |
||
199 | */ |
||
200 | protected function getCompany(string $name): array |
||
206 | |||
207 | /** |
||
208 | * set the mappers for this parser |
||
209 | * |
||
210 | * @param array $mappers |
||
211 | * @return Parser |
||
212 | */ |
||
213 | public function setMappers(array $mappers): Parser |
||
219 | |||
220 | /** |
||
221 | * normalize the name |
||
222 | * |
||
223 | * @param string $name |
||
224 | * @return string |
||
225 | */ |
||
226 | protected function normalize(string $name): string |
||
234 | |||
235 | /** |
||
236 | * get a string of characters that are supposed to be treated as whitespace |
||
237 | * |
||
238 | * @return string |
||
239 | */ |
||
240 | public function getWhitespace(): string |
||
244 | |||
245 | /** |
||
246 | * set the string of characters that are supposed to be treated as whitespace |
||
247 | * |
||
248 | * @param string $whitespace |
||
249 | * @return Parser |
||
250 | */ |
||
251 | public function setWhitespace($whitespace): Parser |
||
257 | |||
258 | /** |
||
259 | * @return array |
||
260 | */ |
||
261 | protected function getPrefixes() |
||
272 | |||
273 | /** |
||
274 | * @return array |
||
275 | */ |
||
276 | protected function getSuffixes() |
||
287 | |||
288 | /** |
||
289 | * @return array |
||
290 | */ |
||
291 | protected function getSalutations() |
||
302 | |||
303 | /** |
||
304 | * @return array |
||
305 | */ |
||
306 | protected function getExtensions() |
||
317 | |||
318 | /** |
||
319 | * @return array |
||
320 | */ |
||
321 | protected function getTitles() |
||
332 | |||
333 | /** |
||
334 | * @return array |
||
335 | */ |
||
336 | protected function getCompanies() |
||
347 | |||
348 | /** |
||
349 | * @return array |
||
350 | */ |
||
351 | public function getNicknameDelimiters(): array |
||
355 | |||
356 | /** |
||
357 | * @param array $nicknameDelimiters |
||
358 | * @return Parser |
||
359 | */ |
||
360 | public function setNicknameDelimiters(array $nicknameDelimiters): Parser |
||
366 | |||
367 | /** |
||
368 | * @return int |
||
369 | */ |
||
370 | public function getMaxSalutationIndex(): int |
||
374 | |||
375 | /** |
||
376 | * @param int $maxSalutationIndex |
||
377 | * @return Parser |
||
378 | */ |
||
379 | public function setMaxSalutationIndex(int $maxSalutationIndex): Parser |
||
385 | |||
386 | /** |
||
387 | * @return int |
||
388 | */ |
||
389 | public function getMaxCombinedInitials(): int |
||
393 | |||
394 | /** |
||
395 | * @param int $maxCombinedInitials |
||
396 | * @return Parser |
||
397 | */ |
||
398 | public function setMaxCombinedInitials(int $maxCombinedInitials): Parser |
||
404 | } |
||
405 |
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.