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 |
||
12 | final class StopWords |
||
13 | { |
||
14 | /** |
||
15 | * @var array |
||
16 | */ |
||
17 | private static $availableLanguages = array( |
||
18 | 'ar', |
||
19 | 'bg', |
||
20 | 'ca', |
||
21 | 'cz', |
||
22 | 'da', |
||
23 | 'de', |
||
24 | 'el', |
||
25 | 'en', |
||
26 | 'eo', |
||
27 | 'es', |
||
28 | 'et', |
||
29 | 'fi', |
||
30 | 'fr', |
||
31 | 'hi', |
||
32 | 'hr', |
||
33 | 'hu', |
||
34 | 'id', |
||
35 | 'it', |
||
36 | 'ka', |
||
37 | 'lt', |
||
38 | 'lv', |
||
39 | 'nl', |
||
40 | 'no', |
||
41 | 'pl', |
||
42 | 'pt', |
||
43 | 'ro', |
||
44 | 'ru', |
||
45 | 'sk', |
||
46 | 'sv', |
||
47 | 'tr', |
||
48 | 'uk', |
||
49 | 'vi' |
||
50 | ); |
||
51 | |||
52 | /** |
||
53 | * @var array |
||
54 | */ |
||
55 | private $stopWords = array(); |
||
56 | |||
57 | /** |
||
58 | * Load language-data from one language. |
||
59 | * |
||
60 | * @param string $language |
||
61 | * |
||
62 | * @throws StopWordsLanguageNotExists |
||
63 | */ |
||
64 | 2 | View Code Duplication | private function loadLanguageData(string $language = 'de') |
72 | |||
73 | /** |
||
74 | * Get data from "/data/*.php". |
||
75 | * |
||
76 | * @param string $file |
||
77 | * |
||
78 | * @return array <p>Will return an empty array on error.</p> |
||
79 | */ |
||
80 | 2 | private function getData(string $file): array |
|
98 | |||
99 | /** |
||
100 | * Get the stop-words from one language. |
||
101 | * |
||
102 | * @param string $language |
||
103 | * |
||
104 | * @return array |
||
105 | * |
||
106 | * @throws StopWordsLanguageNotExists |
||
107 | */ |
||
108 | 2 | View Code Duplication | public function getStopWordsFromLanguage(string $language = 'de'): array |
120 | |||
121 | 1 | private function loadLanguageDataAll() |
|
129 | |||
130 | /** |
||
131 | * Get all stop-words from all languages. |
||
132 | * |
||
133 | * @return array |
||
134 | * |
||
135 | * @throws StopWordsLanguageNotExists |
||
136 | */ |
||
137 | 1 | public function getStopWordsAll(): array |
|
143 | } |
||
144 |
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.